Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner keyboard = new Scanner(System.in);
  8.  
  9.         Random random = new Random();
  10.  
  11.         int rand = 0;
  12.  
  13.         while (true){
  14.  
  15.             rand = random.nextInt(101);
  16.             if(rand != 0) break;
  17.         }
  18.  
  19.         int guess = 0;
  20.         int numberOfAttempts = 0;
  21.         int maxattempts = 7;
  22.  
  23.  
  24.         System.out.println("\n I'm thinking of a number between 1-100.  You have 7 guesses.");
  25.  
  26.  
  27.         do{
  28.  
  29.             System.out.println("\n Guess #"+(numberOfAttempts+1)+ ":");
  30.             guess = keyboard.nextInt();
  31.  
  32.             if(guess == rand){
  33.                 System.out.println("\n You guess correct! You are a genius");
  34.                 System.out.println("\n You guess with "+numberOfAttempts+ " attempts");
  35.             }
  36.  
  37.             if(numberOfAttempts == maxattempts){
  38.                 System.out.println("\n Sorry, you didn't guess it in 7 tries.  You lose.");
  39.             }
  40.  
  41.             if(guess > rand){
  42.                 System.out.println("\n Sorry, that guess is too high");
  43.             }
  44.             else if(guess < rand){
  45.                 System.out.println("\n Sorry, you are too low");
  46.             }
  47.  
  48.             numberOfAttempts = numberOfAttempts + 1;
  49.  
  50.         }while (guess != rand && numberOfAttempts < maxattempts);
  51.  
  52.  
  53.  
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement