12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class Main {
  4. public static void main(String[] args){
  5. Random ran = new Random();
  6. Scanner input = new Scanner(System.in);
  7. int value = ran.nextInt(10);
  8. int count = 0;
  9. int guess = 0;
  10. while(true){
  11. System.out.println("Please enter a number between 1 to 10 inclusive:");
  12. guess = input.nextInt();
  13. count++;
  14. if(guess < 1 || guess > 10){
  15. System.out.println("Invalid input");
  16. }
  17. else if(guess < value){
  18. System.out.println("Too small");
  19. System.out.println("Please guess a number again between 1 to 10");
  20. }
  21. else if(guess > value){
  22. System.out.println("Too large");
  23. System.out.println("Please guess a number again between 1 to 10");
  24. }
  25. else if(guess == value){
  26. System.out.println("correct guess!");
  27. System.out.println("You guessed the ans in " + count + " attempts!");
  28. break;
  29. }
  30. }
  31. }
  32. }