use java design a working calculator as shown in 4th slide
时间: 2024-04-12 07:28:13 浏览: 15
Sure, I can help you design a working calculator in 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.println("Welcome to the Calculator!");
System.out.println("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.println("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Select an operation (+, -, *, /): ");
String operator = scanner.next();
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);
}
}
```
You can run this code and it will prompt you to enter two numbers and an operator (+, -, *, /). After that, it will perform the corresponding operation and display the result.