File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whille loop

 
Fifi Akt
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

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;


i
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33699
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fifi,
"userAns1" isn't used inside the loop. Which means you are checking the value of userInt (which is the same) on each iteration through the loop.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Fifi Akt
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer, I changed but It still does not seem correct. Where I am missing?

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you re-assign userInt?
 
Fifi Akt
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep - that is the right area. In particular, line 24: what are you assigning the result of keyboard.nextInt() to?
 
Fifi Akt
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I think I got it
 
Samrat Som
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ..

Your program should look like this

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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Samrat: we try not to provide explicit answers, but rather steer people in the right direction.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Samrat: we try not to provide explicit answers, but rather steer people in the right direction.
We often delete such material, but by the time we noticed, it was too late to do anything about it.
 
Samrat Som
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am extremely sorry for this...My intention was not of that sort
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted We have all done that sort of thing, myself included ( ). I am sure you won't do it again
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic