Main.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("WELCOME! Guess a number between 1 and 49:");
  6. int inputtedNumber = in.nextInt();
  7. int previousInputNumber = 0;
  8. int randomNumber = (int) (Math.random()*50);
  9. int numberOfGuesses = 0;
  10. String tryOrTries = "";
  11. if (numberOfGuesses == 1){
  12. tryOrTries ="try!";
  13. }
  14. else {
  15. tryOrTries = "tries!";
  16. }
  17. while (true){
  18. if(inputtedNumber < 1 || inputtedNumber > 49){
  19. System.out.println("Guess a number between 1 and 49!");
  20. }
  21. else if(inputtedNumber < randomNumber && inputtedNumber > 0 && inputtedNumber < 50){
  22. if (inputtedNumber != previousInputNumber){
  23. numberOfGuesses = numberOfGuesses + 1;
  24. }
  25. System.out.println("TOO SMALL! Try again");
  26. }
  27. else if (inputtedNumber > randomNumber && inputtedNumber > 0 && inputtedNumber < 50){
  28. if (inputtedNumber != previousInputNumber){
  29. numberOfGuesses = numberOfGuesses + 1;
  30. }
  31. System.out.println("TOO LARGE! Try again!");
  32. }
  33. else if(inputtedNumber == randomNumber){
  34. if (inputtedNumber != previousInputNumber){
  35. numberOfGuesses = numberOfGuesses + 1;
  36. }
  37. System.out.println("CORRECT GUESS! You guessed the correct answer after " + numberOfGuesses + " "+ tryOrTries);
  38. break;
  39. }
  40. previousInputNumber = inputtedNumber;
  41. inputtedNumber = in.nextInt();
  42. }
  43. }
  44. }