【发布时间】:2018-05-06 18:02:38
【问题描述】:
我正在学习 Java 入门课程,我们有一个处理刽子手游戏的项目。我已经完成了大部分代码,但我无法让这部分按照我想要的方式工作。首先,程序提示用户输入一个字母,我需要它在字母第一次出现在单词中时显示出来。假设这个词是苹果,我输入 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
}
【问题讨论】: