用Java写一个计算器程序
时间: 2023-04-10 13:02:25 浏览: 113
好的,我可以回答这个问题。以下是一个简单的Java计算器程序的代码:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num1, num2, result;
char operator;
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();
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("Invalid operator");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
```
这个程序可以让用户输入两个数字和一个运算符,然后计算并输出结果。
阅读全文