因此,爲了做作業,我必須編寫一個程序,打印默認標記爲-
的航班座位表(,即,以表明它們「打開」並符合預訂條件)。隨後,它應該提示用戶想要購買的座位&將陣列中該特定地點的值更改爲X
。在Java中操作二維數組
這是我的代碼看起來像現在 - 它打印出的表格就好了,但是當我嘗試改變位置時,它給我一個錯誤:
import java.util.*;
public class AirlineSeeting {
public static void main(String[]args) {
int row = 0;
int colum = 0;
System.out.println("Enter n:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[][] SeetingChart = new String[n][4];
for (int i = 1; i <= 4; i++) {
System.out.printf("\t%d\t", i);
}
System.out.println("");
for (int j = 1; j <= n; j++) {
System.out.printf("%d", j);
for (int k = 1; k <= 4; k++) {
System.out.print("\t-\t");
}
System.out.println("");
}
for(int i = 0 ; i < SeetingChart.length; i++){
System.out.println("What row would you like to sit in");
row = scanner.nextInt();
System.out.println("What colum would you like to sit in");
colum = scanner.nextInt();
if (SeetingChart[row][colum].equals("X")){
System.out.println("Please pick a seat that is avalable");
}
else if (SeetingChart[row][colum].equals("-")){
SeetingChart[row][colum] = "X";
}
for (int j = 1; j <= 4; j++) {
System.out.printf("\t%d", j);
}
System.out.println("");
for (int j = 1; j <= n; j++) {
System.out.printf("%d", j);
for (int k = 1; k <= 4; k++) {
System.out.print("\t-");
}
System.out.println("");
}
}
}
}
這是我得到的輸出當執行上面的代碼:
Enter n:
9
1 2 3 4
1 - - - -
2 - - - -
3 - - - -
4 - - - -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
What row would you like to sit in
2
What colum would you like to sit in
2
Exception in thread "main" java.lang.NullPointerException
at AirlineSeeting.main(AirlineSeeting.java:30)
任何幫助,將不勝感激! 謝謝!
什麼錯誤它給你? –
「[...]它給了我一個錯誤。」 - >極好的錯誤? – Turing85
我編輯問題 –