Guessing Game

/** * Charlotte Weisman * B Block CompSci * Sep 12, 2017 * Guessing Game */ import java.util.*; public class guessingGame { public static void main(String args[]){ Scanner sc = new Scanner(System.in); Random rand = new Random(); int rnd = rand.nextInt(10) + 1; boolean keepGoing = true; while(keepGoing) { System.out.println("Please guess a number between 1 and 10:"); int firstGuess = sc.nextInt(); if (firstGuess==rnd){ System.out.println("Your guess:" + firstGuess + " My number:" + rnd); System.out.println("You win!"); }else{ System.out.println("Sorry, wrong. Guess again:"); int secondGuess = sc.nextInt(); if (secondGuess==rnd){ System.out.println("Your guess:" + secondGuess + " My number:" + rnd); System.out.println("You win!"); }else{ System.out.println("Sorry, wrong. Guess again:"); int thirdGuess = sc.nextInt(); if (thirdGuess==rnd){ System.out.println("Your guess:" + thirdGuess + "My number:" + rnd); System.out.println("You win!"); }else{ System.out.println("Sorry, wrong. The number was:" + rnd + " You lose!"); } } }System.out.println("Press 1 to play again. Press any other number to quit."); int cont = sc.nextInt(); if(cont !=1){ keepGoing = false; } } }}
Guessing Game for AP Comp Sci

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.