java - 猜谜游戏不计算相同的猜测两次

标签 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

最佳答案

您想使用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);

编码愉快! (:

关于java - 猜谜游戏不计算相同的猜测两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26520237/

相关文章:

java - 更高质量的 JFreeChart 图表

java - 在 servlet 中获取 cookie

java - Spring Boot自定义通过抛出异常接收带有HTTP OPTIONS的响应

java - 带有 ListView 的 XYPlot 具有黑色背景而不是透明背景

java - 如何使扫描仪变量成为数组?

Java 删除 -> 编译时类型对齐 - 或 Java 库/框架代码转换为应用程序逻辑

java - 修复数组索引越界异常

c++ - 从 vector 的 vector 中移除重叠

java - 当我们将数组作为参数传递给其他函数时,数组的值会如何变化?

arrays - Array.Find和IndexOf用于完全相同对象的多个元素