为什么CodeChef的编译器抱怨我的代码需要抛出异常?

时间:2014-10-03 13:17:57

标签: java exception-handling

当我尝试在CodeChef的编译器中运行代码时,它说我需要抛出某种异常。但是,Eclipse和JRE 8u20的代码运行良好。这是为了解决http://www.codechef.com/problems/SNAKY的问题。有人能帮助我吗?

我收到的信息是:

  

NZEC代表非零退出代码。对于C用户,如果主方法没有返回0,则会生成此用户;声明。其他语言如Java / C ++如果抛出异常会产生此错误

这是我的代码:

import java.util.Scanner;
public class Snake {
int N, M, x, y, L;
String path;
public Snake(int N, int M, int x, int y, int L,String path) {
    this.N=N;
    this.M=M;
    this.x=x;
    this.y=y;
    this.L=L;
    this.path=path;
}

public void snaky(){
    int[][] area =new int[M][N];
    for(int i=0;i<M;i++)
        for(int j1=0;j1<N;j1++)
            area[i][j1]=0;
    int j=2;
    Snake[] s = new Snake[L];
    for(int i=0;i<L;i++)
        s[i] = new Snake(0,0,0,0,0,path);
    s[L-1] = new Snake(0,0,x,y,0,path);
    area[x][y]=1;
    char[] moves = path.toCharArray();
    int k=0;
    while(j<=L){
        char turn=moves[k++];
        if(turn=='U'){
            s[L-j++] = new Snake(0,0,x-1,y,0,path);
            area[x-1][y]=1;
            x=x-1;
            continue;
        }
        if(turn=='D'){
            s[L-j++] = new Snake(0,0,x+1,y,0,path);
            area[x+1][y]=1;
            x=x+1;
            continue;
        }
        if(turn=='L'){
            s[L-j++] = new Snake(0,0,x,y-1,0,path);
            area[x][y-1]=1;
            y=y-1;
            continue;
        }
        if(turn=='R'){
            s[L-j++] = new Snake(0,0,x,y+1,0,path);
            area[x][y+1]=1;
            y=y+1;
            continue;
        }

    }   
    char last = moves[L-2];
    int count=1;
    if(last=='U'){
            while(x>0){
                    if(area[x-1][y]==0){
                        area[x-1][y]=1;
                        area[s[L-count].x][s[L-count].y]=0;
                        count++;
                        x=x-1;
                    }
                    else{
                        System.out.println("BODY"+" "+(count-1));
                        return;
                    }
            }
            System.out.println("WALL"+" "+(count-1));
    }
    if(last=='D'){
        while(x<M){
                if(area[x+1][y]==0){
                    area[x+1][y]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    x=x+1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }
    if(last=='L'){
        while(y>0){
                if(area[x][y-1]==0){
                    area[x][y-1]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    y=y-1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }
    if(last=='R'){
        while(y<N){
                if(area[x][y+1]==0){
                    area[x][y+1]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    y=y+1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }   
}

public static void main(String[] args){
    Scanner reader = new Scanner(System.in);
    int T = reader.nextInt();
    Snake[] s = new Snake[T];
    for(int i=0;i<T;i++){
        int N = reader.nextInt();
        int M = reader.nextInt();
        int x = reader.nextInt();
        int y = reader.nextInt();
        int L = reader.nextInt();
        String path = reader.next();
        s[i] = new Snake(N,M,M-y,x-1,L,path);
    }
    for(int i=0;i<T;i++){
        s[i].snaky();
    }
    reader.close();
}

}

1 个答案:

答案 0 :(得分:0)

错误似乎在告诉您,在某些情况下,您的代码会抛出异常。很明显,比赛希望你避免这种情况。

一个简单的解决方案是将整个main方法包装在try / catch中,并在最后发布一些消息。

public static void main(String[] args){
  try {
    // original code
  } catch (Throwable t) {
    t.printStackTrace();
  }
}

更好的解决方案是弄清楚你在什么情况下抛出异常并尽可能避免它。这需要分析你的代码和潜在的问题 - 我不会为你做的事。