我目前正在製作一個紙張石頭剪刀遊戲,你在電腦上玩。我用於控制誰達到最高分數的while循環一直保持凍結狀態。我希望下面的方法不太難閱讀,並且有足夠的信息。基本上,當我玩這個遊戲並嘗試移動時,程序就會凍結。我試過沒有while循環的程序,它工作正常。我做到了,所以每一步都是一個數字。 0表示岩石,1表示剪刀,2表示紙張。方法computerPlayer.newChoice();是來自另一個班級的隨機生成的數字。While循環凍結
public void newChoice() {
while (humanS < 3 && computerS < 3) {
computerPlayer.newChoice();
if (playerChoice == 0) {
viewer.jLblHumanM.setText("Sten");
if (computerPlayer.choice == 0) {
viewer.jLblComputerM.setText("Sten");
} else if (computerPlayer.choice == 1) {
HScore += 1;
viewer.jLblComputerM.setText("Sax");
viewer.jLblHumanS.setText("" + HScore);
} else if (computerPlayer.choice == 2) {
CScore += 1;
viewer.jLblComputerM.setText("Påse");
viewer.jLblComputerS.setText("" + CScore);
}
} else if (playerChoice == 1) {
viewer.jLblHumanM.setText("Sax");
if (computerPlayer.choice == 0) {
HScore += 1;
viewer.jLblComputerM.setText("Sten");
viewer.jLblHumanS.setText("" + HScore);
} else if (computerPlayer.choice == 1) {
viewer.jLblComputerM.setText("Sax");
} else if (computerPlayer.choice == 2) {
HScore += 1;
viewer.jLblComputerM.setText("Påse");
viewer.jLblHumanS.setText("" + HScore);
}
} else if (playerChoice == 2) {
viewer.jLblHumanM.setText("Påse");
if (computerPlayer.choice == 0) {
viewer.jLblComputerM.setText("Sten");
HScore += 1;
viewer.jLblHumanS.setText("" + HScore);
} else if (computerPlayer.choice == 1) {
viewer.jLblComputerM.setText("Sax");
CScore += 1;
viewer.jLblComputerS.setText("" + CScore);
} else if (computerPlayer.choice == 2) {
viewer.jLblComputerM.setText("Påse");
}
}
}
}
如果'(人類<3臺&&電腦<3)'那麼你就會不斷的打電話'newChoice'永遠(並且有可能在某些時候的StackOverflowError).. – assylias
我不認爲你正在增加你的'humanS'或'computerS'變量...... – brso05
什麼線程正在執行這個代碼? – talex