写一个简易计算器用java
时间: 2023-06-09 14:02:36 浏览: 96
用java写一个简单计算器
5星 · 资源好评率100%
好的,下面是一个简单的计算器 Java 代码,可以实现加减乘除四则运算:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
double num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("输入第一个数字: ");
num1 = scanner.nextDouble();
System.out.print("输入第二个数字: ");
num2 = scanner.nextDouble();
System.out.print("请选择运算符 (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
switch(operator) {
case '+':
result = num1 + num2;
System.out.print(num1 + " + " + num2 + " = " + result);
break;
case '-':
result = num1 - num2;
System.out.print(num1 + " - " + num2 + " = " + result);
break;
case '*':
result = num1 * num2;
System.out.print(num1 + " * " + num2 + " = " + result);
break;
case '/':
result = num1 / num2;
System.out.print(num1 + " / " + num2 + " = " + result);
break;
default:
System.out.println("错误! 请输入正确的运算符");
return;
}
}
}
```
如果您有任何其他问题,请告诉我。
阅读全文