Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class hw8 {
- public static void main(String[] args) {
- int rn = 0, num = 0, count = 0;
- Scanner s = new Scanner(System.in);
- rn = ((int) (Math.random() * 100) + 1);// random number between 1-100
- // System.out.println(rn);
- do {
- System.out.println("Guess the random number:");
- num = s.nextInt();// users guess
- if (num > rn) {// checking if users guess is higher then the random number
- System.out.println("The number you entered is higher");
- } else {
- System.out.println("The number you entered is lower");
- }
- count += 1;// counting the number of trying
- } while (num != rn);// as long as the user never guessed the number
- System.out.println("Correct! Number of tries is " + count);// result
- }
- }
Add Comment
Please, Sign In to add comment