Advertisement
yoyo106

Random Number game

Mar 5th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MyFirstClass
  4. {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner read = new Scanner(System.in);
  9.         int randomNum = (int) (Math.random() * 100) + 1;
  10.         System.out.println("I have chosen a number between [1-100]");
  11.         System.out.println("Try to guess it and I'll help you do so!");
  12.         System.out.println("Unfortunately you have only 10 guesses, so good luck!");
  13.         int count=0;
  14.         int guess;
  15.         while (count<=10)
  16.         {
  17.             count++;
  18.             System.out.println("Enter number: ");
  19.             guess = read.nextInt();
  20.             if (guess == randomNum)
  21.             {
  22.                 System.out.println("YOU WON!");
  23.                 break;
  24.             }
  25.             else
  26.             {
  27.                 System.out.println("Wrong, you got "+(10-count)+" left!");
  28.                 if (guess>randomNum)
  29.                     System.out.println("Number smaller than "+guess);
  30.                 else
  31.                     System.out.println("Number greater than "+guess);
  32.             }
  33.         }
  34.         System.out.println("Good luck next try!");
  35.     }          
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement