要求:java写一个计算器,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现
废话少说,代码如下:
public class calculator {
public static void main(String[] args) {
//计算器循环
while(true){
Scanner k = new Scanner(System.in);
System.out.println("请输入一个数字:");
double x = k.nextDouble();
System.out.println("请输入一个符号:");
String y = k.next();
System.out.println("请输入一个数字:");
double z = k.nextDouble();
//switch循环进行判断计算
switch(y){
case "+":
System.out.println(add(x,z));
break;
case "-":
System.out.println(sub(x,z));
break;
case "*":
System.out.println(mul(x,z));
break;
case "/":
System.out.println(div(x,z));
break;
default:
System.out.println("error");
}
}
}
//加法运算
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);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)