用java编写一个简易计算器
时间: 2023-09-20 09:11:29 浏览: 112
好的,这个问题我可以回答。以下是使用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;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("输入的运算符无效");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
阅读全文