我正在Netbeans中制作一个名为"Game of Nim"的游戏。基本上,生成 15-30 块之间的随机数量的石头,计算机和玩家轮流拿 1-3 块石头,直到没有石头。拿走最后一块石头的玩家输了。我正在以 jframe 形式对其进行编码。我想确保玩家输入的数字不会大于 3、小于 1 且大于总宝石数,所以我用一个 if 语句制作了一个 while 循环,用于满足要求的输入,如果不满足,则使用 else 语句。我的问题是,当玩家输入不应该输入的数字时,不会出现错误消息,游戏会照常进行。这就是我认为问题所在:
public int playerInput(){
// Getting the user input and converting it into an integer
int userIn = Integer.parseInt(txtIn.getText());
//
boolean input = false;
// Do this while the input isn't between 1-3 and higher than the total amount of rocks
while(!input){
//
if (userIn < 3 || userIn > 1 || userIn < totalStone){
//
input = true;
}
//
else{
// Output an error message
txtaOut.setText(txtaOut.getText() +"nEnter a number between 1 - 3 and less than the amount of stones left.");
}
}
// return the amount of rocks the user takes
return userIn;
}
这是游戏的大部分代码(我将使用随机斜杠进行注释(:
public void computerMove() {
// Generating a number for the computer's move
int comIn = (int)(Math.random() * 2) + 1;
// If number generated is bigger than the total stones,
if (comIn > totalStone){
// Get the difference between the total and the random number
int totalComDiff = Math.abs(totalStone - comIn);
// Subtract the difference from the random number
comIn -= totalComDiff;
// Substract the rocks taken from the total
totalStone -= comIn;
// Display a message of the rocks taken and the rocks left
txtaOut.setText(txtaOut.getText() +"nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");
}
// Otherwise, if the random number is smaller than the total,
else if (comIn < totalStone){
// Substract the rocks taken from the total
totalStone -= comIn;
// Display a message of the rocks taken and the rocks left
txtaOut.setText(txtaOut.getText() +"nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");
}
// If the total equals amount the computer takes,
else if (totalStone == comIn){
// Substract the rocks taken from the total
totalStone -= comIn;
// Display a message of the rocks taken and the rocks left
txtaOut.setText(txtaOut.getText() +"nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");
// Display a message that says the player wins
txtaOut.setText(txtaOut.getText() +"nThere are no more stones left. The player wins!");
}
// Otherwise, if the amount of stones is 0,
else if (totalStone == 0){
// Display an end game message
txtaOut.setText(txtaOut.getText() + "nThe game has ended.");
}
}
public void playerMove(){
// If there are no more stones left,
if (playerInput() == totalStone){
// Subtracting how much the player took from the total amount of rocks
totalStone -= playerInput();
// Displaying how many rocks were taken and how many are left
txtaOut.setText(txtaOut.getText() +"nYou picked up " +playerInput() +" stone(s). There are " +totalStone +" stones left.");
// Display a message that says the computer wins
txtaOut.setText(txtaOut.getText() + "nThere are no more stones left. The computer wins.");
}
//
else if (playerInput() != totalStone){
// Subtracting how much the player took from the total amount of rocks
totalStone -= playerInput();
// Displaying how many rocks were taken and how many are left
txtaOut.setText(txtaOut.getText() +"nYou picked up " +playerInput() +" stone(s). There are " +totalStone +" stones left.");
}
//
else if (totalStone <= 0){
//
txtaOut.setText(txtaOut.getText() + "nThe game has ended.");
}
}
private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {
//
if (totalStone > 0){
// Display how many rocks there are
txtaOut.setText(txtaOut.getText() +"nThere are " +totalStone +" stones.");
// The player does their move
playerMove();
}
//
if (totalStone > 0){
// Computer does a turn
computerMove();
}
//
else if (totalStone == 0){
//
txtaOut.setText(txtaOut.getText() + "nThe game has ended.");
}
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// Generating another random number
totalStone = (int)(Math.random() * 15) + 15;
// Clearing all the textfields
txtIn.setText("");
txtaOut.setText("");
// Outputting the number of starting stones
txtaOut.setText(txtaOut.getText() +"There are " +totalStone +" stones. It's your turn.");
}
你的if需要看起来像这样:
while (!input) {
if (!(userIn < 3 && userIn > 1 && userIn < totalStone)) {
// Output an error message
txtaOut.setText(txtaOut.getText() +"nEnter a number between 1 - 3 and less than the amount of stones left.");
// Prompt the user again
userIn = Integer.parseInt(txtIn.getText());
} else {
input = true;
}
}
而且,它会起作用。
最好先检查条件是否无效,如果验证通过,则在 else 块中执行正常流程。
你写它的方式,if 条件总是真的,因为 || 代表"或",所以你问天气用户是小于 3 或大于 1 或小于 totalStone,这总是正确的。
另一方面,&&代表">和",而"!"代表不。因此,您基本上希望满足所有条件,并通过将它们放入括号中并放入 !(否定(在前面
如果不满足条件,还需要再次提示用户。否则它将永远运行并冻结 ui。
如果用户输入了错误的号码,则应提示他们输入新号码。在你的代码中,它将永远卡在循环中。
您还应该使用 && 检查是否满足所有条件。您需要一个数字是 (<=3( 和 (>=1( 和 (<=总石头(
public int playerInput () {
int userIn;
do {
System.out.println("Enter a number between 1 - 3 and less than the amount of stones left.")
userIn = Integer.parseInt (txtIn.getText ());
} while (!(userIn <= 3 && userIn >= 1 && userIn <= totalStone));
在不满足条件时,这将继续循环。