我的刽子手项目有一个逻辑问题,它需要用户的一封信并搜索该字母是否包含在 secret 单词中。问题在于我的编程方式,如果用户在 secret 单词中猜测的字母多次出现。它只会遍历并表示所有这些。这不是我想要的,我只想它一次更新正确猜测的一个字母的状态。
我尝试了一些不同的东西,例如在 status(guessCh,
) 之后设置中断,但迭代器只会转到字母匹配的第一个匹配项并在那里停止。
对此有什么简单的解决办法吗?
private void compare(String str)
{
guessCh = str.charAt(0);
char secretCh = '0';
for (int i = i2; i < secretWord.length(); i++) // Cuts the secret word into individual chars to process.
{
secretCh = secretWord.charAt(i);
// Compare the two strings.
if (guessCh == secretCh)
{
status(guessCh, i); // Sends the letter & placement to status().
}
}
}
n
private String status(char guessCh, int placement)
{
/* Update and return status. */
if (guessCh >='A' && guessCh <= 'Z')
{
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement,guessCh).toString();
println("That guess is correct.");
canvas.displayWord(status);
return status;
}
return status;
}
最佳答案
您可以使用比较方法中的状态变量来测试先前的解决方案。
if (guessCh == secretCh && status.charAt(i) != secretCh)
{
status(guessCh, i);
break;
}
关于java:刽子手游戏重复字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664597/