aspose file tools*
The moose likes Beginning Java and the fly likes Whille loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whille loop" Watch "Whille loop" New topic
Author

Whille loop

Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

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?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
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

Joined: Mar 28, 2003
Posts: 11279
    
  59

Where do you re-assign userInt?
Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
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

Joined: Mar 28, 2003
Posts: 11279
    
  59

Yep - that is the right area. In particular, line 24: what are you assigning the result of keyboard.nextInt() to?
Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
Thank you so much. I think I got it
Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
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

SCJP 1.6
SCWCD 1.5 (Preparing...)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Samrat: we try not to provide explicit answers, but rather steer people in the right direction.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Feb 04, 2009
Posts: 40
I am extremely sorry for this...My intention was not of that sort
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Apologies accepted We have all done that sort of thing, myself included ( ). I am sure you won't do it again
 
wood burning stoves
 
subject: Whille loop
 
Similar Threads
New Help on letting the user to choose to play again or not. here is what i have
Noob computer science student.
help needed with my while loop
Trouble with Guessing Game
Need help ending number guessing game and asking if user wants to play again