扫描仪跳过 for 循环中的输入
Scanner skips input in a for loop
所以我有这个代码:
public class Subjects {
String name;
int period;
char grade;
public void period()
{
System.out.println("I have " + this.name + " during period " + this.period + ".");
}
public void study()
{
if (this.grade == 'B')
{
System.out.println("I study for " + this.name + ", so I get a B!");
}
else if (this.grade == 'A')
{
System.out.println("I study for " + this.name + ", so I get an A!");
}
else
{
System.out.println("I don't study for " + this.name + ", so I get a " + this.grade +". :(");
}
}
}
而这个测试class:
import java.util.Scanner;
public class SubjectsTest {
public static void main (String [] args)
{
Scanner kboard = new Scanner(System.in);
Subjects[] classes;
System.out.print ("How many classes do you have? ");
int x = kboard.nextInt();
int y;
classes = new Subjects[x];
for (int b = 0; b < x; b++)
{
classes[b] = new Subjects();
}
for (int a = 0; a < x; a++)
{
y = a + 1;
System.out.println("Period " + y );
System.out.println ("Enter the subject name: ");
classes[a].name = kboard.nextLine();
System.out.println ("Enter your class period: ");
classes[a].period = kboard.nextInt();
System.out.println ("Enter your grade in the class: ");
classes[a].grade = kboard.next().charAt(0);
}
for (int i = 0; i < x; i++)
{
classes[i].period();
classes[i].study();
}
}
}
应该发生的是用户输入他们拥有的 classes 的数量(例如 8),然后输入每个人的名字、时期和他们的成绩。然后在最后,它为每个 class 打印 2 个语句。
但是,当我 运行 程序(在 Eclipse 中)询问 How many classes do you have?
并且用户回答后,系统会打印出接下来的两个问题,而无需等待对第一个问题的回答.我的错误消息如下所示:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at SubjectsTest.main(SubjectsTest.java:27)
为什么要这样做?我该如何解决?我是 Java 的新手,如有任何帮助,我们将不胜感激!
你应该在你的 kboard.nextInt();
调用之后放置一个 kboard.nextLine();
来获取 类 的号码。
这将读取 kboard.nextInt();
行的其余部分,并允许读取您的主题名称以正常工作。目前,您要读入主题名称的 kboard.nextLine();
正在读入您输入的剩余部分 类。因此,当您尝试阅读该主题时,它实际上是在等待 int
句号并给您那个例外。
编辑:抱歉所有的编辑,这个问题的公认答案可能更有意义:Using scanner.nextLine()
所以我有这个代码:
public class Subjects {
String name;
int period;
char grade;
public void period()
{
System.out.println("I have " + this.name + " during period " + this.period + ".");
}
public void study()
{
if (this.grade == 'B')
{
System.out.println("I study for " + this.name + ", so I get a B!");
}
else if (this.grade == 'A')
{
System.out.println("I study for " + this.name + ", so I get an A!");
}
else
{
System.out.println("I don't study for " + this.name + ", so I get a " + this.grade +". :(");
}
}
}
而这个测试class:
import java.util.Scanner;
public class SubjectsTest {
public static void main (String [] args)
{
Scanner kboard = new Scanner(System.in);
Subjects[] classes;
System.out.print ("How many classes do you have? ");
int x = kboard.nextInt();
int y;
classes = new Subjects[x];
for (int b = 0; b < x; b++)
{
classes[b] = new Subjects();
}
for (int a = 0; a < x; a++)
{
y = a + 1;
System.out.println("Period " + y );
System.out.println ("Enter the subject name: ");
classes[a].name = kboard.nextLine();
System.out.println ("Enter your class period: ");
classes[a].period = kboard.nextInt();
System.out.println ("Enter your grade in the class: ");
classes[a].grade = kboard.next().charAt(0);
}
for (int i = 0; i < x; i++)
{
classes[i].period();
classes[i].study();
}
}
}
应该发生的是用户输入他们拥有的 classes 的数量(例如 8),然后输入每个人的名字、时期和他们的成绩。然后在最后,它为每个 class 打印 2 个语句。
但是,当我 运行 程序(在 Eclipse 中)询问 How many classes do you have?
并且用户回答后,系统会打印出接下来的两个问题,而无需等待对第一个问题的回答.我的错误消息如下所示:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at SubjectsTest.main(SubjectsTest.java:27)
为什么要这样做?我该如何解决?我是 Java 的新手,如有任何帮助,我们将不胜感激!
你应该在你的 kboard.nextInt();
调用之后放置一个 kboard.nextLine();
来获取 类 的号码。
这将读取 kboard.nextInt();
行的其余部分,并允许读取您的主题名称以正常工作。目前,您要读入主题名称的 kboard.nextLine();
正在读入您输入的剩余部分 类。因此,当您尝试阅读该主题时,它实际上是在等待 int
句号并给您那个例外。
编辑:抱歉所有的编辑,这个问题的公认答案可能更有意义:Using scanner.nextLine()