java - How to replay basic High Low game? -
i finished second java assignment class, stuck on how take user's last choice , either play again or quit. ideas?
import java.util.random; import java.util.scanner; public class highlow { public static void main(string[]args) { random randomnumber = new random(); int answer = randomnumber.nextint(100) + 1; int numberoftries = 0; scanner userinput = new scanner(system.in); string userguess = null; boolean gamewin = false; { while (!gamewin) { system.out.println("welcome guessing game, guess number between 1 - 100 or type 'quit' exit: "); userguess = userinput.nextline(); numberoftries++; if (userguess.equals("quit")) break; if (integer.parseint(userguess) == answer) { gamewin = true; } else if (integer.parseint(userguess) < answer) { system.out.println("your guess low"); } else if (integer.parseint(userguess) > answer) { system.out.println("your guess high"); } } if (userguess.equals("quit")) { system.out.println("you choose quit! playing"); system.out.println("the number " + answer); system.exit(0); } system.out.println("the number " + answer); system.out.println("it took " + numberoftries + " tries"); system.out.println("you win! play again(type: yes or no)"); } while (userguess.equals("yes")); userinput.nextline(); } } }
the problem because have userinput.nextline()
prompt user play again or not outside of game loop , isn't assigned value. referring section...
system.out.println("the number " + answer); system.out.println("it took " + numberoftries + " tries"); system.out.println("you win! play again(type: yes or no)"); } while (userguess.equals("yes")); userinput.nextline(); <--- }
to fix problem need assign final input call userguess
, place game loop so.
system.out.println("the number " + answer); system.out.println("it took " + numberoftries + " tries"); system.out.println("you win! play again(type: yes or no)"); userguess = userinput.nextline(); } while (userguess.equals("yes")); }
doing assign play again
value prompt user variable testing do-while
loop. in original, had condition testing value userguess
wasn't going storing , instead prompting user option after loop have been exited instead of inside needs be.
to reset values next game, can move variables' initialization game loop @ top so...
import java.util.random;
import java.util.scanner; public class highlow {
public static void main(string[]args) { random randomnumber = new random(); int numberoftries, answer; scanner userinput = new scanner(system.in); string userguess; boolean gamewin; { answer = randomnumber.nextint(100) + 1; gamewin = false; userguess = null; numberoftries = 0; while (!gamewin) { system.out.println("welcome guessing game, guess number between 1 - 100 or type 'quit' exit: "); userguess = userinput.nextline(); numberoftries++; if (userguess.equals("quit")) break; if (integer.parseint(userguess) == answer) { gamewin = true; } else if (integer.parseint(userguess) < answer) { system.out.println("your guess low"); } else if (integer.parseint(userguess) > answer) { system.out.println("your guess high"); } } if (userguess.equals("quit")) { system.out.println("you choose quit! playing"); system.out.println("the number " + answer); system.exit(0); } system.out.println("the number " + answer); system.out.println("it took " + numberoftries + " tries"); system.out.println("you win! play again(type: yes or no)"); userguess = userinput.nextline(); } while (userguess.equals("yes")); } } }
this way every time game loops through, values reset @ beginning of new game loop.
also should close random
object , scanner
object @ end of entire program using close()
on each instance.
Comments
Post a Comment