我为我正在从事的项目编写了这个 java 代码。我已经尽我所能,但仍然无法让它工作,所以我想我会在这里尝试。
该程序应该使用 aJOptionPane
来询问用户他想看到的城市之间的里程距离。这些数字来自 .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...");
}
}