JAVA中的对象数组提供InputMismatchException
附加异常的代码和快照。请帮我解决输入不匹配的问题。我认为在运行时输入值时有问题
import java.util.Scanner;
class ObjectArray
{
public static void main(String args[])
{
Scanner key=new Scanner(System.in);
Two[] obj=new Two[3];
for(int i=0;i<3;i++)
{
obj[i] = new Two();
obj[i].name=key.nextLine();
obj[i].grade=key.nextLine();
obj[i].roll=key.nextInt();
}
for(int i=0;i<3;i++)
{
System.out.println(obj[i].name);
}
}
}
class Two
{
int roll;
String name,grade;
}
# 1 楼答案
使用
Integer.parseInt(key.nextLine());
}
输出
# 2 楼答案
而不是:
使用:
这样可以确保正确拾取和处理整数后的换行符