0

我为我正在从事的项目编写了这个 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...");
   }
}
4

3 回答 3

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.
于 2012-08-25T18:04:11.657 回答
2

在您的代码的两个版本中,您都jfor语句中声明变量:

for (int j = 0; j < i; j++)

当您j以这种方式声明时,它的范围仅限于for循环,并且在循环外无法使用。但是你试图在循环之外使用它。

j如果您想在该循环之后继续使用,请在for循环外声明它:

int j = 0;
for (j = 0; j < i; j++)
于 2012-08-25T18:04:00.470 回答
1

在第一个片段中, j 变量在 for 循环中声明,这意味着它是 for 循环的本地变量。从外面看不到。只需在 for 循环之前声明它:

int j = 0;

接着

for (j = 0; j < i; j++)

第二段代码也是如此。

于 2012-08-25T18:04:54.993 回答