use Java to design a working calculator
时间: 2024-04-12 09:28:15 浏览: 13
Sure! I can help you design a simple calculator using Java. Here's an example code:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter the operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
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("Invalid operator!");
return;
}
System.out.println("Result: " + result);
}
}
```
This program prompts the user to enter two numbers and an operator (+, -, *, /). It then performs the corresponding operation and displays the result. I hope this helps! Let me know if you have any further questions.