当我运行以下代码(在系统编译器中)时,它运行良好,但是当我将它提交到站点时,所有测试用例都会抛出nzec运行时错误。不幸的是,该网站不共享测试用例,但可以使用问题创建它们。
任何人都可以解释为什么会出现nzec错误吗?我使用过Java,因此代码抛出了需要捕获的异常:
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Readinput1 {
public static int readinput () throws IOException
{
//InputStreamReader r=new InputStreamReader(System.in);
//BufferedReader br=new BufferedReader(r);
int n1=0;
int name=0;
System.out.println("Enter num");
//name=br.read();
// if (name!=null){
// n1=Integer.parseInt(name.trim());
// }
Scanner in = new Scanner(System.in);
name= in.nextInt();
return name;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int num=0;
ArrayList<Integer> al = new ArrayList<Integer>();
while (num!=42)
{
num=readinput();
al.add(num);
}
//Iterator itr=(Iterator) al.iterator();
//while(((java.util.Iterator<Integer>) itr).hasNext()){
al.remove(al.size()-1);
// String num1=itr.toString();
//System.out.println(num1);
for (Integer integer : al) {
System.out.println(integer);
}
}
}
答案 0 :(得分:2)
有任何理由抛出异常并产生所谓的NZCE
。
我没有给你解决方案,但指出了一些关键部分。
int n1=Integer.parseInt(name);
- 请注意name
不是有效的Integer
al.remove(al.size()-1);
- 关于al
大小的内容 0 答案 1 :(得分:-1)
使用异常处理
try{
// code which you think might throw an exception
}catch(Exception t){
// you got the exception. Now what??
}