2 回答

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
如果我正確地閱讀了您的問(wèn)題,您希望繼續(xù)運(yùn)行該giveLetter()函數(shù),直到獲得在 guessedLetters 映射中找不到的輸入。
為此,我建議使用while循環(huán)。一個(gè)while循環(huán)將持續(xù)到給定的條件false。
例如:
int i = 0;
while(i / 2 != 1) {
i ++;
}
此循環(huán)將在i /2 != 1為真時(shí)運(yùn)行。這對(duì)于 i = 0 和 i = 1 是正確的,對(duì)于 i = 2 是錯(cuò)誤的 - 所以它會(huì)停止。
所以考慮到你的問(wèn)題,我建議:
public void givenLetter(){
String givenLetter = player1.giveLetter(); // Get the letter
while (guessedLetters.containsKey(givenLetter)) { // While the given letter is found, continue to run the method.
givenLetter = player1.giveLetter();
} // Exit the loop once the given letter is not found
// Rest of function here
}

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
像這樣的循環(huán):
public void givenLetter(){
while (true) {
String givenLetterString = player1.giveLetter();
if(!guessedLetters.containsKey(givenLetterString))
return;
}
}
它將一直運(yùn)行,直到集合中不存在該字母為止。
while (true)創(chuàng)建一個(gè)無(wú)限循環(huán),只有在return執(zhí)行語(yǔ)句時(shí)它才會(huì)停止。
添加回答
舉報(bào)