写一个计算器,实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现(1)
public class DemoTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double result = 0;
while (scanner.hasNextDouble()){
double i = scanner.nextDouble();
String str = scanner.next();
double j = scanner.nextDouble();
switch (str){
case "+":
result = add(i,j);
System.out.println(result);
break;
case "-":
result = minus(i,j);
System.out.println(result);
break;
case "*":
result = multiply(i,j);
System.out.println(result);
break;
case "/":if (j==0){
System.out.println("除数不能为0!");break;
}else {
result = divide(i,j);
System.out.println(result);
break;
}
}
}
scanner.close();
}
public static double add(double a,double b){
return a+b;
}
public static double minus(double a,double b){
return a-b;
}
public static double multiply(double a,double b){
return a*b;
}
public static double divide(double a,double b){
return a/b;
}
}