Advertisement
Felanpro

Number guesser.

Sep 5th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.lang.Math;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class mainclass
  6. {  
  7.     public static void main(String[] args)
  8.     {
  9.         Random r = new Random();
  10.         Scanner s = new Scanner(System.in);
  11.        
  12.         int randNum = r.nextInt(101);
  13.         int guess;
  14.         int tries = 0;
  15.        
  16.         do
  17.         {
  18.             System.out.print("Guess the number between 1 - 100: ");
  19.             guess = s.nextInt();
  20.            
  21.             if(guess < randNum)
  22.             {
  23.                 System.out.println("Higher.");
  24.             }
  25.             else if(guess > randNum)
  26.             {
  27.                 System.out.println("Lower.");
  28.             }
  29.             else if(guess == randNum)
  30.             {
  31.                 System.out.printf("You guessed the right number! " + "It was " + randNum, ". " + "You made it in " + tries + " tries." );
  32.             }
  33.            
  34.             tries += 1;
  35.         }
  36.         while(guess != randNum);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement