This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I want to ask something. I wrote a code. But when I run it, it never stops. I do not know where I did wrong. If you have time, could you check it.
This is the question. The program performs the following actions:
1. Generate a secret random number between 1 and 100 (see below how to do that), and print a message to communicate that to the user;
2. Repeatedly ask the user for a guess, and tell the user whether the guess is too big or too small compared to the secret number;
3. When the user finally guesses the correct number, print a message stating how many guesses were needed, and a message assessing the user's performance according to the following table:
This is the code I wrote;
Aww, beat me by a few minutes. If I hadnt gone to see what the vacuum cleaner was stuck on I would have answered this already.
Also - think about where in the program you should be reading the input line. Right now you have it read once before you go into the loop - is that really desirable?
Andrew Monkhouse wrote:Where do you re-assign userInt?
I am really new java. Really thank you for your patience.
# while(userInt!=x)
# {
# if(userInt<x){
# System.out.println("Your guess is too small.");
# System.out.println("What is your guess?");
# }
# else{
# System.out.println("Your guess is too big.");
# System.out.println("What is your guess?");
# }
# counter++;
# keyboard.nextInt();
# }
Do you mention the bold area? If not I think I do not understand >
Andrew Monkhouse
author and jackaroo
Marshal Commander
while(userAns1!=x){
if(userAns1<x){
System.out.println("Your guess is too small.");
System.out.println("What is your guess?");
}
else{
System.out.println("Your guess is too big.");
System.out.println("What is your guess?");
}
counter++;
userAns1=keyboard.nextInt();
}
I feel now it shouls work fine ...because you were not validatng the user input in the loop