我编这个程序是想从键盘键入5个学生的姓名,学号,还有两门课程以及两门课程的成绩,但是我才输入了两个就报错了,而且我想要输出后的结果好看一点,该怎么弄啊?代码和运行结果如下所示:
package 学生成绩管理;
public class Student
{
String sname;
long sno;
String cname1;
String cname2;
int score1;
int score2;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public long getSno() {
return sno;
}
public void setSno(long sno) {
this.sno = sno;
}
public String getCname1() {
return cname1;
}
public void setCname1(String cname1) {
this.cname1 = cname1;
}
public String getCname2() {
return cname2;
}
public void setCname2(String cname2) {
this.cname2 = cname2;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
}
package 学生成绩管理;
import java.util.*;
public class test
{
public static void main(String[] args)
{
System.out.println("姓名"+" "+"学号"+" "+"高数"+" "+"物理");
Student[] stu;
stu=new Student[5];
for(int i=0;i<5;i++)
{
stu[i]=new Student();
stu[i].sname=(new Scanner(System.in).next());
stu[i].sno=(new Scanner(System.in).nextLong());
stu[i].cname1=(new Scanner(System.in).next());
stu[i].cname2=(new Scanner(System.in).next());
stu[i].score1=(new Scanner(System.in).nextInt());
stu[i].score2=(new Scanner(System.in).nextInt());
System.out.println(stu[i].getSname()+"\t"+stu[i].getSno()+"\t"+stu[i].getScore1()+"\t"+stu[i].getScore2()+"\t");
}
}
}
运行结果:
姓名 学号 高数 物理
马叶 2014211401 76 74
张三 2014211405 90 84
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.nextLong(Scanner.java:2222)
at java.util.Scanner.nextLong(Scanner.java:2182)
at 学生成绩管理.test.main(test.java:15)