import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
System.out.println("欢迎使用计算器!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字:");
while (scanner.hasNextDouble()){ //while语句循环
double num1 = scanner.nextDouble();
System.out.println("请输入运算符号:");
String symbol = scanner.next();
System.out.println("请输入第二个数字:");
double num2 = scanner.nextDouble();
switch (symbol){ //判断计算符号
case "+":
add( num1 , num2 );
break;
case "-":
sub( num1 , num2 );
break;
case "*":
mul( num1 , num2 );
break;
case "/":
divi( num1 , num2 );
break;
default:
System.out.println("您的输入不规范!");
}
System.out.println("输入任意字母退出!");
}
scanner.close(); //关闭计算器
}
public static void add(double num1,double num2){
System.out.println(num1 + num2); //加法
}
public static void sub(double num1,double num2){
System.out.println(num1 - num2); //减法
}
public static void mul(double num1,double num2){
System.out.println(num1 * num2); //乘法
}
public static void divi(double num1,double num2){
if (num2 == 0){
System.out.println("除数不能为0!");
}else{
System.out.println(num1 / num2); //除法
}
}
}