Advertisement
Stelios_Gakis

Exercise_2/Task_8

Sep 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Task_8 {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         Random rand = new Random();
  8.         System.out.println("How big should the range of the number be?");
  9.         int range = scan.nextInt();
  10.         int tries = 0;
  11.  
  12.         int num = rand.nextInt(range) + 1;
  13.         int guess;
  14.  
  15.         do {
  16.             System.out.println("Guess a number: ");
  17.             guess = scan.nextInt();
  18.             tries++;
  19.  
  20.             if (guess < 1 || guess > range){
  21.                 System.out.println("Error! Number is out of range!");
  22.             }  else if (guess < num){
  23.                 System.out.println("The number is bigger than " + guess);
  24.             } else if (guess > num) {
  25.                 System.out.println("The number is smaller than " + guess);
  26.             }
  27.  
  28.         } while (guess != num);
  29.         System.out.println("Congratulations, you won with only " + tries + " tries!!!\nThe number was " + num + " !!!");
  30.  
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement