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("请输入运算符号(+、-、*、/):");
char operator = scanner.next().charAt(0);
System.out.print("请输入第二个数字:");
double num2 = scanner.nextDouble();
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);
}
}
用户评论