-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AdvancedCalculator.java
127 lines (118 loc) · 3.73 KB
/
AdvancedCalculator.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Advance Calculator
*
* @author PP-Namias
*
* Licensed under the MIT
*
*/
/**
* @param [input] The scanner for user input.
* @param [exit] Used to exit the program.
* @param [operator] The seleceted operator
* @param [numStrings] The parameters needed to make the array of numbers
* @param [numbers] The parameters needed to validaite the numbers
* @param [result] The reuslt of the calculations
*/
/*
* Features:
* -> Add multiple numbers
* -> loops infinitly
* -> Gets the operator (+, -, *, /)
* -> View the result
* -> Exit
* -> Error handling
*
*
* Future Features:
* Make an whole statement for the result and then print it out at the end of the loop
* -> Add multiple numbers
* -> Make an equation for the result
* -> View the result
* -> Exit
*
* Example:
* 1 + 2 + 3 + 4 + 5 = 15
* 2 * 5 / 10 + 3 = 3.5
* 6 - 5 + 4 - 3 + 2 - 1 = 1
* 1 + 2 * 3 = 7
*
* Make an char that allows the user to input the equation
* and then split it up into an array of strings
* and then parse it into an array of doubles
* and then do the calculations and then print out the result
* and then loop it back to the start of the program
* and then make an exit statement
* and then make an error handling statement
* and then make an equation statement
*/
import java.util.Scanner;
public class AdvancedCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("Select an operation (+, -, *, /) or enter 'exit' to quit:");
String operation = input.nextLine();
if (operation.equalsIgnoreCase("exit")) {
exit = true;
} else if (operation.equals("+") || operation.equals("-") || operation.equals("*") || operation.equals("/")) {
System.out.println("Enter numbers (separated by spaces):");
String[] numStrings = input.nextLine().split("\\s+");
double[] numbers = new double[numStrings.length];
for (int i = 0; i < numStrings.length; i++) {
numbers[i] = Double.parseDouble(numStrings[i]);
}
double result = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (operation.equals("+")) {
if (i == 1) {
System.out.print(numbers[0]);
System.out.print(" + " + numbers[1]);
}
else if (i >= 1) {
System.out.print(" + " + numbers[i]);
}
result += numbers[i];
} else if (operation.equals("-")) {
if (i == 1) {
System.out.print(numbers[0]);
System.out.print(" - " + numbers[1]);
}
else if (i >= 1) {
System.out.print(" - " + numbers[i]);
}
result -= numbers[i];
} else if (operation.equals("*")) {
if (i == 1) {
System.out.print(numbers[0]);
System.out.print(" * " + numbers[1]);
}
else if (i >= 1) {
System.out.print(" * " + numbers[i]);
}
result *= numbers[i];
} else if (operation.equals("/")) {
if (numbers[i] == 0) {
System.out.println("Cannot divide by zero");
break;
} else {
if (i == 1) {
System.out.print(numbers[0]);
System.out.print(" / " + numbers[1]);
}
else if (i >= 1) {
System.out.print(" / " + numbers[i]);
}
result /= numbers[i];
}
}
}
System.out.println(" = " + result);
} else {
System.out.println("Invalid operation");
}
}
input.close();
}
}