Advertisement
CamiloCastilla

Untitled

Dec 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner keyboard = new Scanner(System.in);
  8.         Random random = new Random();
  9.         int rand = 0;
  10.  
  11.             rand = random.nextInt(100);
  12.             System.out.println("I'm thinking of a number between 1-100.  You have 7 guesses.");
  13.  
  14.  
  15.             int max=8;
  16.             int guesses=0;
  17.             int attemps=1;
  18.  
  19.             while (guesses != rand && attemps<max) {
  20.                 System.out.println("Guess # " + attemps++ + ":");
  21.                 guesses = keyboard.nextInt();
  22.                 if (guesses == rand) {
  23.                     attemps++;
  24.                     System.out.println("You guessed correctly with" + attemps + "tries");
  25.                 } else if (guesses < rand) {
  26.                     System.out.println("Sorry, you are too low.");
  27.                 } else {
  28.                     System.out.println("Sorry, that guess is too high.");
  29.                 }
  30.  
  31.             }
  32.  
  33.             if (guesses != rand && attemps==max);
  34.                 System.out.println("Sorry, you didn't guess it in 7 tries.  You lose.");
  35.  
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement