Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Whille loop

 
Greenhorn
Posts: 10
  • 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
 
author & internet detective
Posts: 39433
768
Eclipse IDE VI Editor Java
  • 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.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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
  • 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
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you re-assign userInt?
 
Fifi Akt
Greenhorn
Posts: 10
  • 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
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I think I got it:)
 
Ranch Hand
Posts: 41
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • 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.
 
Marshal
Posts: 65407
248
  • 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: 41
  • 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
Marshal
Posts: 65407
248
  • 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
 
You can't expect to wield supreme executive power just because
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!