public class PracticeDemo1 {标签:Java,calculate,double,practice,System,println,total,public,out From: https://www.cnblogs.com/onlyxue/p/16755171.html
//计算器2.0
public static void main(String[] args) {
count();
}
public static void count(){
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("请输入加减乘除类型,按c结束");
char C = scanner.next().charAt(0);
if(C=='c'){
break;
}
System.out.println("请输入需要就算的两个数字:");
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
double total = 0.0;
switch (C){
case '+':
total = add(num1,num2);
System.out.println(total);
break;
case '-':
total = sub(num1,num2);
System.out.println(total);
break;
case '*':
total = mul(num1,num2);
System.out.println(total);
break;
case '/':
total = div(num1,num2);
System.out.println(total);
break;
default:
System.out.println("输入错误");
}
}
scanner.close();
}
//加法
public static double add(double a,double b){
return a+b;
}
//减法
public static double sub(double a,double b){
return a-b;
}
//乘法
public static double mul(double a,double b){
return a*b;
}
//除法
public static double div(double a,double b){
return a/b;
}
}