Java 循环似乎工作不正常,它跳过了第一个字段
好吧,这是我的问题。这是我正在编写的代码。在第一个循环中,这很好。但当它进入下一个循环时,申请人名称上的光标不会出现。它跳转到学习水平。为什么会这样?我的代码有问题吗Java 循环似乎工作不正常,它跳过了第一个字段,java,Java,好吧,这是我的问题。这是我正在编写的代码。在第一个循环中,这很好。但当它进入下一个循环时,申请人名称上的光标不会出现。它跳转到学习水平。为什么会这样?我的代码有问题吗 while (totalScholarship != 0) { System.out.println("Applicant Name: "); s.setsName(input.nextLine()); System.out.println("Study Level: "); System.out
while (totalScholarship != 0) {
System.out.println("Applicant Name: ");
s.setsName(input.nextLine());
System.out.println("Study Level: ");
System.out.println("1-Pre Dip, 2-Dip 3-Degree");
s.setiStudyLevel (input.nextInt());
System.out.println("Personallity Score: ");
s.setiPersonalityScore(input.nextInt());
System.out.println("Parent Income: ");
s.setdParentIncome(input.nextDouble());
int iPersonalityScore = s.getiPersonalityScore();
double dParentIncome = s.getdParentIncome();
if (iPersonalityScore < 350) {
if (dParentIncome >= 3000) {
System.out.println("NOT ELIGIBLE!");
} else {
System.out.println("CONGRATULATIONS!");
bEligible = true;
}
} else {
System.out.println("CONGRATULATIONS!");
}
System.out.println("");
System.out.println(s.toString());
}
while(总奖学金!=0){
System.out.println(“申请人名称:”);
s、 setsName(input.nextLine());
System.out.println(“学习级别:”);
系统输出打印LN(“1-预浸,2-浸3度”);
s、 setiStudyLevel(input.nextInt());
System.out.println(“个性分数:”);
s、 setiPersonalityScore(input.nextInt());
System.out.println(“母公司收入:”);
s、 setdParentIncome(input.nextDouble());
int iPersonalityScore=s.getiPersonalityScore();
double dParentIncome=s.getdParentIncome();
if(iPersonalityScore<350){
如果(dParentIncome>=3000){
System.out.println(“不合格!”);
}否则{
System.out.println(“恭喜你!”);
可信=真实;
}
}否则{
System.out.println(“恭喜你!”);
}
System.out.println(“”);
System.out.println(s.toString());
}
在此之后添加input.nextLine()
:
System.out.println ("Parent Income: ");
s.setdParentIncome (input.nextDouble());
因为在第一次迭代中,您在input.nextDouble()
之后按下的“Enter”
键在第二次迭代中作为input.nextLine()
内的输入接收。这就是为什么您觉得输入被跳过了。
例如:
int option = input.nextInt();
input.nextLine(); // Consume newline left-over
String str1 = input.nextLine();
因此,在循环结束时输入一个
input.nextLine()
,它将正常工作请参见。基本上,在调用nextDouble()
之后,仍然需要使用一个换行符,我猜输入是scanner的一个实例。可能会发布您的示例输入。