@@ -10,65 +10,72 @@ public static void main(String[] args) {
10
10
int attempts = 5 ;
11
11
12
12
System .out .println ("Simple Calculator" );
13
-
14
- double firstNumber = getUserInput ("Enter the first number: " );
15
- double secondNumber = getUserInput ("Enter the second number: " );
16
- int operationChoice ;
17
-
18
- while (true ) {
19
- System .out .println ("Choose an operation:" );
20
- System .out .println ("1. Addition" );
21
- System .out .println ("2. Subtraction" );
22
- System .out .println ("3. Multiplication" );
23
- System .out .println ("4. Division" );
24
- System .out .print ("Enter the number corresponding to the operation: " );
25
-
26
- if (scanner .hasNextInt ()) {
27
- operationChoice = scanner .nextInt ();
28
- if (operationChoice >= 1 && operationChoice <= 4 ) {
29
- break ;
13
+ char recur ;
14
+
15
+ do {
16
+ double firstNumber = getUserInput ("Enter the first number: " );
17
+ double secondNumber = getUserInput ("Enter the second number: " );
18
+ int operationChoice ;
19
+
20
+ while (true ) {
21
+ System .out .println ("Choose an operation:" );
22
+ System .out .println ("1. Addition" );
23
+ System .out .println ("2. Subtraction" );
24
+ System .out .println ("3. Multiplication" );
25
+ System .out .println ("4. Division" );
26
+ System .out .print ("Enter the number corresponding to the operation: " );
27
+
28
+ if (scanner .hasNextInt ()) {
29
+ operationChoice = scanner .nextInt ();
30
+ if (operationChoice >= 1 && operationChoice <= 4 ) {
31
+ break ;
32
+ } else {
33
+ System .out .println ("Invalid operation choice. Please enter a number between 1 and 4." );
34
+ scanner .nextLine (); // Clear the input buffer
35
+ }
30
36
} else {
31
- System .out .println ("Invalid operation choice . Please enter a number between 1 and 4 ." );
37
+ System .out .println ("Invalid input . Please enter a valid number ." );
32
38
scanner .nextLine (); // Clear the input buffer
33
39
}
34
- } else {
35
- System .out .println ("Invalid input. Please enter a valid number." );
36
- scanner .nextLine (); // Clear the input buffer
40
+
41
+ attempts --;
42
+
43
+ if (attempts == 0 ) {
44
+ System .out .println ("You've reached the maximum number of attempts. Exiting the program." );
45
+ System .exit (1 );
46
+ }
37
47
}
38
48
39
- attempts -- ;
49
+ double result = 0.0 ;
40
50
41
- if (attempts == 0 ) {
42
- System .out .println ("You've reached the maximum number of attempts. Exiting the program." );
43
- System .exit (1 );
51
+ switch (operationChoice ) {
52
+ case 1 :
53
+ result = firstNumber + secondNumber ;
54
+ break ;
55
+ case 2 :
56
+ result = firstNumber - secondNumber ;
57
+ break ;
58
+ case 3 :
59
+ result = firstNumber * secondNumber ;
60
+ break ;
61
+ case 4 :
62
+ if (secondNumber != 0 ) {
63
+ result = firstNumber / secondNumber ;
64
+ } else {
65
+ System .out .println ("Error: Division by zero is not allowed." );
66
+ System .exit (1 );
67
+ }
68
+ break ;
69
+ default :
70
+ break ;
44
71
}
45
- }
46
72
47
- double result = 0.0 ;
48
-
49
- switch (operationChoice ) {
50
- case 1 :
51
- result = firstNumber + secondNumber ;
52
- break ;
53
- case 2 :
54
- result = firstNumber - secondNumber ;
55
- break ;
56
- case 3 :
57
- result = firstNumber * secondNumber ;
58
- break ;
59
- case 4 :
60
- if (secondNumber != 0 ) {
61
- result = firstNumber / secondNumber ;
62
- } else {
63
- System .out .println ("Error: Division by zero is not allowed." );
64
- System .exit (1 );
65
- }
66
- break ;
67
- default :
68
- break ;
69
- }
73
+ System .out .println ("Result: " + result );
74
+
75
+ System .out .println ("Do you wish to perform another operation? Enter y for YES; any other key means NO\n " );
76
+ recur = scanner .next ().charAt (0 );
70
77
71
- System . out . println ( "Result: " + result );
78
+ } while ( recur == 'y' );
72
79
}
73
80
74
81
private static double getUserInput (String prompt ) {
0 commit comments