Guessing Game

/** * User guesses a number 3 times. * * Sejin Park * 09/08/17 */ import java.util.*; public class guessingGame { public static void main(String args[]){ boolean playAgain = true; while(playAgain){ int max = 10; int min = 1; int randomNum = (int)(Math.random() * ((max - min) + 1)) + min; //inclusive int counter = 0; Scanner scan = new Scanner(System.in); Scanner sc = new Scanner(System.in); System.out.println("This is the one and only guessing game"); while(counter<3){ System.out.println("Please guess a number between 1 and 10"); int guessInt = scan.nextInt(); if(guessInt == randomNum){ System.out.println("Winner!"); counter = 4; } if(guessInt != randomNum){ counter ++; if(counter>2){ System.out.println("You lose!"); }else{ System.out.println("Guess Again!"); } } }//end of guessing loop System.out.println("The Random Number was: " + randomNum); System.out.println("Play again?"); String playGame = sc.nextLine(); if(playGame.equals("no")){ playAgain = false; } }//end of playGame } }

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.