chaibs

loops-hw8

Dec 27th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class hw8 {
  4.  
  5.     public static void main(String[] args) {
  6.         int rn = 0, num = 0, count = 0;
  7.  
  8.         Scanner s = new Scanner(System.in);
  9.  
  10.         rn = ((int) (Math.random() * 100) + 1);// random number between 1-100
  11.         // System.out.println(rn);
  12.  
  13.         do {
  14.             System.out.println("Guess the random number:");
  15.             num = s.nextInt();// users guess
  16.             if (num > rn) {// checking if users guess is higher then the random number
  17.                 System.out.println("The number you entered is higher");
  18.             } else {
  19.                 System.out.println("The number you entered is lower");
  20.             }
  21.             count += 1;// counting the number of trying
  22.         } while (num != rn);// as long as the user never guessed the number
  23.  
  24.         System.out.println("Correct! Number of tries is " + count);// result
  25.     }
  26.  
  27. }
Add Comment
Please, Sign In to add comment