12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. public class Main {
  7. public static void game() {
  8. Random rand = new Random();
  9. Scanner scan = new Scanner(System.in);
  10. // int answer = rand.nextInt(11);
  11. int answer = 2;
  12. String prevGuess = "-1337";
  13. int guessCount = 1;
  14. System.out.println("Guess my number between 0 - 10.");
  15. String guess = scan.nextLine();
  16. while (Integer.parseInt(guess) != answer) {
  17. if (!guess.equals(prevGuess)) guessCount++;
  18. String output =
  19. Integer.parseInt(guess) == Integer.parseInt(prevGuess)
  20. ? "That was the same guess as before... try again: "
  21. : Integer.parseInt(guess) < 0 || Integer.parseInt(guess) > 10
  22. ? "It's between 0-10. Try again: "
  23. : Integer.parseInt(guess) == answer
  24. ? "reset"
  25. : Integer.parseInt(guess) > answer
  26. ? "Too much. Try again: "
  27. : Integer.parseInt(guess) < answer
  28. ? "Too little. Try again: "
  29. : null;
  30. System.out.println(output);
  31. prevGuess = guess;
  32. guess = scan.nextLine();
  33. }
  34. System.out.println("Total guess count: " + guessCount);
  35. System.out.println("You won! Would you like to play again? (Y/N)");
  36. String playAgain = scan.nextLine();
  37. if (playAgain.toLowerCase().equals("y")) game();
  38. System.out.println("Now shutting down.");
  39. }
  40. public static void main(String[] args){
  41. game();
  42. }
  43. }