public class CalculatorDemo {
public static void main(String[] args) {
Scanner scanner =new.Scanner(System.in);
System.out.println(“请输入第一个数字”);
if (scanner.hasNextDouble()) {
double num1 = scanner.nextDouble();
System.out.println(“第一个数字:” + num1);
System.out.println(“请输入运算符”);
if (scanner.hasNext()) {
String symbol = scanner.next();
System.out.println(num1 + symbol);
System.out.println(“请输入第二个数字”);
if (scanner.hasNextDouble()) {
double num2 = scanner.nextDouble();
switch (symbol) {
case “+”:
System.out.println(num1 + symbol + num2 + “=” + (num1 + num2));
break;
case “-”:
System.out.println(num1 + symbol + num2 + “=” + (num1 - num2));
break;
case “*”:
System.out.println(num1 + symbol + num2 + “=” + (num1 * num2));
break;
case “/”:
System.out.println(num1 + symbol + num2 + “=” + (num1 / num2));
break;
default:
System.out.println(“输入字符非法,不能进行运算”);
}
}
}
}
scanner.close();
}
}
java实现加减乘除计算,刚学习编程,欢迎指导,感谢
最新推荐文章于 2024-04-21 13:45:00 发布