首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scan.nextLine不工作

scan.nextLine不工作
EN

Stack Overflow用户
提问于 2013-10-12 22:43:34
回答 1查看 13.2K关注 0票数 1

我使用Java,我尝试编写程序来读取许多患者,然后让员工输入他们的姓名和年龄。

我希望程序读取全名,比如(Maha Saeed),所以我写了这样的代码,但我不知道为什么它不能工作

代码语言:javascript
复制
name = scan.nextLine();

这是完整的代码

代码语言:javascript
复制
import java.util.*;

public class Answer1
{
static Scanner scan = new Scanner (System.in);
public static void main (String[] args)
{

int age ; 
int PatientNumber ;
int PatientNO;
String name ;


System.out.print("Enter number of patients :");

PatientNumber =  scan.nextInt();
PatientNO = 1;

while ( PatientNO <= PatientNumber)
{
System.out.println("Patient #" +PatientNO);
System.out.print("Enter patient's Name: ");
name = scan.nextLine(); //<- here is the problem if I write scan.next it works but it reads only the first name

System.out.print("Enter patient's Age: ");
age= scan.nextInt();
PatientNO = PatientNO + 1;
}

}
}

感谢所有人

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-12 22:50:54

请参阅Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

简单的解决方案,你可以使用\n字符:

代码语言:javascript
复制
scan.nextLine();
name = scan.nextLine();
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19335420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档