我能够在Eclipse IDE中成功执行代码,但仅在HackerRank上报错。该错误是由于Scanner.nextInt()引起的,但是为什么hackerrank抛出异常时eclipse成功执行了该程序?
https://www.hackerrank.com/challenges/java-list/problem
Scanner scanner = new Scanner(System.in);
List<Integer> list1=new ArrayList<Integer>();
int n,x,y,d;
n=scanner.nextInt();
for(int i=0;i<n;i++)
list1.add(scanner.nextInt());
int q=scanner.nextInt();
if(q==1)
{
System.out.println("Insert");
x=scanner.nextInt();
y=scanner.nextInt();
list1.add(x,y);
System.out.println(list1);
}
if(q==2)
{
System.out.println("Insert");
x=scanner.nextInt();
y=scanner.nextInt();
list1.add(x,y);
System.out.println("Delete");
d=scanner.nextInt();
list1.remove(d);
System.out.println(list1);
}
scanner.close();
HackerRank Error:
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 Solution.main(Solution.java:27)
Eclipse executes successfully :
5
12
0
1
78
12
1
Insert
0
50
[50, 12, 0, 1, 78, 12]