我为我正在处理的项目编写了这个java代码。我尽可能地走了,但仍然无法让它工作,所以我想我会在这里尝试。
该程序应该使用JOptionPane
来询问用户他希望看到的城市里程距离。这些数字来自.txt文件。
我总是得到一个“j”无法找到的错误。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Mileage
{
public static void main(String[] paramArrayOfString)
throws IOException
{
String str3 = "";
Scanner localScanner = new Scanner(new File(JOptionPane.showInputDialog("Enter the name of the data file.")));
String str1 = localScanner.nextLine() + " Cities:\n\n";
int i = localScanner.nextInt();
ArrayList localArrayList = new ArrayList(i);
int[][] arrayOfInt = new int[i][i];
for (int j = 0; j < i; j++)
{
localScanner.nextLine();
localArrayList.add(localScanner.nextLine());
for (int k = 0; k < i; k++)
{
arrayOfInt[j][k] = localScanner.nextInt();
}
}
j = 1 ;
for (String str4 : localArrayList)
{
str1 = str1 + j++ + ". " + str4 + "\n"; str1 = str1 + "\nEnter from and to cities for mileage, \"Quit\" or \"quit\" to quit.";
String str2;
}
do
{
str2 = JOptionPane.showInputDialog(str3 + str1);
if (str2.equalsIgnoreCase("quit"))
{
continue;
}
localScanner = new Scanner(str2);
int m = localScanner.nextInt();
int n = localScanner.nextInt();
if ((m < 1) || (m > i) || (n < 1) || (n > i))
str3 = "Invalid values, try again.\n\n";
else {
str3 = "The distance between " + (String)localArrayList.get(m - 1) + " and " + (String)localArrayList.get(n - 1) + " is " + arrayOfInt[(m - 1)][(n - 1)] + " miles.\n\n";
}
}
while (!str2.equalsIgnoreCase("quit"));
}
}
这是我尝试过的另一种形式,很少有不同,但两者都有相同的错误:
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class MileageTest
{
public static void main(String[] paramArrayOfString)
throws IOException
{
String str5 = "";
String str1 = JOptionPane.showInputDialog("Enter the name of the data file.");
Scanner localScanner = new Scanner(new File(str1));
String str2 = localScanner.nextLine();
int i = localScanner.nextInt();
ArrayList localArrayList = new ArrayList(i);
int[][] arrayOfInt = new int[i][i];
for (int j = 0; j < i; j++)
{
localScanner.nextLine();
localArrayList.add(localScanner.nextLine());
for (int k = 0; k < i; k++)
{
arrayOfInt[j][k] = localScanner.nextInt();
}
}
String str3 = str2 + " Cities:\n\n";
j = 1;
for (String str6 : localArrayList)
str3 = str3 + j++ + ". " + str6 + "\n";
str3 = str3 + "\nEnter from and to cities for mileage, \"Quit\" or \"quit\" to quit.";
String str4;
do {
str4 = JOptionPane.showInputDialog(str5 + str3);
if (str4.equalsIgnoreCase("quit"))
continue;
localScanner = new Scanner(str4);
int m = localScanner.nextInt();
int n = localScanner.nextInt();
if ((m < 1) || (m > i) || (n < 1) || (n > i))
str5 = "Invalid values, try again.\n\n";
else
{
str5 = "The distance between " + (String)localArrayList.get(m - 1) + " and " + (String)localArrayList.get(n - 1) + " is " + arrayOfInt[(m - 1)][(n - 1)] + " miles.\n\n";
}
}
while (!str4.equalsIgnoreCase("quit"));
test();
}
private static void test()
{
System.out.println("Testing...");
}
}
答案 0 :(得分:3)
for (int j = 0; j < i; j++)
{
localScanner.nextLine();
localArrayList.add(localScanner.nextLine());
for (int k = 0; k < i; k++)
{
arrayOfInt[j][k] = localScanner.nextInt();
}
}
j = 1 ; <--- THIS IS THE PROBLEM. j does not exist at this scope.
答案 1 :(得分:2)
在您的代码的两个版本中,您在j
语句中声明变量for
:
for (int j = 0; j < i; j++)
当您以这种方式声明j
时,它的范围仅限于for
循环,并且在循环外部不可用。但是你试图在循环之外使用它。
如果您希望在j
循环之后继续使用for
,请在循环外声明它:
int j = 0;
for (j = 0; j < i; j++)
答案 2 :(得分:1)
在第一个片段中,j变量在for循环内声明,这意味着它是for循环的本地。从外面看不到它。只需在for循环之前声明它:
int j = 0;
然后
for (j = 0; j < i; j++)
上面的第二段代码也是如此。