From dbaaf0c768e279f8c147f859c9bae40e167d5cd6 Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 1 Mar 2023 16:37:42 -0500 Subject: [PATCH 1/3] I have added changes --- pom.xml | 4 ++++ src/main/java/Main.java | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 1efac6c..e71048d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,10 @@ io.zipcoder MicroLabs-OOP-TooLargeTooSmall 1.0-SNAPSHOT + + 11 + 11 + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 05e41a9..229d4c9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,33 @@ -/** - * Created by iyasuwatts on 10/17/17. - */ +import java.util.Scanner; + + public class Main { + public static void main(String[] args) { + int min = 0; //minimum number for range of generation + int max = 50; //maximum number for range of generation + int numGuesses = 0; //this will track the number of user's guesses + int guess = 0; //this will track the value of the user's guesses + int answer = (int) Math.floor(Math.random() * (max - min + 1) + min); //randomly generate number between min and max + + while (guess != answer) { //loop will continue until user guesses the correct value + + Scanner input = new Scanner(System.in); //initializes scanner + System.out.println("Guess the number"); //prompts user to enter their guess + guess = input.nextInt(); //stores user input into int guess + numGuesses++; //increments the value of the numGuesses variable - public static void main(String[] args){ - + if (guess < min || guess > max) { //checks if guess is below/above the expected range + System.out.println("You guess over or below the acceptable range of: " + min + " to " + max); //informs user they've guessed lower/higher than expected range + } + else if (guess > answer) { //checks if guess is higher than the answer + System.out.println("You guessed too high."); //informs user they've guessed too high + } + else if (guess < answer) { //checks if guess is less than the answer + System.out.println("You guessed too low."); //informs user they've guessed too low + } + else { + System.out.println("Congrats. You guessed " + answer + " and it only took you " + numGuesses + " tries."); + } + } } -} +} \ No newline at end of file From 7d2d34f6a4b0fd070a38cbecdfc50d7014b9359d Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 1 Mar 2023 18:59:26 -0500 Subject: [PATCH 2/3] added set to track number of user guesses instead of incrementing an int with for loop and to make sure there are no duplicates --- src/main/java/Main.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 229d4c9..3b94b26 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Scanner; @@ -7,14 +8,16 @@ public static void main(String[] args) { int max = 50; //maximum number for range of generation int numGuesses = 0; //this will track the number of user's guesses int guess = 0; //this will track the value of the user's guesses - int answer = (int) Math.floor(Math.random() * (max - min + 1) + min); //randomly generate number between min and max + //int answer = (int) Math.floor(Math.random() * (max - min + 1) + min); //randomly generate number between min and max + int answer = 5; //controlled answer value for testing + HashSet guessSet = new HashSet(); //set for storing guesses while (guess != answer) { //loop will continue until user guesses the correct value Scanner input = new Scanner(System.in); //initializes scanner System.out.println("Guess the number"); //prompts user to enter their guess guess = input.nextInt(); //stores user input into int guess - numGuesses++; //increments the value of the numGuesses variable + guessSet.add(guess); //adds guess as entry into set if (guess < min || guess > max) { //checks if guess is below/above the expected range System.out.println("You guess over or below the acceptable range of: " + min + " to " + max); //informs user they've guessed lower/higher than expected range @@ -26,7 +29,7 @@ else if (guess < answer) { //checks if guess is less than the answer System.out.println("You guessed too low."); //informs user they've guessed too low } else { - System.out.println("Congrats. You guessed " + answer + " and it only took you " + numGuesses + " tries."); + System.out.println("Congrats. You guessed " + answer + " and it only took you " + guessSet.size() + " tries."); } } } From 116767115f0b8406fd83f2e7c56f64ae911b7cb3 Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 1 Mar 2023 19:03:59 -0500 Subject: [PATCH 3/3] disabled test case and re-enabled random number generation --- src/main/java/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 3b94b26..43be493 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -8,8 +8,8 @@ public static void main(String[] args) { int max = 50; //maximum number for range of generation int numGuesses = 0; //this will track the number of user's guesses int guess = 0; //this will track the value of the user's guesses - //int answer = (int) Math.floor(Math.random() * (max - min + 1) + min); //randomly generate number between min and max - int answer = 5; //controlled answer value for testing + int answer = (int) Math.floor(Math.random() * (max - min + 1) + min); //randomly generate number between min and max + //int answer = 5; //controlled answer value for testing HashSet guessSet = new HashSet(); //set for storing guesses while (guess != answer) { //loop will continue until user guesses the correct value