每次我尝试修复错误时,我都会执行以下操作,但错误导致错误。
给出以下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 ");
}
}
答案 0 :(得分: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 + ".");
}
}
基本上,要读取用户的键盘输入,您需要扫描仪对象:
// This creates the scanner:
Scanner keyboardReader = new Scanner(System.in);
您看到的错误可能是由于您尝试从 null 对象引用(即指向任何内容的指针)调用方法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();
最后,不要忘记在最后得到的字符串中添加里程数:
// Add mileage:
System.out.println("The distance between your two cities is " + mileage + ".");
答案 1 :(得分:1)
看起来你最后没有打印里程。尝试将代码的最后一行更改为:
System.out.println("The distance between your two cities is "+mileage);
答案 2 :(得分:0)
更改代码,
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);
}
答案 3 :(得分:-1)
使用应该定义keyboard.Use Scanner
class。
试试这个 import 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);
}
}