Results 1 to 6 of 6
Thread: Looping: Help Please
- 10-12-2011, 02:13 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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);
{
}
}
}
- 10-12-2011, 09:13 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Looping: Help Please
You can use a call to System.exit(); to close your program.
- 10-12-2011, 10:13 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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 #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 10-12-2011, 11:22 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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 #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Similar Threads
-
LOOPing for Biggner
By LogicBrix in forum New To JavaReplies: 2Last Post: 10-08-2011, 06:09 PM -
Looping
By Dean29126 in forum New To JavaReplies: 3Last Post: 09-08-2010, 02:01 PM -
Advanced Looping
By nuggetman4 in forum New To JavaReplies: 1Last Post: 05-10-2010, 06:54 AM -
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
Looping Help Please
By JonnySnip3r in forum New To JavaReplies: 5Last Post: 01-31-2010, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks