From 252f14b8cf606692e60f3af23c02c075b0704b32 Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Halili Date: Sat, 8 Nov 2025 13:30:27 +0000 Subject: [PATCH] gonna be tweaking at 06 PT 1 now Signed-off-by: GitHub --- .../studentops/projects/calucatorPrompts.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 java/src/dev/andreijiroh/studentops/projects/calucatorPrompts.java diff --git a/java/src/dev/andreijiroh/studentops/projects/calucatorPrompts.java b/java/src/dev/andreijiroh/studentops/projects/calucatorPrompts.java new file mode 100644 index 0000000..641980f --- /dev/null +++ b/java/src/dev/andreijiroh/studentops/projects/calucatorPrompts.java @@ -0,0 +1,65 @@ +package dev.andreijiroh.studentops.projects; +import java.util.Scanner; + +/** + * Computer Programming 1 - 06 Performance Task 1 + * @author Andrei Jiroh Eugenio Halili (STI College Meycauayan) + */ +public class calucatorPrompts { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int firstNumber; + int secondNumber; + String mathOperation; + + System.out.print("Enter the first number: "); + firstNumber = scanner.nextInt(); + + System.out.print("Enter the second number: "); + secondNumber = scanner.nextInt(); + scanner.nextLine(); // consume the leftover newline to avoid input issues + + while (true) { + System.out.print("Select the math operation to use between these two numbers: [+ | - | / | * ] "); + mathOperation = scanner.nextLine().trim(); + + if (mathOperation.isEmpty() || mathOperation.isBlank()) { + System.out.println("error: Math operation shouldn't be blank!"); + } else { + break; + } + } + + int mathOperationResult; + + switch (mathOperation) { + case "+": + mathOperationResult = firstNumber + secondNumber; + System.out.println(String.format("Math: %s + %s = %s", firstNumber, secondNumber, mathOperationResult)); + break; + case "-": + mathOperationResult = firstNumber - secondNumber; + System.out.println(String.format("Math: %s - %s = %s", firstNumber, secondNumber, mathOperationResult)); + break; + case "*": + mathOperationResult = firstNumber * secondNumber; + System.out.println(String.format("Math: %s * %s = %s", firstNumber, secondNumber, mathOperationResult)); + break; + case "/": + // we don't want to cause problems due to division by zero here + if (secondNumber == 0) { + System.out.println("error: Division by zero is not allowed!"); + System.exit(1); + } + double divisionResult = (double) firstNumber / secondNumber; + System.out.println(String.format("Math: %s / %s = %s", firstNumber, secondNumber, divisionResult)); + break; + default: + System.out.println("Unsupported math operation (signs only)"); + System.exit(1); + } + + scanner.close(); // always close the scanner to avoid memory leaks + } +} \ No newline at end of file -- 2.51.2