Java 爪哇:刽子手游戏我需要它只揭示第一次发生的猜测

Java 爪哇:刽子手游戏我需要它只揭示第一次发生的猜测,java,substring,indexof,Java,Substring,Indexof,我正在学习Java的入门课程,我们有一个关于刽子手游戏的项目。我已经完成了大部分代码,但是我不能让这部分按照我想要的方式工作。首先,程序提示用户输入一个字母,我需要它在字母第一次出现在单词中时就显示出来。假设单词是apple,我输入p,我只想去p而不是pp。我想我需要使用indexof或类似的东西,但我需要一些帮助来确定使用哪一个以及如何应用它 for (int i = 0; i < word.length(); i++) { //check if the letter is corre

我正在学习Java的入门课程,我们有一个关于刽子手游戏的项目。我已经完成了大部分代码,但是我不能让这部分按照我想要的方式工作。首先,程序提示用户输入一个字母,我需要它在字母第一次出现在单词中时就显示出来。假设单词是apple,我输入p,我只想去p而不是pp。我想我需要使用indexof或类似的东西,但我需要一些帮助来确定使用哪一个以及如何应用它

 for (int i = 0; i < word.length(); i++) { //check if the letter is correct
            if (word.charAt(i) == letter && wordToShow.charAt(i) == '_') 
            {   //checking only free spaces _
                System.out.println("Good!");
                wordToShow.setCharAt(i, letter);  //change '_' to the letter guessed
                guessed += letter;                  //save the letter
                guessed += '+';                     //mark the success
                if (wordToShow.indexOf("_") == -1) 
                {   //if there more unsolved letters?
                    return true;        //there is no '_' symbols, all letters on their places
                }      
for(int i=0;i
找到一个后,您需要做的就是停止检查。只需在if块中插入break语句,它就会退出for循环并停止检查后续字母:

for (int i = 0; i < word.length(); i++) { //check if the letter is correct
        if (word.charAt(i) == letter && wordToShow.charAt(i) == '_') 
        {   //checking only free spaces _
            System.out.println("Good!");
            wordToShow.setCharAt(i, letter);  //change '_' to the letter guessed
            guessed += letter;                  //save the letter
            guessed += '+';                     //mark the success
            if (wordToShow.indexOf("_") == -1) 
            {   //if there more unsolved letters?
                return true;        //there is no '_' symbols, all letters on their places
            }      
            break;
        }
}
for(int i=0;i