Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: School Assignment AHH!

  1. #1
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question 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!" );
    System.out.print( "Your Guess:" );
    int guess = keyboard.nextInt();

    do
    {
    System.out.println();
    System.out.println( "Haha! WRONG! Try again" );
    System.out.print( "Your Guess:" );
    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!" );
    }
    }
    }
    I don't know what your problem is, but I bet it's hard to pronounce.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. The Following User Says Thank You to copeg For This Useful Post:

    Europa (December 15th, 2011)

  4. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: School Assignment AHH!

    Probably not a bad assumption.. just saying

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: School Assignment AHH!

    Try a while loop instead of a do while loop.

  6. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: School Assignment AHH!

    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.

  7. #6
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  9. #8
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: School Assignment AHH!

    ...

    Try this out may it Helps...
    Last edited by copeg; January 20th, 2012 at 09:46 AM.

  10. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: School Assignment AHH!

    Quote Originally Posted by RaoPatek View Post
    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" );
    System.out.print( "Your Guess:" );
    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.

Similar Threads

  1. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 09:36 PM
  2. school project
    By robin28 in forum Java Theory & Questions
    Replies: 13
    Last Post: November 12th, 2010, 09:11 AM
  3. School Help
    By JavaNewb in forum Loops & Control Statements
    Replies: 1
    Last Post: March 24th, 2010, 09:31 PM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM

Tags for this Thread