public static void main(String[] args) {
System.out.println("欢迎使用计算机");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
double i =scanner.nextDouble();
System.out.println("请选择操作");
System.out.println("1加,2减,3乘,4除");
int j = scanner.nextInt();//对j赋值为加减乘除4个符号
System.out.println("请输入第二个数");
double z = scanner.nextDouble();
while (j == 4 && z == 0){ // 除数不可为0
System.out.println("除数不能为0,请重新输入第二个数");
z = scanner.nextDouble();
}
double result = 0;
switch (j){
case 1:
result=(double)i + z;
break;
case 2:
result=i - z;
break;
case 3:
result=i * z;
break;
case 4:
result=i / z;
break;
default:
break;
}
System.out.println("计算结果为"+result);
scanner.close();
}
}
狂神说java计算器代码,有错误请指出谢谢
最新推荐文章于 2024-11-17 21:07:59 发布