High Low Java Program Asks to Play Again
#1
How to add play once again choice to How-do-you-do Lo game
Posted 17 June 2014 - 01:07 PM
I am a beginner programmer. My job is to write a program that plays the "Hi Lo guessing game." The program runs smoothly. However, it does not proceed to the next game afterward the previous game is washed. I demand to add a "play again" choice to the program so that the user will continue to play until they have chosen to quit. I have tried several different ways simply each one has failed. I am completely stuck on this. I accept attached my code to this. Assistance is profoundly appreciated.
#2
Re: How to add together play again pick to Hi Lo game
Posted 17 June 2014 - 01:11 PM
Your code isn't included. Postal service information technology here, betwixt code-tags.
#three
Re: How to add play again choice to Hi Lo game
Posted 17 June 2014 - 01:11 PM
I apologize if my lawmaking does not testify up. I'g new to this site and I'g non sure how to postal service lawmaking correctly.
this is my lawmaking:
import coffee.util.Random;
import java.util.Scanner;
public class HiLo
{
public static void principal (String[] args)
{
concluding int MAX = 100;
int answer, approximate;
int numberOfTries = 0;
Scanner Keyboard = new Scanner(System.in);
{
Arrangement.out.impress("Guess a number from 1 to "
+ MAX + " (or enter 0 at any time to quit): ");
gauge = Keyboard.nextInt();
}
Random generator = new Random();
reply = generator.nextInt(MAX) +i;
while (guess != answer && estimate != 0)
{
if (judge > respond && guess != 0)
{
System.out.impress("Too high! Guess again:");
guess = Keyboard.nextInt();
}
if (approximate < answer && approximate != 0)
{
System.out.print("Too Low! Guess over again:");
guess = Keyboard.nextInt();
}
if (guess == 0)
{
System.out.impress("You gave up afterwards " + " " + numberOfTries + " " + "guesses.");
Organisation.out.impress("The secret number was " + answer);
guess = Keyboard.nextInt();
}
numberOfTries=numberOfTries+ane;
}
if (guess == answer)
{
System.out.print("Correct!");
Organisation.out.println("Information technology took you " + " " + numberOfTries + " " + "guesses");
System.out.println();
System.out.print( "Play once again(Y/N)?");
}
}
}
#four
Re: How to add play once more option to Howdy Lo game
Posted 17 June 2014 - 01:17 PM
This screencast will show you the style:
http://screenr.com/w9TN
#5
Re: How to add play again choice to Hi Lo game
Posted 17 June 2014 - 01:20 PM
import java.util.Random; import coffee.util.Scanner; public form HiLo { public static void chief (String[] args) { concluding int MAX = 100; int answer, estimate; int numberOfTries = 0; Scanner Keyboard = new Scanner(System.in); { Organization.out.impress("Gauge 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 && gauge != 0) { if (gauge > answer && gauge != 0) { System.out.impress("Likewise high! Guess again:"); guess = Keyboard.nextInt(); } if (guess < answer && judge != 0) { Arrangement.out.impress("Too Depression! Approximate over again:"); guess = Keyboard.nextInt(); } if (guess == 0) { Organization.out.print("You gave upwardly after " + " " + numberOfTries + " " + "guesses."); Organisation.out.print("The secret number was " + respond); guess = Keyboard.nextInt(); } numberOfTries=numberOfTries+1; } if (guess == answer) { System.out.print("Correct!"); System.out.println("Information technology took you " + " " + numberOfTries + " " + "guesses"); System.out.println(); System.out.print( "Play again(Y/N)?"); } } } #half-dozen
Re: How to add play once more choice to Hi Lo game
Posted 17 June 2014 - 01:33 PM
Organization.out.print( "Play over again(Y/Due north)?");
Yous currently don't exercise anything after this line.
The whole section that needs to repeat might be in a do-while loop. You could read a character (at the bottom of the loop) and cheque this grapheme to decide whether to become again:
do { // read a character, stored as 'over again' } while (again == 'y' || again == 'Y'); Y'all might read a string, rather than a graphic symbol. There are a few approaches.
This mail service has been edited by andrewsw: 17 June 2014 - 01:35 PM
#seven
Re: How to add play again choice to Howdy Lo game
Posted 17 June 2014 - 02:45 PM
I'm non sure where i would add that within the code.
#eight
Re: How to add play once more selection to Howdy Lo game
Posted 17 June 2014 - 02:56 PM
Effectually the whole section that needs to repeat.
#ix
Re: How to add play again pick to Hi Lo game
Posted 17 June 2014 - 04:41 PM
Thank you for the help. it was greatly needed. unfortunately, the instructor said to get rid of the line where it asks to play again. Information technology should automatically continue to the adjacent game after the user has guessed the correct code. I made some revisions to the code but now i am stuck over again trying to figure out what needs to be added in guild for the code to proceed later the right guess. Again, thanks for your assistance.
import java.util.Random; import coffee.util.Scanner; public course HiLo { public static void main (Cord[] args) { final int MAX = 100; int reply, guess; int numberOfGuesses = 0; Scanner Keyboard = new Scanner(Organisation.in); { Arrangement.out.print("Guess a number from 1 to " + MAX + " (or enter 0 at any time to quit): "); estimate = Keyboard.nextInt(); } Random generator = new Random(); answer = generator.nextInt(MAX) +1; while (guess != answer && approximate != 0) { if (guess > answer && judge != 0) { Organization.out.impress("Also high! Approximate over again:"); guess = Keyboard.nextInt(); } if (guess < answer && guess != 0) { Organisation.out.print("Too Low! Guess once again:"); estimate = Keyboard.nextInt(); } if (gauge == 0) { System.out.print("You gave upwardly after " + " " + numberOfGuesses + " " + "guesses."); System.out.print("The secret number was " + respond); guess = Keyboard.nextInt(); } numberOfGuesses=numberOfGuesses+1; } if (gauge == answer) { System.out.print("Correct!"); System.out.println("Information technology took you lot " + " " + numberOfGuesses + " " + "guesses"); } } } #10
Re: How to add play again option to Hi Lo game
Posted 22 June 2014 - 07:05 AM
Alex, these gentlemen aren't here to complete your homework for you lot. Andrew'south solution will work regardless whether you lot get user input or not.
practice { // This lawmaking will run equally long as the condition is Truthful } while (True); Yous could likewise wrap everything you want to loop in a while(Truthful) loop.
This mail has been edited by cilaes: 22 June 2014 - 07:05 AM
lambertprighorky1989.blogspot.com
Source: https://www.dreamincode.net/forums/topic/348846-how-to-add-play-again-choice-to-hi-lo-game/
0 Response to "High Low Java Program Asks to Play Again"
Post a Comment