Answer the question
In order to leave comments, you need to log in
How to cast integer to double type in conditional statement?
Hi guys! I already asked a similar question in this thread How to cast an integer to double and then times...
But how to do the same in this example:
package Tu;
import java.util.Scanner;
public class ComplexCalculator {
public static void main(String[] args) {
try (Scanner dev = new Scanner(System.in)) {
int a, b, result = 1;
char ch;
System.out.println("Plese enter operator {+, -, *, /, %, ^, !, v, 2, 3}");
ch = dev.nextLine().charAt(0);
if (ch == '!') {
System.out.println("Please enter A");
a = dev.nextInt();
for (int i = 1; i <= a; i++) {
result = result * i;
}
System.out.println("a! = " + (result));
} else if (ch == 'v') {
System.out.println("Please enter A");
}
}
}
}
else if (ch == 'v') {
System.out.println("Please enter A");
else if (ch == 'v') {
System.out.println("Please enter A");
a = dev.nextInt();
Answer the question
In order to leave comments, you need to log in
Why do you need to declare a variable outside of ifs? Declare inside, in case of factorial int, in case of root - double. And read the input accordingly.
If you really need it outside, then you need to declare it as the maximum allowable type for all operations - i.e. double, and already in special cases (factorial) - lead to an int (with a check, and suddenly there is still not a whole).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question