【发布时间】:2017-11-25 19:03:40
【问题描述】:
我正在使用 GUI 在 Netbeans 中编写游戏。在游戏中,随机产生15-30之间的石头,用户和计算机轮流去除1-3之间的石头,直到没有剩余。最后拿走最后一块石头的玩家输了。我的问题是我的方法(validIn())不会在playerMove()中使用,所以即使满足条件,游戏也不会更新。 这是我认为问题所在:
private boolean validIn(int num){
//
return num < 3 || num > 1 || num < totalStone;
}
public void playerMove(){
// Geting the user input
int userIn = Integer.parseInt(txtIn.getText());
// Declaring and assigning a boolean
boolean check = false;
// If the boolean returns true
if (check == true) {
// Subtracting how much the player took from the total amount of rocks
totalStone -= userIn;
// Displaying how many rocks were taken and how many are left
txtOut.setText(txtOut.getText() +"\nYou picked up " +userIn +" stone(s). There are " +totalStone +" stone(s) left.");
}
// Else,
else {
// Assign the boolean check to what the method validIn returns
check = validIn(userIn);
}
}
public void checkWinner(String player){
// If there are no more stones left,
if (totalStone == 0){
// Display a message that says who won
txtOut.setText(txtOut.getText() +"\nThere are no more stones left. "+player +" wins!");
}
}
private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {
//
if (totalStone > 0){
// Display how many rocks there are
txtOut.setText(txtOut.getText() +"\nThere are " +totalStone +" stone(s).");
// The player does their move
playerMove();
// Checking if the computer won
checkWinner("The computer");
// Computer does a turn
computerMove();
// Checking if the player won
checkWinner("The player");
}
//
else if (totalStone == 0){
//
txtOut.setText(txtOut.getText() + "\nThe game has ended.");
}
}
【问题讨论】:
-
当你声明为假之前的那一行时,如何检查为真?
-
哪种方法不会返回 true?您发布的唯一返回
boolean的方法将总是返回 true。 -
另外... validIn() 将始终返回 true,因为每个整数要么小于 3,要么大于 1
标签: java methods netbeans boolean user-input