2013-08-24 2 views
1

Java 도움말.Java 열과 행

화면에 좌석표를 인쇄하는 프로그램을 작성하고 은 사용자에게 좌석 또는 가격을 선택하라는 메시지를 표시합니다. 에 의해 좌석을 팔아서 가격을 0으로 변경하십시오. 사용자가 좌석을 지정하면 을 사용할 수 있는지 확인하십시오. 사용할 수없는 경우 사용자에게 알려서 에게 좌석 또는 가격을 선택하도록 요청하십시오.

교사는 A-I과 열이 난 내 교양 학교에서이 고급 자바를 배울하지 않았기 때문에 내가이 얻을 수 없습니다 1-10로 수 행을 원한다. 또한 선생님은 내가 할 수없는 Stage를 출력하기를 원합니다. 당신이 참조를 정의 할 때

public class TheatreSeating { 
    public static void main(String[] args) { 


     // Two dimensional array to hold seats 
     int[10][9] seatsArray = 
       {{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 }, 
       { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 }, 
       { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 }, 
       { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
       { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
       { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
       { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, 
       { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, 
       { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }}; 
     String continueFlag = "Y"; 
     Scanner input = new Scanner(System.in); 
     while (continueFlag.equals("Y") || continueFlag.equals("y")) { 
      System.out.println("Please Select an option"); 
      System.out.println("1. Select Seat"); 
      System.out.println("2. Select Price"); 

      // Get selected option 
      int option = input.nextInt(); 
      if (option == 1) { 
       System.out 
         .println("Please enter the row number of seat (1-10):"); 
       // Subtract 1 as array starts from 0 
       char row = input.nextInt() - 1; 
       System.out 
         .println("Please enter the column number of seat (1-10):"); 
       // Subtract 1 as array starts from 0 
       int column = input.nextInt() - 1; 
       boolean seatAvailable = isSeatAvailable(seatsArray, row, column); 
       if (seatAvailable) { 
        // Assign Seat 
        seatsArray[row][column] = 0; 
       } else { 
        System.out.println("Requested seat is not available"); 
       } 
      } else if (option == 2) { 
       System.out 
         .println("Please enter the Price(10,20,30,40 or 50):"); 
       int price = input.nextInt(); 
       // Check available seat 
       boolean found = false; 
       for (int i = 0; i < seatsArray.length; i++) { 
        // Continue looking only if not found 
        if (found == false) { 
         for (int j = 0; j < seatsArray[i].length; j++) { 
          if (seatsArray[i][j] == price) { 
           // Assign the seat 
           seatsArray[i][j] = 0; 
           found = true; 
           // Exit from inner loop 
           break; 
          } 
         } 
        } 
       } 
      } 
      printSeats(seatsArray); 
      System.out.println("Do you want to enter more seats (Y/N)"); 
      continueFlag = input.next(); 
     } 
     System.out.println("Bye"); 
    } 

    private static void printSeats(int[][] seatsArray) { 
     for (int i = 0; i < seatsArray.length; i++) { 
      for (int j = 0; j < seatsArray[i].length; j++) { 
       System.out.print(seatsArray[i][j] + " "); 
      } 
      System.out.println(); 
     } 
    } 

    private static boolean isSeatAvailable(int[][] seatsArray, int row, 
      int column) { 
     for (int i = 0; i < seatsArray.length; i++) { 
      for (int j = 0; j < seatsArray[i].length; j++) { 
       if (i == row && j == column && seatsArray[i][j] != 0) { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 
} 
+1

정확히 문제는 무엇인가? 그것은 심지어 컴파일합니까? –

+0

그것은 그냥 힘든 시간이 있도록 행 수 코드와 오는 데 컴파일 (A-I)과 열 (1-10) 필자는 모든 노력을 표시하고 난 내 코드에 넣어하려고 할 때 그것을 엉망으로 유지합니다. 또한 상단에 단어 무대를 출력해야합니다. – Joy

+0

'int [10] [9] seatsArray = .....;'컴파일합니까? 또한 –

답변

0

당신은 배열의 크기를 지정하지 마십시오. 직접 초기화하고 아무 곳이나 크기를 지정할 필요가 없기 때문에

int[10][9] twoDArray; // ERROR! 

당신은 당신이

int[][] twoDArray = new int[10][9]; 

new 키워드를 사용하여 메모리를 할당 할 때 크기를 지정하지만.

int[][] twoDArray = { {0,0}, {1,1} }; // same as 
int[][] twoDArray = new int[][] { {0,0}, {1,1} }; // but 
int[][] twoDArray = new int[10][9] { {0,0}, {1,1} }; // ERROR! 

또한 char에 직접 int 식을 할당 할 수 없습니다. 당신은 컴파일러가 당신이 작은 char 데이터 타입으로, int, 더 큰 데이터 형식을 때우는하고 있기 때문에 당신이 거기에 약간의 정밀도를 잃을 수 있다는 것을 이해할 수 있음을 알려이 명시 적 캐스트 ()이 필요합니다.

char row = (char) (input.nextInt() - 1); 

, 당신은 전혀 거기 char를 사용해서는 안된다. column에있는 것처럼 int으로 변경하십시오.

int row = input.nextInt() - 1; 
+0

작업 didnt는 - 1; – Joy

+0

내가 묻는 것을 꺼리지 않으면 어떻게하면 정밀도를 잃지 않을 수 있겠습니까? – Joy

+0

아, 그것은'char row = (char) (input.nextInt() - 1);'이어야합니다. 그렇지 않으면'nextInt()'에서 반환 된'int'는'char'가됩니다. 그러나 여러분이 1을 빼면 다시'int'가됩니다. –