标准型计算器Java源代码
时间: 2023-08-03 18:06:18 浏览: 56
以下是一个简单的标准型计算器Java程序的源代码,仅供参考:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num1, num2, result;
char operator;
System.out.println("Welcome to the Java Calculator!");
System.out.print("Please enter the first number: ");
num1 = input.nextDouble();
System.out.print("Please enter the operator (+, -, *, /, %): ");
operator = input.next().charAt(0);
System.out.print("Please enter the second number: ");
num2 = input.nextDouble();
switch(operator) {
case '+':
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case '-':
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case '*':
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case '/':
if(num2 != 0) {
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
} else {
System.out.println("Cannot divide by zero!");
}
break;
case '%':
result = num1 % num2;
System.out.println(num1 + " % " + num2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
}
}
}
```
该程序会提示用户输入两个数和一个运算符,然后根据运算符执行相应的计算,并输出结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)