使用idea编写简易计算器
时间: 2023-09-23 20:06:53 浏览: 225
简单编写计算器
可以的,您需要定义一些基本的数学运算符和输入输出方式,然后实现计算逻辑。以下是一个简单的示例代码:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
double num1, num2, result;
char operator;
Scanner input = new Scanner(System.in);
System.out.println("Enter first number:");
num1 = input.nextDouble();
System.out.println("Enter second number:");
num2 = input.nextDouble();
System.out.println("Enter operator (+, -, *, /):");
operator = input.next().charAt(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("Invalid operator");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
```
阅读全文