code updated
This commit is contained in:
parent
8c97354f51
commit
dc5db99968
1 changed files with 66 additions and 0 deletions
66
src/xs_ibdpcompsci_javaintro/practice1.java
Normal file
66
src/xs_ibdpcompsci_javaintro/practice1.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
package xs_ibdpcompsci_javaintro;
|
||||
import java.util.*;
|
||||
|
||||
class RPN_Calculator {
|
||||
static ArrayList<Object> request_input() {
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
// Initialize variables.
|
||||
ArrayList<Object> USER_INPUTS = new ArrayList<Object>();
|
||||
String USER_INPUT_CURRENT = "";
|
||||
Boolean USER_INPUT_MORE = true;
|
||||
|
||||
// Loop for input.
|
||||
do {
|
||||
USER_INPUT_CURRENT = input.nextLine();
|
||||
|
||||
if (USER_INPUT_CURRENT.isEmpty() || USER_INPUT_CURRENT.isBlank()) {
|
||||
USER_INPUT_MORE = false;
|
||||
} else if (USER_INPUT_CURRENT.matches("-?\\d+(\\.\\d+)?")) {
|
||||
USER_INPUTS.add(Float.parseFloat(USER_INPUT_CURRENT));
|
||||
} else {
|
||||
if (USER_INPUT_CURRENT.matches("[+\\-*\\/^]") && (USER_INPUTS.size() > 2)) {
|
||||
USER_INPUTS.add(USER_INPUT_CURRENT);
|
||||
} else if (USER_INPUT_CURRENT.toLowerCase() == "exit") {
|
||||
System.exit(0);
|
||||
} else {
|
||||
// The user inputs a wrong code. Let the user edit it.
|
||||
System.out.println("\\033[A");
|
||||
};
|
||||
};
|
||||
} while (USER_INPUT_MORE == true);
|
||||
|
||||
input.close();
|
||||
return (USER_INPUTS);
|
||||
}
|
||||
|
||||
static float calculate(ArrayList<Object> ENTRIES_LIST, String OPERATION) {
|
||||
// Calculate length.
|
||||
int ENTRIES_LIST_LENGTH = ENTRIES_LIST.size();
|
||||
|
||||
float OPERATION_RESULT = (float) ENTRIES_LIST.get(0);
|
||||
|
||||
if (ENTRIES_LIST_LENGTH > 1) {
|
||||
for (int ENTRY_CURRENT = 1; ENTRY_CURRENT < ENTRIES_LIST_LENGTH; ENTRY_CURRENT++) {
|
||||
if (OPERATION == "+") {
|
||||
OPERATION_RESULT += (float) ENTRIES_LIST.get(ENTRY_CURRENT);
|
||||
} else if (OPERATION == "-") {
|
||||
OPERATION_RESULT -= (float) ENTRIES_LIST.get(ENTRY_CURRENT);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
return (OPERATION_RESULT);
|
||||
};
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Initialize the input numbers.
|
||||
ArrayList<Object> NUMBERS_INPUT = new ArrayList<Object>();
|
||||
while (true) {
|
||||
NUMBERS_INPUT = request_input();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue