use java design a working calculator as shown in 4th slide
时间: 2024-04-12 07:28:13 浏览: 53
Sure! I can help you design a basic 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.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 (+, -, *, /): ");
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("The result is: " + result);
scanner.close();
}
}
```
This code prompts the user to enter two numbers and an operator (+, -, *, /). It then performs the corresponding calculation and displays the result. Let me know if you have any questions or need further assistance!
阅读全文