High Low Game in Java Play Again
#1
How to add play again choice to Hi Lo game
Posted 17 June 2014 - 01:07 PM
I am a beginner programmer. My chore is to write a plan that plays the "Hullo Lo guessing game." The programme runs smoothly. Nevertheless, it does not keep to the next game afterward the previous game is washed. I need to add a "play again" selection to the programme so that the user volition proceed to play until they have chosen to quit. I have tried several different ways but each 1 has failed. I am completely stuck on this. I have fastened my code to this. Assist is profoundly appreciated.
#2
Re: How to add play once again choice to Hi Lo game
Posted 17 June 2014 - 01:11 PM
Your code isn't included. Post it here, between code-tags.
#3
Re: How to add play again choice to How-do-you-do Lo game
Posted 17 June 2014 - 01:11 PM
I apologize if my code does not show up. I'm new to this site and I'k not certain how to postal service code correctly.
this is my code:
import java.util.Random;
import coffee.util.Scanner;
public form HiLo
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess;
int numberOfTries = 0;
Scanner Keyboard = new Scanner(System.in);
{
System.out.impress("Guess a number from ane to "
+ MAX + " (or enter 0 at whatever time to quit): ");
guess = Keyboard.nextInt();
}
Random generator = new Random();
answer = generator.nextInt(MAX) +i;
while (guess != answer && guess != 0)
{
if (gauge > answer && guess != 0)
{
Organisation.out.print("Too high! Guess again:");
judge = Keyboard.nextInt();
}
if (guess < answer && estimate != 0)
{
System.out.print("Too Low! Guess once again:");
estimate = Keyboard.nextInt();
}
if (gauge == 0)
{
System.out.print("You gave up subsequently " + " " + numberOfTries + " " + "guesses.");
System.out.print("The secret number was " + reply);
guess = Keyboard.nextInt();
}
numberOfTries=numberOfTries+ane;
}
if (approximate == respond)
{
System.out.print("Right!");
System.out.println("It took y'all " + " " + numberOfTries + " " + "guesses");
Organisation.out.println();
System.out.impress( "Play again(Y/N)?");
}
}
}
#iv
Re: How to add play again option to Hullo Lo game
Posted 17 June 2014 - 01:17 PM
This screencast will show you the way:
http://screenr.com/w9TN
#5
Re: How to add play again option to Hi Lo game
Posted 17 June 2014 - 01:20 PM
import java.util.Random; import java.util.Scanner; public class HiLo { public static void main (String[] args) { final int MAX = 100; int answer, gauge; int numberOfTries = 0; Scanner Keyboard = new Scanner(System.in); { Arrangement.out.print("Approximate a number from 1 to " + MAX + " (or enter 0 at any time to quit): "); guess = Keyboard.nextInt(); } Random generator = new Random(); answer = generator.nextInt(MAX) +1; while (guess != answer && approximate != 0) { if (guess > respond && guess != 0) { System.out.impress("Too high! Guess over again:"); guess = Keyboard.nextInt(); } if (approximate < answer && judge != 0) { Arrangement.out.impress("Too Depression! Guess again:"); guess = Keyboard.nextInt(); } if (gauge == 0) { System.out.print("You gave up subsequently " + " " + numberOfTries + " " + "guesses."); Organization.out.print("The secret number was " + answer); guess = Keyboard.nextInt(); } numberOfTries=numberOfTries+1; } if (guess == reply) { System.out.print("Correct!"); Organisation.out.println("It took you lot " + " " + numberOfTries + " " + "guesses"); Arrangement.out.println(); System.out.print( "Play over again(Y/N)?"); } } }
#6
Re: How to add play once more choice to Hi Lo game
Posted 17 June 2014 - 01:33 PM
System.out.print( "Play again(Y/N)?");
You currently don't exercise anything afterwards this line.
The whole section that needs to repeat might be in a practice-while loop. You could read a grapheme (at the lesser of the loop) and check this graphic symbol to make up one's mind whether to go again:
do { // read a character, stored as 'again' } while (again == 'y' || again == 'Y');
Y'all might read a string, rather than a graphic symbol. There are a few approaches.
This mail has been edited by andrewsw: 17 June 2014 - 01:35 PM
#7
Re: How to add play once again selection to Hi Lo game
Posted 17 June 2014 - 02:45 PM
I'm non sure where i would add together that within the code.
#8
Re: How to add play again selection to Hi Lo game
Posted 17 June 2014 - 02:56 PM
Effectually the whole department that needs to repeat.
#9
Re: How to add together play again choice to Hello Lo game
Posted 17 June 2014 - 04:41 PM
Cheers for the help. it was greatly needed. unfortunately, the instructor said to get rid of the line where it asks to play again. It should automatically proceed to the next game after the user has guessed the correct lawmaking. I made some revisions to the code only now i am stuck once again trying to figure out what needs to be added in club for the code to proceed after the correct guess. Again, thanks for your help.
import coffee.util.Random; import java.util.Scanner; public grade HiLo { public static void main (Cord[] args) { final int MAX = 100; int answer, guess; int numberOfGuesses = 0; Scanner Keyboard = new Scanner(System.in); { Organization.out.impress("Guess a number from 1 to " + MAX + " (or enter 0 at whatsoever time to quit): "); guess = Keyboard.nextInt(); } Random generator = new Random(); reply = generator.nextInt(MAX) +1; while (guess != respond && guess != 0) { if (approximate > answer && gauge != 0) { Arrangement.out.print("Too high! Approximate once again:"); guess = Keyboard.nextInt(); } if (estimate < answer && guess != 0) { System.out.print("Too Depression! Guess over again:"); guess = Keyboard.nextInt(); } if (gauge == 0) { System.out.print("You lot gave upwardly after " + " " + numberOfGuesses + " " + "guesses."); System.out.print("The surreptitious number was " + answer); judge = Keyboard.nextInt(); } numberOfGuesses=numberOfGuesses+1; } if (guess == answer) { Arrangement.out.print("Correct!"); System.out.println("It took you lot " + " " + numberOfGuesses + " " + "guesses"); } } }
#10
Re: How to add play again choice to How-do-you-do Lo game
Posted 22 June 2014 - 07:05 AM
Alex, these gentlemen aren't here to complete your homework for yous. Andrew's solution will work regardless whether you get user input or not.
do { // This code volition run equally long as the condition is Truthful } while (TRUE);
You lot could also wrap everything yous want to loop in a while(True) loop.
This mail service has been edited past cilaes: 22 June 2014 - 07:05 AM
banvarddonentolon.blogspot.com
Source: https://www.dreamincode.net/forums/topic/348846-how-to-add-play-again-choice-to-hi-lo-game/