java初学者:打印字符串中的二维数组元素
java beginner: print two dimensional array element in a String
package arrays;
import java.util.Arrays;
public class Route {
int cityindex;
int stadtwahl;
String[] cities = {"Berlin", "Hamburg", "Kiel", "Muenchen", "Stuttgart", "Dresden", "Heidelberg"};
int stadtwahlA;
int stadtwahlB;
int[][] entfernung;
int A;
int B;
public void Route() {
entfernung = new int[A][B];
this.A = A;
this.B = B;
entfernung[0][1] = entfernung [1][0] = 288;
entfernung[0][2] = entfernung [2][0] = 355;
entfernung[0][3] = entfernung [3][0] = 585;
entfernung[0][4] = entfernung [4][0] = 632;
entfernung[0][5] = entfernung [5][0] = 194;
entfernung[0][6] = entfernung [6][0] = 628;
}
public void einlesen() {
System.out.println("Sie haben folgende Staedte zur Auswahl: "
+ "\n0: " + cities[0] //Berlin
+ "\n1: " + cities[1] //Hamburg
+ "\n2: " + cities[2] //Kiel
+ "\n3: " + cities[3] //Muenchen
+ "\n4: " + cities[4] //Stuttgart
+ "\n5: " + cities[5] //Dresden
+ "\n6: " + cities[6] + "\n"); //Heidelberg
System.out.println("Bitte waehlen Sie eine Stadt: ");
stadtwahlA = Konsole.getInputInt();
System.out.println("Bitte waehlen Sie ein Ziel: ");
stadtwahlB = Konsole.getInputInt();
}
public void ausgabe() {
System.out.println("Die Entfernung zwischen " + cities[stadtwahlA] + " und " + cities[stadtwahlB] + " betraegt : " );
this.A = stadtwahlA;
this.B = stadtwahlB;
System.out.print(entfernung[0][1]);
}
}
我的想法是打印一些城市之间的距离 (entfernung)。
所以出于测试原因,我刚刚输入了城市 0(柏林)和其他城市之间的距离。最后我想写
System.out.print(entfernung[stadtwahlA][stadtwahlB]);
或者至少这对我来说似乎是最好的选择 - 但出于某种原因,当 运行 此代码时我得到一个空指针异常:
Sie haben folgende Staedte zur Auswahl: 0: Berlin 1: Hamburg 2: Kiel
3: Muenchen 4: Stuttgart 5: Dresden 6: Heidelberg
Bitte waehlen Sie eine Stadt: 1 Bitte waehlen Sie ein Ziel: 5 Die
Entfernung zwischen Hamburg und Dresden betraegt : Exception in
thread "main" java.lang.NullPointerException at
arrays.Route.ausgabe(Route.java:70) at
arrays.RouteTest.main(RouteTest.java:8)
因为我是新手,所以我非常感谢任何帮助,尽可能简单地解释我的错误;)提前致谢!
编辑:这是我的测试class:
package arrays;
public class RouteTest {
public static void main(String[] args) {
Route R1 = new Route();
R1.einlesen();
R1.ausgabe();
}
}
你遇到的问题是因为你在 Route 中的构造函数前面有 void。所以 entfernung 永远不会被初始化。为空。
改变
public void Route() {
对此
public Route() {
您将遇到的下一个错误是 ArrayIndexOutOfBoundsException,因为您的构造函数正在声明一个 0 x 0 大小的数组,然后您试图引用该数组中不存在的 [0][1] 元素。那是因为你还没有初始化 A 和 B 变量。我会把它留给你作为练习。
package arrays;
import java.util.Arrays;
public class Route {
int cityindex;
int stadtwahl;
String[] cities = {"Berlin", "Hamburg", "Kiel", "Muenchen", "Stuttgart", "Dresden", "Heidelberg"};
int stadtwahlA;
int stadtwahlB;
int[][] entfernung;
int A;
int B;
public void Route() {
entfernung = new int[A][B];
this.A = A;
this.B = B;
entfernung[0][1] = entfernung [1][0] = 288;
entfernung[0][2] = entfernung [2][0] = 355;
entfernung[0][3] = entfernung [3][0] = 585;
entfernung[0][4] = entfernung [4][0] = 632;
entfernung[0][5] = entfernung [5][0] = 194;
entfernung[0][6] = entfernung [6][0] = 628;
}
public void einlesen() {
System.out.println("Sie haben folgende Staedte zur Auswahl: "
+ "\n0: " + cities[0] //Berlin
+ "\n1: " + cities[1] //Hamburg
+ "\n2: " + cities[2] //Kiel
+ "\n3: " + cities[3] //Muenchen
+ "\n4: " + cities[4] //Stuttgart
+ "\n5: " + cities[5] //Dresden
+ "\n6: " + cities[6] + "\n"); //Heidelberg
System.out.println("Bitte waehlen Sie eine Stadt: ");
stadtwahlA = Konsole.getInputInt();
System.out.println("Bitte waehlen Sie ein Ziel: ");
stadtwahlB = Konsole.getInputInt();
}
public void ausgabe() {
System.out.println("Die Entfernung zwischen " + cities[stadtwahlA] + " und " + cities[stadtwahlB] + " betraegt : " );
this.A = stadtwahlA;
this.B = stadtwahlB;
System.out.print(entfernung[0][1]);
}
}
我的想法是打印一些城市之间的距离 (entfernung)。 所以出于测试原因,我刚刚输入了城市 0(柏林)和其他城市之间的距离。最后我想写
System.out.print(entfernung[stadtwahlA][stadtwahlB]);
或者至少这对我来说似乎是最好的选择 - 但出于某种原因,当 运行 此代码时我得到一个空指针异常:
Sie haben folgende Staedte zur Auswahl: 0: Berlin 1: Hamburg 2: Kiel 3: Muenchen 4: Stuttgart 5: Dresden 6: Heidelberg
Bitte waehlen Sie eine Stadt: 1 Bitte waehlen Sie ein Ziel: 5 Die Entfernung zwischen Hamburg und Dresden betraegt : Exception in thread "main" java.lang.NullPointerException at arrays.Route.ausgabe(Route.java:70) at arrays.RouteTest.main(RouteTest.java:8)
因为我是新手,所以我非常感谢任何帮助,尽可能简单地解释我的错误;)提前致谢!
编辑:这是我的测试class:
package arrays;
public class RouteTest {
public static void main(String[] args) {
Route R1 = new Route();
R1.einlesen();
R1.ausgabe();
}
}
你遇到的问题是因为你在 Route 中的构造函数前面有 void。所以 entfernung 永远不会被初始化。为空。
改变
public void Route() {
对此
public Route() {
您将遇到的下一个错误是 ArrayIndexOutOfBoundsException,因为您的构造函数正在声明一个 0 x 0 大小的数组,然后您试图引用该数组中不存在的 [0][1] 元素。那是因为你还没有初始化 A 和 B 变量。我会把它留给你作为练习。