有人能帮我修复这个Java代码吗?

有人能帮我修复这个Java代码吗?,java,arrays,Java,Arrays,每次我试图修复一个错误时,它都会给我一个错误接一个错误 给定以下5行5列的数组,其中包含 城市之间的距离: 请注意,城市名称不在数组中;数组包含 仅限数值项,它们给出了两个值之间的距离 由行和列表示的城市 编写语句以使用 上述里程数据 编写语句以打印以下菜单,分两部分阅读 城市编号,并打印两个城市之间的距离: 要确定城市之间的里程数,请输入里程数 以下列表中的两个城市: 1:奥尔巴尼4:纽约 2:波士顿5:费城 3:哈特福德 输入您的城市号码: **测试用例:奥尔巴尼和纽约之间的距离&哈特福德和

每次我试图修复一个错误时,它都会给我一个错误接一个错误

给定以下5行5列的数组,其中包含 城市之间的距离:

请注意,城市名称不在数组中;数组包含 仅限数值项,它们给出了两个值之间的距离 由行和列表示的城市

  • 编写语句以使用 上述里程数据

  • 编写语句以打印以下菜单,分两部分阅读 城市编号,并打印两个城市之间的距离:

  • 要确定城市之间的里程数,请输入里程数 以下列表中的两个城市: 1:奥尔巴尼4:纽约 2:波士顿5:费城 3:哈特福德

    输入您的城市号码:

    **测试用例:奥尔巴尼和纽约之间的距离&哈特福德和菲拉之间的距离

    public class DistanceofCities {
    
    private static final String keyboard = null;
    
    public static void main(String[] args) {
    
        int [][] distance = {
                {0, 171, 115, 141, 240},
                {171, 0, 103, 194, 333},
                {115, 103, 0, 120, 235},
                {141, 194, 120, 0, 104},
                {240, 333, 235, 104, 0}
    
        };
    
        int mileage;
        int first = -1, second = -1;
    
           System.out.println("To determine the mileage between cities, enter the \n");
           System.out.println("numbers of two cities from the following list:\n");
           System.out.println("      1:  Albany           4:  NY\n");
           System.out.println("      2:  Boston           5:  Phila\n");
           System.out.println("      3:  Hartford\n\n");
           System.out.println("Enter your city numbers ==> ");
           first = keyboard.nextInt();
           second = keyboard.nextInt();
           mileage = distance[first-1][second-1];
           System.out.println("The distance between your two cities is ");
        }
    }
    

    看起来你没有打印最后的里程数。尝试将代码的最后一行更改为:

    System.out.println("The distance between your two cities is "+mileage);
    

    使用应定义键盘。使用
    扫描仪

    试试这个 导入java.util.Scanner

    public class DistanceofCities {
    
    
    
    public static void main(String[] args) {
    
        int [][] distance = {
                {0, 171, 115, 141, 240},
                {171, 0, 103, 194, 333},
                {115, 103, 0, 120, 235},
                {141, 194, 120, 0, 104},
                {240, 333, 235, 104, 0}
    
        };
    
        int mileage;
        int first = -1, second = -1;
           Scanner keyboard= new Scanner(System.in);
           System.out.println("To determine the mileage between cities, enter the \n");
           System.out.println("numbers of two cities from the following list:\n");
           System.out.println("      1:  Albany           4:  NY\n");
           System.out.println("      2:  Boston           5:  Phila\n");
           System.out.println("      3:  Hartford\n\n");
           System.out.println("Enter your city numbers ==> ");
           first = keyboard.nextInt();
           second = keyboard.nextInt();
           mileage = distance[first-1][second-1];
           System.out.println("The distance between your two cities is "+ mileage);
        }
    }
    

    这是固定代码;解释如下:

    import java.util.Scanner;
    
    public class DistanceOfCities {
    
        public static void main(String[] args) {
    
            Scanner keyboardReader = new Scanner(System.in);
    
            int[][] distance = {
                    {0, 171, 115, 141, 240},
                    {171, 0, 103, 194, 333},
                    {115, 103, 0, 120, 235},
                    {141, 194, 120, 0, 104},
                    {240, 333, 235, 104, 0}
    
            };
    
            int mileage;
            int first = -1, second = -1;
    
            System.out.println("To determine the mileage between cities, enter the \n");
            System.out.println("numbers of two cities from the following list:\n");
            System.out.println("      1:  Albany           4:  NY\n");
            System.out.println("      2:  Boston           5:  Phila\n");
            System.out.println("      3:  Hartford\n\n");
            System.out.println("Enter your city numbers ==> ");
            first = keyboardReader.nextInt();
            second = keyboardReader.nextInt();
            mileage = distance[first - 1][second - 1];
            System.out.println("The distance between your two cities is " + mileage + ".");
    
        }
    
    }
    
    基本上,要从用户处读取键盘输入,您需要一个扫描仪对象:

    // This creates the scanner:
    Scanner keyboardReader = new Scanner(System.in);
    
    您看到的错误可能是由于您试图从null对象引用调用方法nextInt()(即指向nothing的指针):

    最后,不要忘记将里程数添加到最后得到的字符串中:

    // Add mileage:
     System.out.println("The distance between your two cities is " + mileage + ".");
    
    更改代码如下:

    public static void main(String[] args) {
    
                int [][] distance = {
                        {0, 171, 115, 141, 240},
                        {171, 0, 103, 194, 333},
                        {115, 103, 0, 120, 235},
                        {141, 194, 120, 0, 104},
                        {240, 333, 235, 104, 0}
    
                };
    
                int mileage;
                int first = -1, second = -1;
                Scanner in = new Scanner(System.in);
                   System.out.println("To determine the mileage between cities, enter the \n");
                   System.out.println("numbers of two cities from the following list:\n");
                   System.out.println("      1:  Albany           4:  NY\n");
                   System.out.println("      2:  Boston           5:  Phila\n");
                   System.out.println("      3:  Hartford\n\n");
                   System.out.println("Enter your city numbers ==> ");
                   first = in.nextInt();
                   second = in.nextInt();
                   mileage = distance[first-1][second-1];
                   System.out.println("The distance between your two cities is "+mileage);
                }
    

    请张贴错误;很难诊断一堆代码。您正在尝试的输入数字是什么。很可能,您正在尝试在两个输入数字之间输入一个enter键,但未经检查就直接转换为int。非常感谢您的帮助。最有可能的错误是编译器错误,导致String没有名为nextInt()的方法。
    public static void main(String[] args) {
    
                int [][] distance = {
                        {0, 171, 115, 141, 240},
                        {171, 0, 103, 194, 333},
                        {115, 103, 0, 120, 235},
                        {141, 194, 120, 0, 104},
                        {240, 333, 235, 104, 0}
    
                };
    
                int mileage;
                int first = -1, second = -1;
                Scanner in = new Scanner(System.in);
                   System.out.println("To determine the mileage between cities, enter the \n");
                   System.out.println("numbers of two cities from the following list:\n");
                   System.out.println("      1:  Albany           4:  NY\n");
                   System.out.println("      2:  Boston           5:  Phila\n");
                   System.out.println("      3:  Hartford\n\n");
                   System.out.println("Enter your city numbers ==> ");
                   first = in.nextInt();
                   second = in.nextInt();
                   mileage = distance[first-1][second-1];
                   System.out.println("The distance between your two cities is "+mileage);
                }