Advertisement
MrDoyle

While) Number Guessing with Counter

Nov 13th, 2020
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 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 worst 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(11);
  14.             if(rand !=0) break;
  15.         }
  16.         int entry = 0;
  17.         int NumberOfAttempts = 0;
  18.  
  19.         while ( entry != rand) {
  20.             System.out.println("You have guessed " + NumberOfAttempts + " times");
  21.             System.out.println("I´´ thinking of a number from 1-10. Try to guess!");
  22.             entry = keyboard.nextInt();
  23.  
  24.             if (entry == rand) {
  25.                 System.out.println("LOL!! U got it, I can´t believe you guessed it was " + rand +". It only took you " + (NumberOfAttempts + 1) + " tries");
  26.             } else {
  27.                 System.out.println("That is incorrect, please try again!");
  28.             }
  29.             NumberOfAttempts = NumberOfAttempts + 1;
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement