//实现计算器
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean flag=true;
while(flag){
System.out.println("请选择要进行的操作(+、-、*、/),输入x代表终止运行:");
String operator = scanner.next();
System.out.print("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.print("请输入第二个数字:");
double num2 = scanner.nextDouble();
switch (operator) {
case "+":
System.out.printf("%.2f + %.2f = %.2f", num1, num2, num1 + num2);
System.out.println();
break;
case "-":
System.out.printf("%.2f - %.2f = %.2f", num1, num2, num1 - num2);
System.out.println();
break;
case "*":
System.out.printf("%.2f * %.2f = %.2f", num1, num2, num1 * num2);
System.out.println();
break;
case "/":
if (num2 != 0) {
System.out.printf("%.2f / %.2f = %.2f", num1, num2, num1 / num2);
System.out.println();
} else {
System.out.println("除数不能为零!");
}
break;
case "x":
flag=false;
System.out.println("结束运行!");
scanner.close();
break;
default:
System.out.println("无效的运算符!");
break;
}
}
}
标签:%.,num1,num2,System,简易,2f,计算器,out
From: https://www.cnblogs.com/Yolanda-fan/p/18023228