用Java写一段计算器的代码
时间: 2024-06-01 13:14:08 浏览: 209
好的,以下是Java编写的计算器代码:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个数:");
double num1 = input.nextDouble();
System.out.print("请输入运算符:");
char operator = input.next().charAt(0);
System.out.print("请输入另一个数:");
double num2 = input.nextDouble();
double result = 0.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("错误的运算符!");
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
希望对你有帮助!
阅读全文