12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- /**
- * Created by iyasuwatts on 10/17/17.
- */
-
- import java.util.Random;
- import java.util.Scanner;
-
- public class Game {
-
- public static void main(String[] args) {
- //creates random secret number 1-10
- Random number = new Random();
- int secretNumber = (number.nextInt(10) +1);
-
- //tracks number of guesses
- int numberOfGuesses = 0;
-
- //track previous guess
- int previousGuess = 0;
-
- //creates scanner for user input
- Scanner input = new Scanner(System.in);
-
- //variable to hold user guess
- int userGuess;
-
- //variable to hold win state
- boolean win = false;
-
- while (win == false) {
- //System asks for user input and stores user guess
- System.out.println("Guess a number between 1 and 10");
- userGuess = input.nextInt();
-
- //compares userGuess to secret number
- if (userGuess == secretNumber) {
- win = true;
- }
- else if (userGuess < secretNumber) {
- System.out.println("Too Low!");
- //increment guess counter if guess does not match
- //previous guess
- if(userGuess != previousGuess) {
- numberOfGuesses++;
- }
- previousGuess = userGuess;
- }
- else if (userGuess > secretNumber) {
- System.out.println("Too High!");
- if(userGuess != previousGuess) {
- numberOfGuesses++;
-
- }
- previousGuess = userGuess;
- }
-
- }
-
- numberOfGuesses++;
- System.out.println("You Win!");
- System.out.println("It took you " + numberOfGuesses + " tries");
- }
- }
|