有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是这个网站的新手。我已经决定创建一个基于游戏机的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);
        }

    }

}

共 (2) 个答案

  1. # 1 楼答案

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

    Set<String> usedLetters = new HashSet<String>();
    
    boolean isUsedLetter(String letter) {
        if (usedLetters.contains(letter)) {
            return true;
        } else {
            usedLetters.add(letter);
            return false;
        }
    }
    
  2. # 2 楼答案

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

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