Guessing Game

/** * Write a description of class guessingGame here. * * @David Huang (your name) * @1.0 9/11 */ import java.util.*; public class guessingGame { public static void main(String args[]){ while (true){ //random Scanner scan = new Scanner(System.in);//sets up scanner Random r = new Random(); //sets up random integer System.out.println("This is the one and only guessing game"); System.out.println(" ");//blank line System.out.println("Please guess a number between 1 and 10"); int guessInt = scan.nextInt();//sets up guessing integer int genrand = r.nextInt(10) + 1;//sets up random integer if (guessInt == genrand){//if the guess equals the random number, this line plays System.out.println(" "); System.out.println("Congratulations! Your guess was correct, winner! Press 1 to play again, or 2 to quit"); }else{ System.out.println(" "); System.out.print("Sorry, your guess was wrong. Press 1 to play again, or 2 to quit");//if not, goes to play again scanner System.out.println(" "); int last = scan.nextInt(); if (last != 1) {//breaks the loop if user presses anything but 1 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.