Hey there....I have a simple problem...well I think it might be a logical error but I don't know...I made a simple number guessing game in Java the other day but I want to improve it a little more...I want the person to be able to play the game again without having to compile the and run the program again!
Anyway...here is my simple code:
See...the code prints out Play Again?(Y/N) but it doesn't allow input for some reason...!Code:import java.util.Scanner; import java.util.Random; public class Guessing { public static void main(String[] args) { final int MAX = 10; int guess, answer; String another = "y"; Scanner scan = new Scanner(System.in); Random generator = new Random(); answer = generator.nextInt(MAX) + 1; System.out.print ( "I'm thinking of a number between 1 and " + MAX + ". Guess what it is: " ); guess = scan.nextInt(); while(another.equalsIgnoreCase("y")) { if (guess == answer){ System.out.println ( "You got it correct! Good guessing!" ); }else{ System.out.println ( "Sorry, but that is incorrect." ); System.out.println ( "The correct number was " + answer + "." ); } System.out.println(); System.out.print ( "Play Again?(Y/N)" ); another = scan.nextLine(); } } }


LinkBack URL
About LinkBacks






Reply With Quote