Main.java 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.Scanner;
  5. public class Main{
  6. public static void main(String[] args){
  7. Scanner stdin = new Scanner(System.in);
  8. int num = 21, usrNum, sum = 0;
  9. boolean isValid = false;
  10. boolean isTrue = false;
  11. while(!isValid){
  12. System.out.println("Enter a number (between 0 and 50) to guess the mystery number (-1 to exit): ");
  13. usrNum = stdin.nextInt();
  14. if(usrNum == -1){
  15. break;
  16. }
  17. while(!isTrue){
  18. if(usrNum>=0 && usrNum<=50){
  19. isTrue = true;
  20. } else if(usrNum == -1){
  21. break;
  22. } else{
  23. System.out.println("Number out of range\n");
  24. System.out.println("Enter a number (between 0 and 50) to guess the mystery number (-1 to exit): ");
  25. usrNum = stdin.nextInt();
  26. }
  27. }
  28. if(usrNum == -1){
  29. break;
  30. }
  31. if(usrNum<num){
  32. System.out.println("Incorrect... number is to small\n");
  33. } else if(usrNum>num){
  34. System.out.println("Incorrect... number is to large\n");
  35. } else if(usrNum == num){
  36. System.out.println("Correct! The number was " + num);
  37. isValid = true;
  38. }
  39. sum++;
  40. }
  41. System.out.println("Total number of tries is: " + sum);
  42. }
  43. }