java 简易计算器
时间: 2023-11-12 18:59:06 浏览: 122
java 简易计算器
以下是一个简易的 Java 计算器代码:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.print("请输入第二个数字:");
double num2 = scanner.nextDouble();
System.out.print("请输入运算符(+、-、*、/):");
char operator = scanner.next().charAt(0);
double result;
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);
}
}
```
阅读全文