123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
-
- /**
- * Write a description of class Main here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
-
- import java.util.Scanner;
- import java.util.Random;
-
- public class GuessingGame{
-
- public static void main(String[] args) {
-
- System.out.println("GUESSING GAME");
-
- Random rand = new Random();
- int answer = rand.nextInt(9) + 1;
- int numGuesses = 0;
- int userGuess = 0;
- int lastGuess = 0;
-
- boolean[] guessArray = new boolean[10];
-
- for (int i = 0; i < 10; i++){
- guessArray[i] = false;
- //System.out.println(guessArray[i]);
- };
-
- while (userGuess != answer){
- Scanner scan = new Scanner(System.in);
- System.out.println("Guess a number from 0 to 9:");
- userGuess = scan.nextInt();
-
- if (userGuess >= 0 && userGuess < 10){
-
- for (int i = 0; i < 10; i++){
- if(guessArray[userGuess] == false){
-
- guessArray[userGuess] = true;
- numGuesses++;
-
- }
- //Prints all boolean values of array
- //System.out.println(guessArray[i]);
- };
-
-
- if (userGuess == answer) {
- System.out.println("You guessed the mystery number " + answer + " correct ");
- System.out.println("in " + numGuesses + " guesses!!");
- } else {
- if (userGuess < answer){
- System.out.println("too small");
- } else {
- System.out.println("too large");
- };
-
- //Testing
- //System.out.println("userGuess is: " + userGuess);
- //System.out.println("lastGuess is: " + lastGuess);
- //System.out.println("numGuesses is: " + numGuesses);
-
- }
-
- lastGuess = userGuess;
- } else {
- System.out.println("You guessed out of the range! Guess again!!");
- }
- }
- }
- }
-
|