gpt4 book ai didi

java - 将参数从方法传递到 main

转载 作者:行者123 更新时间:2023-11-30 11:39:29 24 4
gpt4 key购买 nike

我似乎无法弄清楚我的方法“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;
}
}

最佳答案

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

由于您使用的是 static 方法,并传递座位矩阵,这没问题 - 但您必须检查传入的值是否在矩阵的范围内 - 或者您将获得异常(exception)情况。

例如,您不检查 sellSeatByPrice() 中的边界。你真的应该这样做,否则伪造的行/座位组合可能会破坏你的计划。

同样是给出的比较不正确:

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

应该是

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

=赋值==原始比较

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

关于java - 将参数从方法传递到 main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com