# Looping: Help Please

• 10-12-2011, 02:13 AM
michelle_323
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?");

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);
{
}
}
}
• 10-12-2011, 09:13 AM
jammas615
Re: Looping: Help Please
You can use a call to System.exit(); to close your program.
• 10-12-2011, 10:13 AM
michelle_323
Re: Looping: Help Please
I'm really new to this so I must ask, how would you add that into the code?
• 10-12-2011, 11:17 AM
JosAH
Re: Looping: Help Please
Quote:

Originally Posted by michelle_323
"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
• 10-12-2011, 11:22 AM
michelle_323
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.
• 10-12-2011, 11:59 AM
mine0926
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.