2016-03-21 71 views
-2

我最近在競爭性編程中從C轉移到了Java。但是我提交的任何解決方案都會顯示NZEC運行時錯誤。一個這樣的問題是https://www.codechef.com/problems/FCTRL2 和我的解決辦法是爲什麼這段代碼不工作? FCTRL2

import java.util.Scanner; 
import java.math.BigInteger; 

class Solution{ 
    public int t, i=0; 
    public BigInteger N; 
    public static void main(String args[]){ 
     Solution sol = new Solution(); 
     sol.scanT(); 
     sol.testCase(); 
     System.exit(0); 
    } 

    public void scanT(){ 
     Scanner sc = new Scanner(System.in); 
     t = sc.nextInt(); 
     if(t>100 || t<1){ 
      return; 
     } 
    } 

    public void testCase(){ 
     Scanner sc = new Scanner(System.in); 
     for(i=0; i<t; i++){ 
      N = sc.nextBigInteger(); 
      if(N.compareTo(BigInteger.ONE)<0 || N.compareTo(BigInteger.valueOf(100))>0){ 
       return; 
      } 
      BigInteger z = factorial(); 
      System.out.println(z); 
     } 

    } 

    public BigInteger factorial(){ 
     BigInteger Fact = N; 
     while(N.compareTo(BigInteger.valueOf(2))>0){ 
      Fact = Fact.multiply(N.subtract(BigInteger.ONE)); 
      N = N.subtract(BigInteger.ONE); 
     } 
    return Fact; 
    } 
} 

請幫我找到我的解決方案的錯誤,導致運行錯誤NZEC每次。我的解決方案在計算機上運行時顯示正確的輸出。

+1

尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建一個最小,完整和可驗證的示例。](http://stackoverflow.com/help/mcve) – user7

+0

不確定問題是什麼,但爲什麼要創建多個掃描程序? – Ramanlfc

+0

嘗試創建只有一個掃描儀 – user7

回答

1

NZEC錯誤是由於同時使用System.in的多個Scanner對象而生成的。 僅使用一個掃描儀對象可解決運行時錯誤NZEC的問題。

相關問題