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

-3 java arrays

每次我尝试修复错误时,我都有以下任务,它给出了错误的错误.

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

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

  1. 编写语句以使用上面给出的里程数据初始化数组距离.

  2. 编写语句以打印以下菜单,读入两个城市号码,并打印两个城市之间的距离:

要确定城市之间的里程数,请从以下列表中输入两个城市的数量: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 ");
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ima 6

这是固定代码; 说明如下:

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 + ".");

    }

}
Run Code Online (Sandbox Code Playgroud)

基本上,要从用户读取键盘输入,您需要一个Scanner对象:

// This creates the scanner:
Scanner keyboardReader = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)

您看到的错误可能是由于您尝试从对象引用(即指向任何内容的指针)调用方法nextInt()的事实:

// Here, you defined keyboard as being null:
private static final String keyboard = null;

// Then, your code attempts to call a method from no object:
first = keyboard.nextInt();
Run Code Online (Sandbox Code Playgroud)

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

// Add mileage:
 System.out.println("The distance between your two cities is " + mileage + ".");
Run Code Online (Sandbox Code Playgroud)