Scanner scan = new Scanner(System.in);
System.out.print("Enter 2 numbers: ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
System.out.println("Numbers Saved! Choose your operator + - * / ");
String operator = scan.next();
int result;
result = (operator.equals("+")) ? (num1 + num2) : (num1);
System.out.println("The addition result is " +result);
result = (operator.equals("-")) ? (num1 - num2) : (num1);
System.out.println("The subtraction result is " +result);
result = (operator.equals("*")) ? (num1 * num2) : (num1);
System.out.println("The multiplication result is " +result);
result = (operator.equals("/")) ? (num1 / num2) : (num1);
System.out.println("The division result is " +result);
}
}这是我的简单计算器代码,例如,当我选择+选项时,它会运行所有System.out.println行,我如何防止它这样做并且只执行与运算符匹配的println?
喵喵时光机
DIEA
相关分类