Advertisement
filipparodriguezz

Untitled

Dec 9th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 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.         Scanner keyboard = new Scanner(System.in);
  9.         Random Random = new Random();
  10.         int rand = 0;
  11.         while (true) {
  12.             rand = Random.nextInt(100);
  13.             if (rand != 0) break;
  14.         }
  15.         int guess = 0;
  16.         int Attempts = 0;
  17.         int Maximum = 7;
  18.  
  19.  
  20.         System.out.println("I am thinking of a number between 1 to 100. Try to guess it!");
  21.  
  22.         while (guess != rand && Attempts < Maximum) {
  23.  
  24.  
  25.             System.out.println("You have guessed " + (Attempts) + " times.");
  26.             guess = keyboard.nextInt();
  27.  
  28.             if (guess < rand) {
  29.                 System.out.println("You guessed too low.");
  30.             } else if (guess > rand) {
  31.                 System.out.println("You guessed too high.");
  32.             } else {
  33.                 int sum = Attempts + 1;
  34.                 System.out.println("You guessed correctly, with " + (sum) + " tries.");
  35.             }
  36.             Attempts = Attempts + 1;
  37.             if (Attempts != (Maximum)) {
  38.                 System.out.println("Try again.");
  39.             }
  40.  
  41.  
  42.         }
  43.  
  44.         if (Attempts == Maximum) ;
  45.         System.out.println("Sorry, you used up all your tries, with " + Attempts + " tries");
  46.     }
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement