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[]){ boolean keepGoing = true; 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("Please guess a number between 1 and 10"); int guessInt = scan.nextInt(); System.out.println(" ");//blank line int genrand = r.nextInt(10) + 1; int turns = 2; while(keepGoing==true){ 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"); keepGoing=false; }else{ System.out.println(" "); System.out.print("Sorry, your guess was wrong. Guess again! You have" + turns +" more guesses"); //if not, goes to play again scanner if(turns==0){ keepGoing=false; }else{ System.out.println(" Guess another number"); guessInt = scan.nextInt(); } } turns--; } System.out.println("The random number was " + genrand); System.out.println(" Would you like to continue playing, press 1. Press 2 to quit"); 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.