GuessingGame.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Write a description of class Main here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.Scanner;
  8. import java.util.Random;
  9. public class GuessingGame{
  10. public static void main(String[] args) {
  11. System.out.println("GUESSING GAME");
  12. Random rand = new Random();
  13. int answer = rand.nextInt(9) + 1;
  14. int numGuesses = 0;
  15. int userGuess = 0;
  16. int lastGuess = 0;
  17. boolean[] guessArray = new boolean[10];
  18. for (int i = 0; i < 10; i++){
  19. guessArray[i] = false;
  20. //System.out.println(guessArray[i]);
  21. };
  22. while (userGuess != answer){
  23. Scanner scan = new Scanner(System.in);
  24. System.out.println("Guess a number from 0 to 9:");
  25. userGuess = scan.nextInt();
  26. if (userGuess >= 0 && userGuess < 10){
  27. for (int i = 0; i < 10; i++){
  28. if(guessArray[userGuess] == false){
  29. guessArray[userGuess] = true;
  30. numGuesses++;
  31. }
  32. //Prints all boolean values of array
  33. //System.out.println(guessArray[i]);
  34. };
  35. if (userGuess == answer) {
  36. System.out.println("You guessed the mystery number " + answer + " correct ");
  37. System.out.println("in " + numGuesses + " guesses!!");
  38. } else {
  39. if (userGuess < answer){
  40. System.out.println("too small");
  41. } else {
  42. System.out.println("too large");
  43. };
  44. //Testing
  45. //System.out.println("userGuess is: " + userGuess);
  46. //System.out.println("lastGuess is: " + lastGuess);
  47. //System.out.println("numGuesses is: " + numGuesses);
  48. }
  49. lastGuess = userGuess;
  50. } else {
  51. System.out.println("You guessed out of the range! Guess again!!");
  52. }
  53. }
  54. }
  55. }