2017-05-28 68 views
0

我的代碼返回Runtime error,但我不知道如何解決這個問題。這個代碼是關於問題104.我試圖改變一些東西,但沒有...我讀了關於這個錯誤,基本上是與掃描儀相關聯。 請幫幫我!這樣運行時錯誤UVA

Scanner input = new Scanner(System.in); 

TO

Scanner input = new Scanner(new FileInputStream(args[0])); 

我recommad你

import java.util.Scanner; 

public class Main { 

    static int MAX = 20; 
    static double LUCRO_MIN = 1.01; 

    public static void main(String[] args) { 
     Scanner sc = new Scanner (System.in); 
     int n, i, j; 
     double v; 
     while(sc.hasNext()) { 
      double[][] W = new double [MAX][MAX]; 
      n = sc.nextInt(); 
      sc.nextLine(); 
      for(i = 0; i < n; i++) { 
       int pos = 0; 
       String linha = sc.nextLine(); 
       String vertice[] = linha.split(" "); 
       for(j = 0; j < n; j++) { 
        if(i == j) { 
         W[i][i] = 0; 
         continue; 
        } 
        v = Double.parseDouble(vertice[pos]); 
        W[i][j] = v; 
        pos++; 
      } 
      } 
     Converte(n, W); 
     } 
    } 

    static public void Imprime (int i, int j, int l, int P[][][]) { 
     if(l == 0) 
     System.out.printf ("%d\n", i + 1); 
     else { 
     System.out.printf ("%d ", i + 1); 
     Imprime (P[l][i][j], j, l - 1, P); 
     } 
    } 

    static void IniZero (int n, double m[][][], int l) { 
     int i, j; 
     for(i = 0; i < n; i++) 
     for(j = 0; j < n; j++) 
      m[l][i][j] = 0; 
    } 

    static void Converte(int n, double W[][]) { 
     double[][][] B = new double [MAX][MAX][MAX]; 
     int [][][]P = new int [MAX][MAX][MAX]; 
     int l, i, j, k; 
     for(i = 0; i < n; i++) 
     for(j = 0; j < n; j++) { 
      B[1][i][j] = W[i][j]; 
      if(W[i][j] > 0) 
      P[1][i][j] = j; 
     } 
     for(l = 2; l <= n; l++) { 
     IniZero(n, B, l); 
     for(i = 0; i < n; i++) 
      for(j = 0; j < n; j++) 
      for(k = 0; k < n; k++) { 
       if(B[l][i][j] < W[i][k] * B[l - 1][k][j]) { 
       B[l][i][j] = W[i][k] * B[l - 1][k][j]; 
       P[l][i][j] = k; 
       if(B[l][i][i] >= LUCRO_MIN) { 
        Imprime(i, i, l, P); 
        return; 
       } 
       } 
      } 
     } 
     System.out.printf("no arbitrage sequence exists\n"); 
    } 
} 

回答

0

更改掃描儀使用input.hasNext()代替input.nextInt();這可能會導致nullpointerexception

+0

我改變,但不要解決不了我的問題。仍然運行時錯誤 – Juny