when I run the code the if statements and the choice scanner are skipped. how do I fix this?
public class Main{
public static void main(String[] args){
int result = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the worlds best calulator");
// Prompt the user to enter the first integer
System.out.print("Enter the first integer: ");
int num1 = scanner.nextInt();
// Prompt the user to enter the second integer
System.out.print("Enter the second integer: ");
int num2 = scanner.nextInt();
System.out.print("choose fromm these 4 options(+,-,/,*): ");
String choice = scanner.nextLine();
if(choice.equals("+")){
result = Calculator.add(num1,num2);
System.out.println("the answer is: " + result);
} else if(choice.equals("-")){
result = Calculator.Sub(num1,num2);
}else if(choice.equals("*")){
result = Calculator.multiply(num1,num2);
}if(choice.equals("/")){
result = Calculator.divide(num1,num2);
}else{
System.out.println("invalid operand please try again");
}
// Close the scanner
scanner.close();
}
}
public class Calculator {
public static int Sub(int num1, int num2) {
return num1 - num2;
}
public static int multiply(int num1, int num2) {
return num1 * num2;
}
public static int divide(int num1, int num2) {
if (num2 == 0) {
System.out.println("Division by zero is not allowed.");
return 0;
}
return num1 / num2;
}
public static int add(int num1, int num2) {
return num1 + num2;
}
}
I was expecting after the first 2 integers were prompted by user the program would then ask for an operand to use then calculate once it goes over the if statement. But it just skips it and I’m confused.