【发布时间】:2012-01-18 04:23:32
【问题描述】:
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”没有初始化,即使我在输入输入时初始化它,命令行也保持空白,所以它对我的输入没有任何作用。
【问题讨论】:
标签: java loops methods while-loop do-while