如何在Java中单独读取行

时间:2016-03-23 19:37:50

标签: java java.util.scanner

我正在使用Java进行Nim游戏,如果游戏中有两个人类玩家,我的部分代码是读两个名字,但是当我运行代码时,它打印出要求名字和要求第二个名字的声明,只允许我输入第二个玩家的名字。有人能帮忙吗?谢谢!

System.out.println("Please enter a name for Player 1: ");
String player1Name = scan.nextLine();
player1.setName(player1Name);
System.out.println("Please enter a name for Player 2: ");
String player2Name = scan.nextLine();
player2.setName(player2Name);

2 个答案:

答案 0 :(得分:1)

试试这个:

String player1Name = scan.next();

答案 1 :(得分:0)

试试这个:

public class sendMessageToServerCommand implements Command {

    @Override
    public void execute(String msg){
          sendToServerTheMsg(msg);
    }
}

..
Command command=new sendMessageToServerCommand();
command.execute("msg I got on runtime");