

InterviewSolution
Saved Bookmarks
1. |
Solve : Think of a number? |
Answer» I'm trying to write a program, that generates a random number, between 1 and 100, then asks the user to guess it. If the user is to high it states 'lower', if the guess is to low it states 'higher', until the correct number id guessed. I'm have trouble because i seem to keep creating infinite loops, how can i stop this? Code: [Select]import java.util.Scanner; public class ThinkOfANumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); final int num = 100; int numgen = (int)(Math.random() + num) +1; int count = 0; System.out.println("Guess any number between 0 and 100"); int guess = input.nextInt(); while (guess != numgen) { if (guess > numgen) System.out.println("Lower"); while (guess != numgen) { if (guess < numgen) System.out.println("Higher"); while (guess == numgen) System.out.println("You've got the right number, Well Done"); } } } }your formatting isn't consistent with the actual control structures; Code: [Select]public class ThinkOfANumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); final int num = 100; int numgen = (int)(Math.random() + num) +1; int count = 0; System.out.println("Guess any number between 0 and 100"); int guess = input.nextInt(); while (guess != numgen) { if (guess > numgen) System.out.println("Lower"); while (guess != numgen) { if (guess < numgen) System.out.println("Higher"); while (guess == numgen) System.out.println("You've got the right number, Well Done"); } } } }basically, your loops are nested. Additionally, if you want to keep looping then you would need to have a while loop that terminates only when it's correct. All your while loops seem to be attempting to print the same thing as long as the guess is less than , greater than, or equal to the GENERATED number, but within the loops there is no actual change of that value so the condition remains the same. Something like this ought to be written with a loop that terminates only when the guess is correct, and reads in the next input at the start. Than if it's too low you print a message, if it's too high you print a message, and outside the loop is where it gets when the guess is correct, so you woul print an appropriate message for that outside the loop.I don't understand the use of all those while loops, you should just have one while loop with the appropriate if statements inside of it. Maybe something like this. Sorry, I can't actually TEST it because I do not program in java, so you may need a little tweaking. Code: [Select]public class ThinkOfANumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); final int num = 100; int numgen = (int)(Math.random() + num) +1; int count = 0; int guess = 0; while (guess != numgen) { System.out.println("Guess any number between 0 and 100"); guess = input.nextInt(); if (guess > numgen) System.out.println("Lower"); if (guess < numgen) System.out.println("Higher"); count++; } System.out.println("You guessed it."); } } And I forgot, you may also want to print out count. I assumed that you wanted to count how many guesses. Thank BC, after some tweaks (I had the random number generator set to 101, which didn't help) , it was basically like you said. I've learned alot today about the placement of curly brackets. Thanks again, everyone. public static void main(String[] args) { Scanner input = new Scanner(System.in); final int num = 100; double numgen = (Math.random())*100; int a=(int)numgen; while (a>100 || a<0) { numgen = (Math.random())*100; a=(int)numgen; } int count = 0; System.out.println("numgen"+a); System.out.println("Guess any number between 0 and 100"); int guess = input.nextInt(); while (guess != a) { if (guess > a) System.out.println("Lower"); if (guess < a) System.out.println("Higher"); System.out.println("Guess any number between 0 and 100"); guess = input.nextInt(); } if (guess == a) { System.out.println("You've got the right number, Well Done"); } }you are getting 101...its because the random number generated is becoming 0 each time...its because you made it an int type... to get the full random value....you should get it in double value....all the numbers are like 0.5677777777 or 0.06777777777....its always in decimal value...so to be able to get a value between 0 and 100....i have multiply the random value by 100...then i cast it to an int...so that the value obtained is not a decimal one...then i have checked it the way u wanted. QUOTE from: lina19 on January 09, 2012, 02:57:47 AM you are getting 101...its because the random number generated is becoming 0 each time...its because you made it an int type... Sorry, i should have marks this as solved. As you can see by 'reply 3' it was solved last November. its because...it wasnt marked as solved...and your reply was confusing as well.. Thank BC, after some tweaks (I had the random number generator set to 101, which didn't help) , it was basically like you said. I've learned alot today about the placement of curly brackets. I had the random number generator set to 101, which didn't help: what am i supposed to understand about it???"didnt help"....you could have stated that it was solved more clearly.... INSTEAD of asking u....whether it was solved and WAITING for you to reply...it was quicker to give the answer...its ok eventhough the reply is not useful for now...it can be useful for others as well... |
|