# School Assignment AHH!

• December 15th, 2011, 05:23 PM
Europa
School Assignment AHH!
So I have this school assignment where I have to use the do while loop to make a program that has you guess a random number over and over until you get it. Unfortunately if you do get it right the first time it says you got it wrong... Could somebody please correct my code? I would be very Grateful! ^_^

import java.util.Scanner;
import java.util.Random;

public class AgainWithNumberGuessing
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();

int nbr = 1 + r.nextInt(10);
int tries = 0;

System.out.println( ". . ." );
System.out.println( "I AM SO FREAKING BOERD!" );
System.out.println( "HEY!!!! *idea alert* Wanna play a game? . . . Really you do?!?! AWESOME! \nOK. I'm thinking of a number between 1 and 10. Try to guess it!" );
int guess = keyboard.nextInt();

do
{
System.out.println();
System.out.println( "Haha! WRONG! Try again" );
guess = keyboard.nextInt();
tries++;
}

while ( guess != nbr );

if ( guess == nbr )
{
System.out.println();
System.out.println( "That's right!!! Took ya long enough! ;P \n It took you how many tries? " + tries + " was it? Haha, thanks for playing!" );
}
}
}
• December 15th, 2011, 05:47 PM
copeg
Re: School Assignment AHH!
Write down, step by step, the flow of the program as it is written. Once you retrieve the first guess, it seems you want to evaluate it against the random variable - but you just jump to assume the user is wrong
• December 17th, 2011, 02:29 AM
Tjstretch
Re: School Assignment AHH!
Probably not a bad assumption.. just saying
• December 26th, 2011, 04:00 PM
qurtan
Re: School Assignment AHH!
Try a while loop instead of a do while loop.
• December 27th, 2011, 12:18 AM
Mr.777
Re: School Assignment AHH!
Quote:

Unfortunately if you do get it right the first time it says you got it wrong
I hope you know that do-while is once executable loop whether your provided condition is true or false.
Well, you can try many approaches to get rid of this or to test if the very first number entered is correct. Either set a flag or check condition every time, just think, what do you want and do that.
• January 8th, 2012, 12:06 AM
ranjithfs1
Re: School Assignment AHH!
I think that your problem is now solved. I would like to point out the fact that your program doesn't print any message like 'You've guessed it correct' when he successfully guesses the number. Even I am a new learner and I personally feel that using while loop instead of a do while where ever possible is better.
• January 19th, 2012, 08:07 PM
houndydizzle
Re: School Assignment AHH!
If you absolutely have to use a do-while loop for your assignment then you still can. Just simply don't assume that their guess is wrong. Once you get their guess, inside of your do block insert an error check such as an if statement to see if it matches the random number. The do loop is executed no matter whether the while statement is true or false. Once thte do block is executed then it checks the while statement and if its true it reloops through the do block if its false it breaks the loop. There are many ways to take action once you've figured out whether their guess is correct or not. I would use a separate method call but you could even simply use a global boolean and just set it to true or false according to your results.

Hope this helps,

Jared
• January 20th, 2012, 09:11 AM
RaoPatek
Re: School Assignment AHH!
...

Try this out may it Helps...
• January 20th, 2012, 09:19 AM
Mr.777
Re: School Assignment AHH!
Quote:

Originally Posted by RaoPatek
if(guess==nbr)
{
System.out.println();
System.out.println( "That's right!!! Took ya long enough! ;P \n It took you how many tries? " + tries + " was it? Haha, thanks for playing!" );
}
else
{
do
{
System.out.println();
System.out.println( "Haha! WRONG! Try again" );
guess = keyboard.nextInt();
tries++;
}

while ( guess != nbr );

if ( guess == nbr )
{
System.out.println();
System.out.println( "That's right!!! Took ya long enough! ;P \n It took you how many tries? " + tries + " was it? Haha, thanks for playing!" );
}
}

Try this out may it Helps...

SPOON FEEDING is not allowed @RaoPatek. I will recommend you to read the Forum Rules.