繁体   English   中英

猜游戏,两次都不会计算相同的猜想

[英]Guessing game where doesn't count the same guess twice

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

这就是我到目前为止(我不知道如何解决这个问题)

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<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);

编码愉快! (:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM