So I'm writing a code for a calculator in Java as a homework and I wanted to include an else statement in the end where if the user didn't input a valid operator, you get an error msg. Problem is that even when I input a valid operator, it prints the result and then the else error msg. Any help would be appreciated.
public class Main {
public static void main(String[] args){
Scanner inputi=new Scanner(System.in);
double num1,num2;
char operator;
System.out.println("Please enter the first number: ");
num1=inputi.nextDouble();
System.out.println("Please enter the second number: ");
num2=inputi.nextDouble();
Scanner oper=new Scanner(System.in);
System.out.println("Please enter the operator, the operators are +,-,*,/,%");
operator = oper.next().charAt(0);
if(operator== '+') {
System.out.println("The sum of these numbers is: "+(num1+num2));
}
if(operator=='-') {
System.out.println("The diff of these numbers is: "+ (num1-num2));
}
if(operator=='*') {
System.out.println("The result of this multiplication is: "+(num1*num2));
}
if(operator=='/') {
System.out.println("The division is: "+(num1/num2));
}
if(operator=='%') {
System.out.println("The module is: "+(num1%num2));
}
else {
System.out.println("Please just pick an operator..");
}
}
}