我正在尝试 使用链表解决这个问题https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem?h_r=next-challenge&h_v=zen并在 eclipse 中成功解决了所有输出正确,如hackerrank 输出,但是当我尝试在网站上上传我的代码时,它显示 InputMismatchException 的运行时错误线程“main”中的异常 java.util.InputMismatchExceptionat java.util.Scanner.throwFor(Scanner.java:909)at java.util.Scanner.next(Scanner.java:1530)at java.util.Scanner.nextInt( Scanner.java:2160)在 java.util.Scanner.nextInt(Scanner.java:2119)在 Person.main(Person.java:56)这是我在课堂上的代码public class Person { //Node Structure class Node { public String name; public int phone; Node next; }; public Node head; //Linked List Functions public void add(String name , int phone ) //Add To End { Node n = new Node(); n.name = name; n.phone = phone; n.next = head; head = n; } public void search(String name2) //Search inside The List { Node n = head; boolean flag = false; while(n != null) { if(name2.equals(n.name)) { flag = true; System.out.println(n.name + "=" + n.phone); break; } n = n.next; } if(!flag) System.out.println("Not found"); } //Main Function public static void main(String[] args) { //Objects From Classes Scanner s = new Scanner(System.in); Person p = new Person(); int n = s.nextInt(); for(int i=0 ; i<n ; i++) { s.nextLine(); String name = s.nextLine(); int phone = s.nextInt(); p.add(name, phone); } s.nextLine(); while(s.hasNext()) { String name2 = s.next(); p.search(name2); //System.exit(1); } s.close();}
添加回答
举报
0/150
提交
取消