嗨,我是編程新手,我們有一個任務來創建一個hang子手遊戲。現在我遇到的問題是猜測。該計劃猜測一切正常,並且正常工作。雖然它不打印出JTextField
中的字母,或者它打印出1個字母,但是當我再次猜測正確時,前面的字母會被覆蓋。徘徊者檢查邏輯
那麼任何友善的靈魂都可以伸出援助之手呢?
我的繼承人的檢查代碼:
private class check implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
// Grabs the letter from the guessField and converts it into a char which can be used to compare against the word.
guess = guessField.getText();
guessField.setText("");
char guess2 = guess.charAt(0);
String displaySecret = "";
for (int i = 0; i < random.length(); i++)
displaySecret += "*";
//read in a guess
int position = random.indexOf(guess2);
//now position contains the index of guess inside secret, or
//-1 if the guess was wrong
String newDisplaySecret = "";
for (int i = 0; i < random.length(); i++)
if (i == position)
newDisplaySecret += random.charAt(i); //newly guessed character
else
newDisplaySecret += displaySecret.charAt(i); //old state
displaySecret = new String(newDisplaySecret);
wordField.setText(displaySecret);
}
使用JTextField和wordField.getText(displaySecret)的Im;它不適用於它爲我說的字符串。 – Looptech
對不起,它不參與編輯。 getText()適用,因爲它是從JTextComponent繼承的。 – Dunedain
感謝它正在工作,除了現在它不選擇所有的答:如果一個字是香蕉例如 – Looptech