Results 1 to 6 of 6
  1. #1
    michelle_323 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Question Looping: Help Please

    Hello, I'm new to Java and I was wondering how you could modify the code further by ending the program when EITHER the user successfully guesses OR chooses to end the game in response to a prompting question such as "Would you like to continue testing or stop playing?". If user stops playing print a consolation message, i.e. "Thanks for playing, please try again." Thank you for your help.

    /*
    * Guess the Number
    */

    import java.io.Console;
    import java.util.Random;

    public class numberprojectloop

    {

    public static void main(String[] args) {

    int computerNumber;
    int yourGuess;
    Random myRandom = new Random();
    int benchmark;
    int tryAgain;

    benchmark = 14;


    // get the computer's number between 1 and 10
    computerNumber = myRandom.nextInt(10) + 1;
    System.out.println("I'm thinking of a number between 1 and 10.");


    do
    {
    // get your guess
    yourGuess = Typeit.inInt("What do you think it is?");

    // get clues and tips to help your reach the answer
    System.out.println("My number is "+(Math.abs(benchmark-computerNumber)+" less than 15"));
    if (yourGuess == computerNumber)
    {
    System.out.println("You got it!! That's my number!");
    }
    else if (Math.abs(yourGuess - computerNumber) <= 1)
    {
    System.out.println("Ouch !! You're burning up!! You're one number away!!");
    }
    else if (Math.abs(yourGuess - computerNumber) <= 2)
    {
    System.out.println("You're hot!! You're two numbers away!!");
    }
    else if (Math.abs(yourGuess - computerNumber) <= 3)
    {
    System.out.println("You're warm!! You're three numbers away!! ");
    }
    else
    {
    System.out.println("Brr!! You're freezing!! You're more than three numbers away!!");
    }
    System.out.println("Would you like to try again?");
    }

    //keeps the loop running until you get the correct answer
    while (yourGuess != computerNumber);
    {
    }
    }
    }

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Looping: Help Please

    You can use a call to System.exit(); to close your program.

  3. #3
    michelle_323 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Looping: Help Please

    I'm really new to this so I must ask, how would you add that into the code?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Looping: Help Please

    Quote Originally Posted by michelle_323 View Post
    "Would you like to continue testing or stop playing?".
    How can the user answer that question and what is the program to do if the answer is "yes" (or "no")?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    michelle_323 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Looping: Help Please

    I'm not sure how to answer you question but the applet is run on eclipse where the user can input numbers and answers. I have yet to add in the code as I'm uncertain how to 'phrase' it. If the user answers 'yes', the then program is to continue on a 'loop' whereas if the use answers 'no', then the program stops. I'm sorry that my response is badly worded but I hope that helps.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Looping: Help Please

    You can declare boolean, by default the value of that boolean is false. So while that boolean is false do loop. You set boolean to true ONLY IF user answer right OR IF user do not want to try again.

    Hope that helps.
    Last edited by mine0926; 10-12-2011 at 12:02 PM.

Similar Threads

  1. LOOPing for Biggner
    By LogicBrix in forum New To Java
    Replies: 2
    Last Post: 10-08-2011, 06:09 PM
  2. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  3. Advanced Looping
    By nuggetman4 in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 06:54 AM
  4. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  5. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •