Advertisement
MrDoyle

While) HI-Low Number Guessing

Nov 13th, 2020
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         System.out.println("The best number guessing game ever!!!! ");
  8.  
  9.         Scanner keyboard = new Scanner (System.in);
  10.         Random random = new Random();
  11.         int rand = 0;
  12.         while (true){
  13.             rand = random.nextInt(101);
  14.             if(rand !=0) break;
  15.         }
  16.         int entry = 0;
  17.         int numberOfAttempts = 0;
  18.         int maxAttempts = 7;
  19.  
  20.         while ( entry != rand && numberOfAttempts < maxAttempts) {
  21.             System.out.println("\nYou have guessed " + numberOfAttempts + " times");
  22.             System.out.println("I´´ thinking of a number from 1-100. Try to guess!");
  23.             entry = keyboard.nextInt();
  24.  
  25.             if (entry < rand) {
  26.                 System.out.println("You guessed too low");
  27.             } else if (entry > rand){
  28.                 System.out.println("You guessed too high");
  29.             } else {
  30.                 System.out.println("You guessed correctly, with " + (numberOfAttempts + 1) + " tries.");
  31.             }
  32.             numberOfAttempts = numberOfAttempts + 1;
  33.         }
  34.  
  35.         if (numberOfAttempts == maxAttempts){
  36.             System.out.println("\nSorry you used up all of your tries, with " + numberOfAttempts + " attempts.");
  37.         }
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement