Home > other >  How to check the previous numbers?
How to check the previous numbers?

Time:03-13

i would like to know how to solve my problem. I am stuck in the middle :(

the question is every time the user makes a guess, check the previous guesses. If they guess the same number again then output a message saying and no more extra turn. But totally I have no idea. how to create a single code.

public static void main(String[] args) {
    Random r = new Random();
    int num = r.nextInt(100)   1;
    int[] numbers = new int[99];
    System.out.println(num);
    Scanner myKB = new Scanner(System.in);
    Scanner mySc = new Scanner(System.in);
    for (int i = 0; i <= numbers.length; i  ) {
        int j = 10 - (i);
        System.out.println("Guess a number between 1 and 100. You have "   j   " times left ");
        numbers[i] = mySc.nextInt();
        if (numbers[i] == num) {
            System.out.print("CONGRATS! You are the winner.");
            return;
        }
        else if (numbers[i] > 101) {
            System.out.print("ERROR! You have to pick the number between 1 and 100.");
        }
        else if (numbers[i] != num) {
            System.out.println("Hard luck. Try again.  ");
        }
        else {
            System.out.println("You lose. No more guesses");
        }
    }
}

If you help me , it'll mean a lot to me.

Thanks!

CodePudding user response:

You could store all your previous guesses in some kind of array or list and every time you take in new user input loop through the previous guesses and if there's a match output "no more guesses" else: continue guessing?

CodePudding user response:

Consider the following code.

import java.util.Random;
import java.util.Scanner;

public class NumberGuessingGame {
    private static final int  GUESSES = 10;
    private static final int  LOWER = 1;
    private static final int  UPPER = 100;

    public static void main(String[] args) {
        Random r = new Random();
        int num = r.nextInt(LOWER, UPPER   1);
        int[] guesses = new int[GUESSES];
        System.out.println(num);
        System.out.printf("Guess a number between %d and %d.%n", LOWER, UPPER);
        Scanner mySc = new Scanner(System.in);
        boolean win = false;
        for (int i = 0; i < GUESSES; i  ) {
            System.out.printf("You have %d guesses left: ", (GUESSES - i));
            int guess = mySc.nextInt();
            if (guess > UPPER  ||  guess < LOWER) {
                System.out.printf("ERROR! You have to pick the number between %d and %d.%n",
                                  LOWER,
                                  UPPER);
                i--;
                continue;
            }
            boolean guessed = false;
            for (int j = 0; j <= i; j  ) {
                if (guess == guesses[j]) {
                    System.out.println("You already tried "   guess);
                    guessed = true;
                    break;
                }
            }
            if (guessed) {
                i--;
                continue;
            }
            guesses[i] = guess;
            if (guesses[i] == num) {
                System.out.print("CONGRATS! You are the winner.");
                win = true;
                break;
            }
            else {
                if (i < GUESSES - 1) {
                    System.out.println("Hard luck. Try again.");
                }
            }
        }
        if (!win) {
            System.out.println("You lose. No more guesses");
        }
    }
}

Here is output from a sample run.

37
Guess a number between 1 and 100.
You have 10 guesses left: 30
Hard luck. Try again.
You have 9 guesses left: 30
You already tried 30
You have 9 guesses left: 111
ERROR! You have to pick the number between 1 and 100.
You have 9 guesses left: 35
Hard luck. Try again.
You have 8 guesses left: 37
CONGRATS! You are the winner.

Each guess is saved in guesses array. After user enters a guess, we iterate through all the guesses that have been entered so far and check if the last guess is in the array. If it is, then it doesn't count as a new guess. Also if the entered guess is not within the allowed range, the guess does not count.

On the last guess, i.e. the tenth guess, we don't print Hard luck. Try again. because the user has no more guesses so they cannot try again.

  • Related