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;
}
}
4

1 回答 1

1

您的参数看起来不错 - 内部逻辑和语法不正确。

由于您正在使用static方法并传递座位矩阵,这没关系 - 但您必须检查传入的值是否在矩阵的范围内 - 否则您会遇到异常。

例如,您不检查sellSeatByPrice(). 您真的应该这样做,否则虚假的行/座位组合可能会破坏您的程序。

给出的比较也不正确的情况也是如此:

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

真的应该

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

as=赋值==原始比较

此外,在 中sellSeatByNumber(),您仍然会遇到问题,因为越界行/座位组合仍然会爆炸。您确实检查了界限-荣誉-但如果它们超出了界限,您将不会返回任何东西。实际上,IllegalArgumentException如果它们超出范围,则应该引发 an ,或者您可以返回未修改的矩阵(这可能更直接)。

于 2012-11-07T03:43:48.850 回答