2016-12-02 122 views
-2

我有一個問題,我的2D陣列其輸出不正確,所以程序運行良好,但是當我選擇案例4查看飛機座位圖橫向輸出到許多x的,而不是在正確的位置。如果有人能指出我的方向真的很棒!java飛機座位圖陣列

public static void viewSeatingChartVertical(boolean seat[]){ 
    for(int i = 0; i < 10; ++i){ 
    for(int j = 0; j < 3; ++j){ 
     if(seat[((j + 1) + (i * 3)) - 1]) 
      System.out.print("x "); 
     else 
      System.out.print("o "); 
     } 
     System.out.println(); 
    } 
} 

public static void viewSeatingChartHorizontal(boolean seat[]){ 
       int [][] twoDim = new int [3][10]; 

       int a = (twoDim.length); 
       int b = (twoDim[0].length); 

       for(int i = 0; i < a; i++){ 
       for(int j = 0; j < b; j++) { 
       int x = 0; 
       twoDim[i][j] = x; 

      if(seat[((j + 1) + (i * 3)) - 1]) 
      System.out.print("x "); 
      else 
      System.out.print("o "); 
     } 
     System.out.println(); 
     } 
     } 


public static void main(String args[]){ 

    java.util.Scanner input = new java.util.Scanner(System.in);// if the system doesnt know the Scanner function it then looks to import it 

    boolean seating[] = new boolean[30];//declare the amount of seats available on the plane in an array 

    //display list of options - saving space make easier to read on smaller screen  
System.out.println("Please choose an option:"); 
System.out.println("1 for 「first class」"); 
System.out.println("2 for 「economy」"); 
System.out.println("3 to view seating chart"); 
System.out.println("4 to view seating horizontally chart"); 
System.out.println("0 to exit"); 
System.out.print("? "); 

    while(true){                       //loop while valid 
    int mOpt = input.nextInt();              //mOpt Menu Option - validate number entered, must be 0-4 
     switch (mOpt){ 
     case 0: System.exit(0); 
      break;                      //system exit 

     case 1:                       // first class seats 
     { 
     System.out.print("Which seat would you like (1-9)\n"); 
     int fcseat = input.nextInt(); 
      if(fcseat > 0 && fcseat <10){ 
       if(seating[fcseat - 1]){ 
       System.out.print("That seat is taken.\n"); 
       } 
       else{ 
       seating[fcseat - 1] = true; 
       System.out.print("Seat number " + fcseat + " was assigned.\n"); 
       } 
      } 
     } 
     break; 

     case 2:                       // economic seats 
     { 
     System.out.print("Which seat would you like (10-30)\n"); 
     int econSeat = input.nextInt(); 
      if(econSeat >= 10 && econSeat <= 30){                 // HAD 31 NOT 30. SMH 
       if(seating[econSeat - 1]){ 
       System.out.print("That seat is taken.\n"); 
       } 
       else{ 
       seating[econSeat - 1] = true; 
       System.out.print("Seat number " + econSeat + " was assigned.\n"); 
       } 
      } 
     } 
     break; 

     case 3:                       //printout of available seats vertically 
     { 
     viewSeatingChartVertical(seating); 
     } 
     break; 

     case 4:                       //printout of available seats horizontally 
     { 
     viewSeatingChartHorizontal(seating); 
     } 
     break; 

     default:                      //wrong format or number please try again 
     System.out.print("Sorry, Option not recognized, please Try again.\n"); 
    }  
    } 
} 
} 

回答

0

您正在屏幕上混合行和列的平面上的行和列。

你的橫向輸出真的太複雜了。它應該和垂直輸出一樣簡單。密鑰是從垂直輸出中複製代碼,但反轉兩個for語句。

for(int j = 0; j < 3; ++j) { 
    for(int i = 0; i < 10; ++i) { 

它應該神奇地工作。

好吧,差不多工作。這將翻轉飛機的左側和右側。對第一個循環的小改動也會解決這個問題。留給學生。

+0

謝謝!我知道了。 – corvo