console.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import java.util.*;
  2. public class console
  3. {
  4. public static void main(String[] args){
  5. //Get random # and start counter of guesses
  6. int counter = 0;
  7. int max = 10;
  8. int min = 1;
  9. int range = max - min + 1;
  10. int number = 0;
  11. int guess = 0;
  12. for (int i = 0; i < 10; i++) {
  13. number = (int)(Math.random() * range) + min; }
  14. Scanner in = new Scanner(System.in);
  15. System.out.print("Take a guess between 1-10");
  16. while (in.hasNextInt() == true) {
  17. counter++;
  18. guess = in.nextInt();
  19. if (guess == number) {
  20. System.out.print("Correct Guess!");
  21. }
  22. else if (guess < number && guess <= 10 && guess != 0){
  23. System.out.print("Too Small. Try Again!");
  24. } else if (guess > number && guess <= 10){
  25. System.out.print("Too Large. Try Again!");
  26. } else {
  27. System.out.print("Oops! Please enter a number between 1 and 10");
  28. }; };
  29. if (in.hasNextInt() == false && guess != number) {
  30. int guesses = range - counter;
  31. System.out.print("Thanks for playing! You were " + guesses + " away from guessing the correct answer! Otherwise it would have taken at least 10 tries.");
  32. in.close();
  33. } else {
  34. System.out.print("Thanks for playing and congrats on guessing correct!");
  35. in.close();
  36. };};
  37. }