1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /**
- * Created by iyasuwatts on 10/17/17.
- */
-
- import java.util.Scanner;
- import java.util.Random;
-
- public class Main {
-
- public static void main(String[] args) {
-
- // Create random number from range of 0 to 100.
- Random rand = new Random();
- int mysteryNum = rand.nextInt(101);
-
- // Log number of user inputs.
- int userAttempts = 1;
-
- int userGuess;
- int prevGuess = 101;
-
- // Continue looping infinitely until correct answer is passed. Count each attempt.
- do {
- for (; ; userAttempts++) {
- Scanner input = new Scanner(System.in);
-
- System.out.println("What positive integer between 0 and 100 am I thinking of?");
- userGuess = input.nextInt();
-
- System.out.println("You guessed: " + userGuess);
-
- // Check what the mystery number is. (personal use for testing)
- // System.out.println(mysteryNum);
-
- // Empty variable for storing the previous guess to compare.
-
-
- // Conditionals (correct, too high, too low, same guess, out of bounds)
-
- if (mysteryNum == userGuess) {
- System.out.println("Correct! Number of attempts: " + userAttempts);
- break;
- } else if (userGuess > 100 || userGuess < 0) {
- System.out.println("Is that number between 0 and 100?");
- } else if (prevGuess == userGuess) {
- System.out.println("That's the same number!");
- userAttempts -= 1;
- } else if (mysteryNum > userGuess) {
- System.out.println("Too low! Try again!");
- } else if (mysteryNum < userGuess) {
- System.out.println("Too high! Try again!");
- }
-
- // Save previous guess for comparison.
- prevGuess = userGuess;
- }
-
- } while (mysteryNum != userGuess);
-
- }
- }
|