2012-11-07 3 views
2

"public static int [] [] sellSeatByPrice"내 매개 변수가 무엇인지 알아낼 수 없습니다. 나는 사용자에게 (좌석의) 가격을 물어볼 필요가있다. 가격이 0이라면 그 가격을 계산하고, 그렇지 않다면 0으로 지정해야한다.메서드에서 메인으로 매개 변수를 전달

아래 코드는 도움이된다. !

import java.util.Scanner; 
/** 
* Write a description of class A10_TheaterSeating here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class A10_TheaterSeating 
{ 
public static void main(String args[]) 
{ 
    System.out.println("***Welcome to the Ticket Choice App!***"); 
    System.out.println(); 

    int[][] theaterSeats = //set values in seating chart array 
    { 
     {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, 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}, 
     {20, 20, 30, 30, 40, 40, 30, 30, 20, 20}, 
     {20, 30, 30, 40, 50, 50, 40, 30, 30, 20}, 
     {30, 40, 50, 50, 50, 50, 50, 50, 40, 30} 
    }; 
    int[][] seats = theaterSeats; 

    printArray(seats); //print the seating chart 
    System.out.println(); 

    //Defining variables 
    String str = ""; 
    String input = ""; 

    while (!input.equalsIgnoreCase("Q")) 
    { 
     System.out.print("Select 'S' to pick a seat, 'P' choose a price or 'Q' to quit:  "); 
     Scanner in = new Scanner(System.in); 
     input = in.next(); 

     if (input.equalsIgnoreCase("S")) 
     { 
      System.out.print("Enter row and seat number desired: "); 
      int row = in.nextInt(); 
      int seat = in.nextInt(); 
      System.out.println(); 
      sellSeatByNumber(seats, row, seat); 

      printArray(seats); 
      System.out.println(); 
     } 
     else if (input.equalsIgnoreCase("P")) 
     { 
      System.out.print("Enter price of seat desired: "); 
      int price = in.nextInt(); 
      System.out.println(); 
      sellSeatByPrice(seats, row, seat, price); 

      printArray(seats); 
      System.out.println(); 
     } 
    } 
    System.out.println(); 
    System.out.println("Thank you for choosing the ticket choice app."); 
    System.out.println(); 
} 

public static void printArray(int[][] currSeat) 
{ 
    final int ROWS = 9; 
    final int COLUMNS = 10; 

    for(int i = 0; i < ROWS; i++) 
    { 
     for (int j = 0; j < COLUMNS; j++) 
     { 
      System.out.print(currSeat[i][j] + "\t"); 
     } 
     System.out.println(); 
    } 
} 

public static int[][] sellSeatByNumber(int[][] seats, int row, int seat) 
{ 
    if (row <= 0 || row > 9) 
    { 
     if (seat <= 0 || seat > 10) 
     { 
      System.out.print("Please enter a valid row and seat: "); 
     } 
    } 
    if (seats[row][seat] == 0) 
    { 
     System.out.print("That seat is taken. Please select another seat: "); 
    } 
    else 
    { 
     seats[seats.length - row][seat - 1] = 0; 
    } 
    return seats; 
} 

public static int[][] sellSeatByPrice(int[][] seats, int row, int seat, int price) 
{ 
    if (seats[row][seat] = price) 
    { 
     seats[seats.length - row][seat - 1] = 0; 
    } 
    return seats; 
} 
} 
+0

대신 sellseatbyprice()에 대한 두 가지 함수가 있어야합니다. 누군가가 "S"를 선택하는 경우 "-1"이라고 말하면서 하나의 함수 만 있고 가격 값을 전달하지 않는 이유는 무엇입니까? – Arham

+0

나는 당신의 코멘트를 완전히 이해할 지 모르겠다. 나는 두 가지 방법을 가져야한다. 하나는 sellSeatByNumber이고 하나는 sellSeatByPrice이다. – josherickson

답변

1

내부 논리 및 구문이 올바르지 않습니다.

static 메서드를 사용하고 있으므로 시트 행렬을 따라 가며 괜찮습니다.하지만 전달되는 값이 행렬의 경계 안에 있는지 확인해야합니다. 그렇지 않으면 예외가 발생합니다.

예를 들어 sellSeatByPrice()에서 경계를 확인하지 않습니다. 당신은 정말로 그것을해야합니다, 또는 가짜 행/좌석 조합이 프로그램을 날려 버릴 수 있습니다.

if (seats[row][seat] = price) 

정말 =할당입니다

if (seats[row][seat] == price) 

로해야하고, ==원시적 비교입니다 :

이 주어진 비교가 잘못된 것 또한 경우입니다.

또한 sellSeatByNumber()에서 범위를 벗어난 행/시트 콤보가 여전히 폭발하기 때문에 문제가 계속 발생할 수 있습니다. 경계를 확인하십시오. 그러나 경계 밖이라면 아무 것도 반환하지 않습니다. 모든 현실에서 은 경계에서 벗어나거나 수정되지 않은 행렬을 반환 할 수 있습니다 (좀 더 직관적 일 수 있음).

+0

와우, 이것은 매우 도움이되었다. 고마워. 내가 문제가되는 두 가지가 있습니다 : 사용자가 sellSeatByNumber() 및 전체 sellSeatByPrice()에서 위조 된 행/자리를 입력 할 때. 내가 제안한 변경 사항에 대한 작업을 시작하겠습니다. 다시 한번 감사드립니다. – josherickson

+0

입력이 범위를 벗어 났는지 확인하는 데 문제가 있습니다. 행 row.length와 seat.length를 사용합니까? – josherickson

+0

자바 배열의 경우,'seats.length'는 가로 길이를,'seats [row] .length'는 세로 길이를 제공합니다. 대신 이것을 상수로 설정하는 것이 더 좋을 수 있으므로 더 쉽게 참조 할 수 있습니다. 내 관심을 필요로하는 다른 프로젝트가 있지만, 고려해야 할 것은 - 유효하기 위해 행 시트 콤보에 대해 사실이어야하는 것은 무엇입니까? – Makoto

관련 문제