简体   繁体   English

如何在Java中分别读取行

[英]How to read in lines separately in Java

I'm working on a Nim game in Java, and part of my code is to read in two names if the game is to have two human players, but when I run the code it prints the statement asking for the first name and the statement asking for the second name, which only allows me to enter a name for the second player. 我正在用Java开发Nim游戏,如果游戏要有两个人类玩家,我的代码的一部分是用两个名字读的,但是当我运行代码时,它会打印要求输入名字和声明的语句询问第二个名字,这只允许我输入第二个玩家的名字。 Can anyone help with this? 有人能帮忙吗? Thanks! 谢谢!

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);

尝试这个:

String player1Name = scan.next();

Try this: 尝试这个:

Scanner scan = new Scanner(System.in);
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM