我的代码正在返回Runtime error
,但我不知道如何解决这个问题。这段代码是关于问题104.我试图改变一些事情,但没有...我读到了这个错误,基本上与扫描仪有关。
请帮帮我!
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 :(得分:0)
像这样更换扫描仪
Scanner input = new Scanner(System.in);
以强>
Scanner input = new Scanner(new FileInputStream(args[0]));
我建议您使用input.hasNext()
代替input.nextInt();
,这可能导致nullpointerexception