我有一個hangman項目的邏輯問題,它需要用戶的一封信,並搜索該字母是否包含在祕密詞中。問題在於我編寫它的方式,如果用戶在祕密詞中猜出了幾個字母。它只會通過並表示它們。這不是我想要的,我只希望它一次更新正確猜測的字母的狀態。java: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().
}
}
}
ň
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;
}
爲'status.charAt測試(i)!= secretCh'使其跳過已經解決的字母,並且當一個新字母被解決時,「break」使它停止。 – phatfingers