首先让我说,这是我在课堂上做的一个“实验室”,我已经为两个程序中的第二个做了大约5个小时的工作,其中两个可能被困在了其中一个部分。让我先告诉你这个程序的要求,这样也许所有这些都会更有意义。
剧院座位表被实现为票价的二维数组,如下所示:
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编写一个程序,提示用户选择一个座位或价格。马克通过把价格改为0出售了座位。当用户指定一个座位时,确保它是可用的。当用户指定价格时,请找出该价格的任何位置。
这就是这本书的描述,这也是我的教授给我的建议。
所需的程序组件:此解决方案中的初始座位图是生成的,如示例所示。该解决方案还假定用户将输入一行和座位,从1开始,其中第1行是最下面的前排。您的程序应该至少有3种静态方法,一是打印数组,二是sellSeatByPrice,三是sellSeatByNumber。请声明并初始化您的阵列提供的座位价格值,在主。
我花了大约两个小时才弄明白如何将二维数组中的一个改变为0。假设我输入了一个30的位置,为了让它工作,我已经修改了大约5次代码,我要么每30次更改一个0,或者几个,或者一个也没有。我需要帮助的主要部分是名为sellSeatByPrice()的方法。目前我没有任何数字可以改变。以下是我当前的完整代码:
import java.util.*;
public class Theater
{
static Scanner in = new Scanner(System.in);
static int[][] seatingChart;
public static void main(String[] args)
{
seatingChart = new int[][] {
{ 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 },
};
System.out.println("Welcome to the Box Office Program."); //Introduction Statements
System.out.println("Please note that seats are arranged such that");
System.out.println("row 1, column 1, is the bottom front row.");
System.out.println("Also, a zero denotes the seat is already sold.");
printSeats(seatingChart);
char response = 'Y';
while ((response == 'Y') || (response == 'y'))
{
System.out.print("Pick by Seat <s>, Price <p>, or Quit <q>: ");
char choice = in.next().charAt(0);
switch (choice)
{
case'S':case's':
{ sellSeatByNumber(seatingChart);
break; }
case'P':case'p':
{ sellSeatByPrice(seatingChart);
break; }
case'Q':case'q':
{ System.out.print("Thank you, come again!");
System.exit(0); }
default:
{ System.out.println("Error: Invalid choice."); }
}
System.out.print("Would you like to reserve another seat (Y/N)?: ");
response = in.next().charAt(0);
}
System.out.print("Thank you, come again!");
}
public static void printSeats(int seatingChart[][])
{
for(int i=0; i<seatingChart.length; i++)
{
for(int j=0; j<seatingChart[i].length; j++)
{
if (j>0)
System.out.print("\t");
System.out.print(seatingChart[i][j]);
}
System.out.println();
}
}
public static void sellSeatByPrice(int seatingChart[][])
{
System.out.print("Please enter a price for the seat you would like: ");
int price = in.nextInt();
boolean found = false;
for (int i=0;i<9;i++)
for (int j=0;j<10;j++)
if ((found == true)&&(seatingChart[i][j]==price))
{ seatingChart[i][j]=0; break; }
printSeats(seatingChart);
}
public static void sellSeatByNumber(int seatingChart[][])
{
System.out.println("Enter a row, then enter a seat number.");
System.out.print("What row would you like to sit on?:");
int row = in.nextInt();
row = Math.abs(row-9);
System.out.print("What seat of that row would you like to sit in?:");
int col = in.nextInt();
col -= 1;
if (seatingChart[row][col]!=0)
{
seatingChart[row][col] = 0;
printSeats(seatingChart);
System.out.println("Your seat has been reserved and reflected with a 0 on the chart now.");
}
else { System.out.println("Sorry, that seat is already taken."); }
}
}(如果这件事看上去很草率,现在是凌晨3点,我不能再想了,我真的很抱歉)
发布于 2014-03-21 07:41:02
您的方法必须如下所示:
public static void sellSeatByPrice(int seatingChart[][])
{
System.out.print("Please enter a price for the seat you would like: ");
int price = in.nextInt();
// boolean found = false;
out: for (int i=0;i<9;i++)
for (int j=0;j<10;j++)
if (seatingChart[i][j]==price)
{ seatingChart[i][j]=0; break out; } // Notice this change
printSeats(seatingChart);
}发布于 2014-03-21 07:25:38
首先,检查found == true (顺便说一句,它可以缩短为if (found) )。found被初始化为false,再也不会被碰触,所以每次检查都失败。
这里甚至不需要布尔变量。对座位进行迭代;如果一个座位有您要寻找的价格,请将座位的价格设置为0,打印数组并返回(break只退出内部for循环)。
https://stackoverflow.com/questions/22552621
复制相似问题