This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTask1
More file actions
56 lines (49 loc) · 1.87 KB
/
Task1
File metadata and controls
56 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the first number
System.out.print("Enter the first number: ");
double firstNumber = scanner.nextDouble();
// Prompt the user to enter the second number
System.out.print("Enter the second number: ");
double secondNumber = scanner.nextDouble();
// Prompt the user to choose an operation
System.out.println("Choose an operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
double result = 0;
// Perform the selected operation based on user's choice
switch (choice) {
case 1:
result = firstNumber + secondNumber;
break;
case 2:
result = firstNumber - secondNumber;
break;
case 3:
result = firstNumber * secondNumber;
break;
case 4:
if (secondNumber != 0) {
result = firstNumber / secondNumber;
} else {
System.out.println("Error: Cannot divide by zero");
return; // Exit the program
}
break;
default:
System.out.println("Invalid choice");
return; // Exit the program
}
// Display the result
System.out.println("Result: " + result);
// Close the scanner to release resources
scanner.close();
}
}