From dc5db999688fbb9563edfbe008d7bb8e98ed31d7 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 14 Jan 2024 18:50:54 +0800 Subject: [PATCH] code updated --- src/xs_ibdpcompsci_javaintro/practice1.java | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/xs_ibdpcompsci_javaintro/practice1.java diff --git a/src/xs_ibdpcompsci_javaintro/practice1.java b/src/xs_ibdpcompsci_javaintro/practice1.java new file mode 100644 index 0000000..f853121 --- /dev/null +++ b/src/xs_ibdpcompsci_javaintro/practice1.java @@ -0,0 +1,66 @@ +package xs_ibdpcompsci_javaintro; +import java.util.*; + +class RPN_Calculator { + static ArrayList request_input() { + Scanner input = new Scanner(System.in); + + // Initialize variables. + ArrayList USER_INPUTS = new ArrayList(); + 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 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 NUMBERS_INPUT = new ArrayList(); + while (true) { + NUMBERS_INPUT = request_input(); + + + } + } +} \ No newline at end of file