帮我用java写一个计算器
时间: 2023-11-17 15:07:09 浏览: 84
好的,下面是一个简单的Java计算器程序,可以进行四则运算:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
double num1 = sc.nextDouble();
System.out.println("请输入运算符号(+、-、*、/):");
char operator = sc.next().charAt(0);
System.out.println("请输入第二个数字:");
double num2 = sc.nextDouble();
double result = 0;
switch(operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default: System.out.println("输入的运算符号不正确"); return;
}
System.out.println("计算结果为:" + result);
}
}
```
运行程序后,按照提示输入两个数字和运算符号即可得到计算结果。注意,输入的运算符号必须是+、-、*、/中的一个。如果输入的运算符号不正确,程序会提示错误并退出。
阅读全文
相关推荐











