Main.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. public class Main {
  5. int randomNum;
  6. int counterTooBig = 0;
  7. int counterTooSmall = 0;
  8. int totalGuesses = 0;
  9. int prevGuess = 0;
  10. int currentGuess = 0;
  11. public static void main(String[] args){
  12. System.out.println("You have 5 chances to correctly guess a random number between 1 and 20.");
  13. Scanner input = new Scanner(System.in);
  14. Random random = new Random();
  15. randomNum = 1+random.nextInt(20);
  16. System.out.println("Enter your first guess now: ");
  17. for (int i = 1; i <= 5; i++){
  18. currentGuess = input.getNextInt();
  19. if (currentGuess == prevGuess) {
  20. i=i-1;
  21. System.out.println("Same as your last guess...Try Again")
  22. }
  23. if (currentGuess == randomNum) {
  24. System.out.println("*** Correct!!! ***");
  25. System.out.println("Total guesses to win: " + i);
  26. }else if (currentGuess > randomNum) {
  27. System.out.println("TOO BIG");
  28. System.out.println("Total guesses remaining: " + (5-i));
  29. } else if (currentGuess < randomNum) {
  30. System.out.println("too small");
  31. System.out.println("Total guesses remaining: " + (5-i));
  32. prevGuess = currentGuess;
  33. }
  34. }
  35. }
  36. }