如何使字符串在循环中第二次不可用?

ctehm74n  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(296)

我是这个网站的新手。我已经决定创建一个控制台基地hangmaan游戏,我一直做得很好,直到现在。我现在的问题使我陷入困境。
我试着这样做,如果用户输入了一个字母,它被标记为正确或不正确。然后,程序不应该让用户在while循环的后续迭代中再次输入相同的字母。
这些评论应该能让你更好地理解我在说什么。
有人能帮我吗?

package hangMan2;

import java.io.IOException;
import java.util.Scanner;

public class mainClss {
    public static void main(String[] args) {

        int point = 0;
        int numberOfLetterInWord;

        // prompt user for input and store input into String word
        System.out.println("Enter a word in lower case from the dictionary: ");
        Scanner inputWord = new Scanner(System.in);
        String word = inputWord.next();

        // letters remaining in word equals the length of the word at the start
        int lettersRemainingInWord = word.length();

        for (int i = 0; i < 30; i++) {
            System.out.println("\b");

        }

        // while points are above 7 (7 is when man is hung) and there's more
        // than one letter remaining do all the following:
        while (point > -7 && lettersRemainingInWord >= 1) {

            //prompts user to input letter guess and stores in letter
            System.out.print("\nEnter a letter for this " + word.length()
                    + " letter word: ");
            Scanner inputLetter = new Scanner(System.in);
            String letter = inputLetter.next();

            if (word.contains(letter)) {
                System.out.println("Correct!");
                point += 1;
                System.out.println("Score: " + point);
                lettersRemainingInWord -= 1;
                //I need code here to remove the current letter from being able to be used again

                if (lettersRemainingInWord > 0) {
                    continue;
                }

                else {
                    System.out.println("\nYou win!!!");
                    System.out.println("The word was: " + word);
                    break;
                }

            }

            else {
                System.out.println("Incorrect\n");
                point -= 1;
                System.out.println("Score: " + point);
                //I need code here to remove the current letter from being able to be used again

                if (lettersRemainingInWord > 0) {
                    continue;
                }

                else {
                    System.out.println("Game over! You lose!");
                    System.out.println("Score: " + point);
                    System.out.println("The word was: " + word);
                    break;
                }

            }

        }

        if (point <= -7) {
            System.out.println("Game over! You lose!");
            System.out.println("Score: " + point);
            System.out.println("The word was: " + word);
        }

    }

}
yqkkidmi

yqkkidmi1#

你可以测试这个字母是否在一个集合中。如果没有,接受它并将其添加到集合中。如果是,那么拒绝它。

Set<String> usedLetters = new HashSet<String>();

boolean isUsedLetter(String letter) {
    if (usedLetters.contains(letter)) {
        return true;
    } else {
        usedLetters.add(letter);
        return false;
    }
}
xt0899hw

xt0899hw2#

可以使用arraylist保存已键入的字符,然后检查列表中是否有该字符。

List<Character> used = new ArrayList<Character>();
char let = //letter that they inputted
if(used.contains(let)) {
     //do something
}
else {
     used.add(let);
}

相关问题