From e929828dc59284e2f433735d1576bd7889f9b7a0 Mon Sep 17 00:00:00 2001 From: Cameron Seamons Date: Sun, 4 Jan 2026 21:48:41 -0700 Subject: [PATCH] Created random number / coin flip generator with error catching --- practice_projects/src/randomNums.java | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 practice_projects/src/randomNums.java diff --git a/practice_projects/src/randomNums.java b/practice_projects/src/randomNums.java new file mode 100644 index 0000000..19b0d78 --- /dev/null +++ b/practice_projects/src/randomNums.java @@ -0,0 +1,74 @@ +import java.util.Random; +import java.util.Scanner; + +public class randomNums { + public static void main(String[] args) { + + Random random = new Random(); + Scanner scanner = new Scanner(System.in); + + int number; + boolean isHeads; + String choice; + int limit; + + // loop to make sure user picks valid selection + while (true) { + System.out.print("Random number or coin flip? (num/coin) "); + choice = scanner.nextLine(); + + // If user chose Num + if (choice.equals("num")) { + + // Validate numeric input + while (true) { + System.out.print("Random number between 1 and what? "); + String input = scanner.nextLine(); + + // Catch invalid number inputs + try { + limit = Integer.parseInt(input); + + if (limit <= 0) { + System.out.println("❌ Please enter a number greater than 0.\n"); + continue; + } + + break; + + } catch (NumberFormatException e) { + System.out.println("❌ That's not a valid number. Try again.\n"); + } + } + + // Pick the random number + number = random.nextInt(limit) + 1; + // Print out the random number + System.out.println("\nRandom number between 1 and " + limit +": \n" + number); + break; + } + + + // If user chose Coin + else if (choice.equals("coin")) { + // Flip a coin + isHeads = random.nextBoolean(); + // Print out the result + if (isHeads) { + System.out.println("\nHeads"); + } else { + System.out.println("\nTails"); + } + break; + } + + // If the user choice was invalid. Print error + else { + System.out.println("Invalid choice. Please type 'num' or 'coin'.\n"); + } + + } + scanner.close(); + } + +}