2017-05-28 22 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