guessing game

/** * Guess the computer's random number by typing in a number between 1 and 10. * * @author Adam Kovacs * @version 1.0 */ import java.util.*; public class guessingGame { public static void main(String args[]){ while (true) { //While loop allows the user to keep playing if they so choose int random; int userInt; //user int is the variable for the user to entr there guess //random is the random value picked by the program Random rand = new Random(); random = rand.nextInt(10) + 1; System.out.println("This is the one and only guessing game!"); int count = 1; while (count <= 3) { userInt = 0; Scanner scan = new Scanner(System.in); System.out.println("Please enter a number between 1 and 10: "); userInt = scan.nextInt(); //here is where the user enters the number between 1 an 10 if (userInt == random) { System.out.println("Winner! Congratulations!"); break; } else { System.out.println("Sorry, Wrong. Please try again."); count += 1; }//if the users number matches they win the game and can play again } System.out.println("The computer's number was " + random + "."); Scanner scanAgain = new Scanner(System.in); System.out.println("Would you like to play again? Type YES if so. "); String answer = scanAgain.nextLine(); if (!answer.equals("YES")) { break; } } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.