猜猜游戏不会计算两次相同的猜测

时间:2014-10-23 02:14:25

标签: java arrays

在我的代码中,即使他们重复相同的猜测,也会计算每个错误的猜测。但是我想找到一种方法,它不会将任何重复的猜测作为猜测,而是给出一条消息说你已经猜到了这个数字并再试一次。

这是我到目前为止(我不知道如何处理)

public static void main (String[] args)

{
    c = new Console ();

    // Place your program here.  'c' is the output console
    char ynToContinue;
    int maxNumber;
    int minNumber;
    int maxx = c.getHeight ();
    int maxy = c.getWidth ();
    Font titleFont = new Font ("Impact", 0, 45);
    c.setFont (titleFont);
    c.drawString ("LETS PLAY THE GUESSING GAME!!", 50, 180);

    Font font1 = new Font ("Arial", 0, 25);
    c.setFont (font1);
    c.setColor (Color.RED);
    c.drawString ("Press any key to continue", 175, 220);
    char anyKey = c.getChar ();
    c.clear ();
    Font rulesTitle = new Font ("Arial", 0, 20);
    c.setFont (rulesTitle);

    c.drawString ("How To Play:", 5, 25);
    Font subTitle = new Font ("Arial", 0, 18);
    c.setFont (subTitle);
    c.setColor (Color.BLACK);
    c.drawString ("1. The Game will choose a number from the range of your choice.", 5, 45);
    c.drawString ("2. Then you will guess the number.", 5, 65);
    c.drawString ("3. The faster you guess the right number, the higher your score.", 5, 85);
    c.setColor (Color.RED);
    c.drawString ("Press any key to continue", 5, 105);
    anyKey = c.getChar ();
    c.clear ();
    do
    {

        c.println ("Please enter the max number you want to guess");
        maxNumber = c.readInt ();
        c.println ("Pleae enter the minimum number you want to guess");
        minNumber = c.readInt ();
        c.clear ();
    }
    while (maxNumber < minNumber);
    {
    }
    Random generator = new Random ();
    int randomInt;
    randomInt = generator.nextInt ((maxNumber - minNumber) + 1) + minNumber;
    c.drawString (" The Game has chosen a random number within your range", 5, 25);
    c.drawString (" Would you like to guess it? (y/n)", 5, 45);
    do
    {
        do
        {
            ynToContinue = Character.toLowerCase (c.getChar ());
        }
        while (ynToContinue != 'y' && ynToContinue != 'n');
        c.clear ();
        c.print ("GOODBYE");
    }
    while (ynToContinue == 'n');
    {
        c.clear ();
    }
    int guess;
    int guessCount = 0;
    do
    {
        guessCount++;
        c.print ("Enter your guess");
        guess = c.readInt ();
        if (guess > maxNumber || guess < minNumber)
        {
            c.clear ();
            c.println ("That is not withing the range you entered");
            guessCount -= 1;
        }
        else if (guess != randomInt)
        {
            c.clear ();
            c.println ("TryAgain");
        }


    }
    while (guess != randomInt);
    {
        c.clear ();
        c.println ("Congrats");
        c.println ("Your Score is: " + ((maxNumber - minNumber + 1) - guessCount) + " points");


    }


} // main method
} // GuessingGame class

1 个答案:

答案 0 :(得分:1)

您想要使用Set.这是因为它不存储重复值,因此您可以在集合中检查用户是否已经猜到了值。

Set<Integer> guesses = new TreeSet<>();

do {
    c.print ("Enter your guess");
    guess = c.readInt ();
    if (guesses.contains(guess)) {
        c.clear();
        c.println ("You have already made that guess!");
    } else if (guess > maxNumber || guess < minNumber) {
        c.clear ();
        c.println ("That is not within the range you entered");
    } else {
        guessCount++;
        guesses.add(guess);
        else if (guess != randomInt) {
            c.clear ();
            c.println ("TryAgain");
        }
    }
} while (guess != randomInt);

快乐的编码! (: