Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Task_8 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Random rand = new Random();
- System.out.println("How big should the range of the number be?");
- int range = scan.nextInt();
- int tries = 0;
- int num = rand.nextInt(range) + 1;
- int guess;
- do {
- System.out.println("Guess a number: ");
- guess = scan.nextInt();
- tries++;
- if (guess < 1 || guess > range){
- System.out.println("Error! Number is out of range!");
- } else if (guess < num){
- System.out.println("The number is bigger than " + guess);
- } else if (guess > num) {
- System.out.println("The number is smaller than " + guess);
- }
- } while (guess != num);
- System.out.println("Congratulations, you won with only " + tries + " tries!!!\nThe number was " + num + " !!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement