2014-10-03 63 views
-2

當我嘗試在CodeChef的編譯器中運行代碼時,它說我需要拋出某種異常。但是,Eclipse和JRE 8u20的代碼運行良好。這是爲了解決http://www.codechef.com/problems/SNAKY的問題。有誰能夠幫助我?爲什麼CodeChef的編譯器會抱怨我的代碼需要拋出異常?

我收到的消息是:

NZEC代表非零退出代碼。對於C用戶,如果你的main方法沒有返回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(); 
} 

} 
+0

那麼它說你需要拋出一個異常呢? – msrd0 2014-10-03 13:22:15

+0

它真的說「某種例外」嗎?你能給我們確切的錯誤信息嗎? – 2014-10-03 13:22:43

+0

@ msrd0 NZEC代表非零退出代碼。對於C用戶,如果你的main方法沒有返回0,將會產生這個;聲明。其他語言如Java/C++如果拋出異常,則可能會產生此錯誤。 – Daedalus 2014-10-03 13:24:48

回答

0

的錯誤似乎在告訴大家的是,在某些情況下,你的代碼會拋出一個例外。顯然,競爭要你避免這種情況。

一個簡單的解決方案是將您的整個main方法包裝在try/catch中,並在最後發佈一些消息。

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

一個更好的解決方法是弄清楚在什麼情況下拋出異常並儘量避免它。這需要分析你的代碼和潛在的問題 - 我不打算爲你做。

相關問題