public void humanPlay()
{
if (player1.equalsIgnoreCase("human"))
System.out.println("It is player 1's turn.");
else
System.out.println("It is player 2's turn.");
System.out.println("Player 1 score: " + player1Score);
System.out.print("Player 2 score: " + player2Score);
String eitherOr;
do {
eitherOr= input.nextLine();
humanRoll();
} while (eitherOr.isEmpty());
if (!eitherOr.isEmpty())
humanHold();
}
这是整个方法,我唯一要解决的就是这个。
String eitherOr;
do {
eitherOr= input.nextLine();
humanRoll();
} while (eitherOr.isEmpty());
它必须多次接受输入,因此每次都需要输入来确定会发生什么,这就是我喜欢 Do While 循环的原因,但由于它每次至少初始化一次,所以我得到了比需要的额外滚动。
我试图这样做,以及这种方式的各种变体:
String eitherOr = input.nextLine();
while(eitherOr.isEmpty());
humanRoll();
这不起作用,因为它不会再次要求输入。如果我尝试输入 input.nextline(); 进入while循环,它说“eitherOr”没有初始化,即使我在输入输入时初始化它,命令行保持空白,所以它对我的输入没有任何作用。