Java 运行出错 Exception in thread "main" 求大佬帮解决

Whistleྂ 2020-05-22 09:52:58
import java.util.*;

class Student implements Comparable {
private int no;
private String name;
private int score;

public Student(int no, String name, int score) {
this.no = no;
this.name = name;
this.score = score;
}

public int getNo() {
return no;
}

public void setNo(int no) {
this.no = no;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no;
}

@Override
public int hashCode() {
return Objects.hash(no);
}

@Override
public String toString() {
return "no:" + this.no + " name:" + this.name + " score:" + this.score;
}
@Override
public int compareTo(Object o) {
return this.no - ((Student)o).getNo();
}
}
public class testmain12{

public static void main(String[] args) {
List <Student> list = new LinkedList<Student>();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < num;++i) {
list.add(new Student (sc.nextInt(),sc.next(),sc.nextInt()));
}
num = sc.nextInt();
for (int i = 0; i < num; ++i) {
String s = sc.next();
switch (s) {
case "add":
list.add(new Student (sc.nextInt(),sc.next(),sc.nextInt()));

case "delete":
int n = sc.nextInt();
int len = list.size();
for (int j = 0; j < len;++j) {
if (list.get(j).getNo() == n)list.remove(j);
break;
}

case "set" :
int nn = sc.nextInt();
int score = sc.nextInt();
int lenl = list.size();
for (int j = 0; j < lenl;++j) {
if (list.get(j).getNo() == nn) {
list.get(j).setScore(score);
break;
}
}
}
}
Object[] objects = list.toArray();
Arrays.sort(objects);
for(int j = 0; j < objects.length; j++) {
System.out.println(objects[j]);
}
/*int l = list.size();
for (int k = 0; k < l; ++k)System.out.println(list.get(i));*/
sc.close();


}

}

编译没问题,运行出错
出错信息:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at testmain12.main(testmain12.java:75)

...全文
419 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhxjtoa 2020-05-26
  • 打赏
  • 举报
回复
Whistleྂ 2020-05-24
  • 打赏
  • 举报
回复
引用 1 楼 yk3251011的回复:
你是在测的时候出问题了吧?nextint不能输字符串类型
查出问题来了,报的输入的错,但是问题出在switch--case语句中,case:后面没加break,导致所有case全部执行,从而读取输入的时候就乱了,蟹蟹大佬的帮助
莫秋生 2020-05-23
  • 打赏
  • 举报
回复
你是在测的时候出问题了吧?nextint不能输字符串类型

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧