因此,我正在用 Java 编写计算器代码作为家庭作业,我想在最后包含一个 else 语句,如果用户没有输入有效的运算符,则会收到错误消息。问题是,即使我输入有效的运算符,它也会打印结果,然后打印 else 错误消息。任何帮助,将不胜感激。
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..");
}
}
}
浮云间
一只斗牛犬
偶然的你
紫衣仙女
相关分类