【发布时间】:2012-09-30 19:59:53
【问题描述】:
我有一个hangman项目的逻辑问题,它需要用户的一封信,然后搜索该信是否包含在密码中。问题是我的编程方式,如果用户在密码中猜到的字母多次出现。它只会通过并表示它们。这不是我想要的,我只希望它一次更新正确猜测的字母的状态。
我尝试了一些不同的方法,例如在 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;
}
【问题讨论】:
标签: java string stringbuffer chars