gpt4 book ai didi

java - JAVA中 "while loop"之后输出不一致

转载 作者:行者123 更新时间:2023-12-01 17:46:23 24 4
gpt4 key购买 nike

我是JAVA新手,写了一个Nim游戏项目,其中包括Nimsys和Nimplayer。一切看起来都很好。当我运行测试文件时,结果是正确的。

但是,当再次播放时,突然出现一个多余的空格,我不能想办法。可能会有一些错误。非常感谢任何帮助。

The test results

import java.util.Scanner;

public class Nimsys {
int stoneBalance;

public static void main(String[] args) {
String playOrNot;
Scanner input = new Scanner(System.in);// Scanner
System.out.println("Welcome to Nim\n");
// bring two players in the main.

System.out.println("Please enter Player 1's name:");
NimPlayer player1 = new NimPlayer(); // bring player1 object to be used
String name1 = input.nextLine(); // set the string's name to be used
System.out.println("");

NimPlayer player2 = new NimPlayer(); // bring player2 object to be used
System.out.println("Please enter Player 2's name:");
String name2 = input.nextLine();
System.out.println("");

do {
// stone upper bound
System.out.println("Please enter upper bound of stone removal:");
int removeUpBound = input.nextInt();
System.out.println("");

// intital stone numbers
System.out.println("Please enter initial number of stones:");
int totalStone = input.nextInt();
System.out.println("");

NimPlayer numStone = new NimPlayer(); // bring new object to be used
numStone.initialStone(totalStone);
System.out.print(numStone.stoneBalance + " stones left:");
numStone.printStar(totalStone);

// while stoneBalance > 0, two players keep playing the game
while (numStone.stoneBalance > 0) {
// player1's turn and remove the stones; decision of winning
player1.enterName(name1); // pass the name to be used
player1.player1Turn();
int takeStone = input.nextInt();
while (takeStone > removeUpBound || takeStone <= 0) {
System.out.println(
"Invalid, you need to remove stones under upper bound limit or above 0. \n Please enter again.");
takeStone = input.nextInt();
}

numStone.removeStones(takeStone);

if (numStone.stoneBalance > 0) {
System.out.print(numStone.stoneBalance + " stones left:");
numStone.printStar(numStone.stoneBalance);
} else if (numStone.stoneBalance <= 0) {
System.out.println("Game Over\n" + name2 + " wins!\n");
break;
}

// player2's turn and remove the stones; decision of winning
player2.enterName(name2); // pass the name to be used
player2.player2Turn();
int takeStone2 = input.nextInt();
while (takeStone2 > removeUpBound || takeStone2 <= 0) {
System.out.println(
"Invalid, you need to remove stones under upper bound limit or above 0. \n Please enter again.");
takeStone2 = input.nextInt();
}
numStone.removeStones(takeStone2);

if (numStone.stoneBalance > 0) {
System.out.print(numStone.stoneBalance + " stones left:");
numStone.printStar(numStone.stoneBalance);
} else if (numStone.stoneBalance <= 0) {
System.out.println("Game Over\n" + name1 + " wins!\n");
break;
}

}
System.out.println("Do you want to play again (Y/N):");
playOrNot = input.next();

} while (playOrNot.equals("Y"));

}
}
public class NimPlayer {
String player1;
String player2;
int stoneBalance;
int stars;

public void enterName(String name) {
player1 = name;
player2 = name;
}

public void initialStone(int startStones) {
stoneBalance = startStones;
}

public void removeStones(int stonesTaken) {

int updatedBalance = stoneBalance - stonesTaken;
stoneBalance = updatedBalance;
}

public void player1Turn() {
System.out.println(player1 + "'s turn - remove how many?\n");
}

public void player2Turn() {
System.out.println(player2 + "'s turn - remove how many?\n");
}

public void printStar(int star) {
stars = star;
stars = stoneBalance;
for (int stars = 1; stars <= star; stars++) {
System.out.print(" *");
}
System.out.println();
}
}

最佳答案

要完全解决您的两个问题,我将执行以下操作 -

        input.nextLine();
System.out.println("Do you want to play again (Y/N):");
playOrNot = input.nextLine();

而不是

System.out.println("Do you want to play again (Y/N):");
playOrNot = input.next();

关于java - JAVA中 "while loop"之后输出不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com