使用2个二维数组表示电影院座位表。使用3种方法,让用户输入要价的席位,然后搜索一个数组,然后找到第二个数组中的第一个席位。我的问题是,如果输入“ 10”是正确的输入。首先找到的座位号应该是1。相反,它找到的是座位51。问题可能出在我的第二种方法上,该方法搜索定价数组并保存位置以传递给第三种方法。
我尝试打印搜索以查看是否存在问题,似乎不读取每一行并跳过定价数组中的列。香港专业教育学院尝试从搜索开始的其他值。
import java.util.Scanner;
public class MovieSeating
{
public static void main(String[] args)
{
//Seating arrangment for movie theater
System.out.println("Seating Arrangement");
int seat = 1;
int[][] seating = new int[9][10];
for(int i = 0;i < seating.length;i++){
for(int j = 0; j < seating[0].length;j++){
seating[i][j] = seat;
seat = seat + 1;
}
}
for(int i = 0;i < seating.length;i++){
for(int j = 0; j < seating[0].length;j++){
System.out.print(seating[i][j] + " ");
}
System.out.println("");
}
System.out.println();
//seat pricing array for movie theater
System.out.println("Seat Ticket Price");
int[][] pricing = 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, 40, 50, 50, 50, 50, 50, 50, 40, 20},
{80, 50, 50, 80, 80, 80, 80, 50, 50, 30}
};
for(int i = 0;i < pricing.length;i++){
for(int j = 0; j < pricing[0].length;j++){
System.out.print(pricing[i][j] + " ");
}
System.out.println();
}
System.out.println();
//Calling method 1
pricing(pricing, seating);
}
//Method 1
// validation and termination
//arguements take the 2 arrays
//Method performs user input searching for seat pricing and conducts validation
public static void pricing(int[][] pricing, int[][] seating)
{
Scanner in = new Scanner(System.in);
int price = 0;
while(true){
System.out.println("Please pick price or press Q to quit");
if(in.hasNextInt()){
//checks to see if it is valid input
price = in.nextInt();
//enter price
}else{
String garbage = in.next();
//Bad input catch
if(garbage.equals("Q") || garbage.equals("q")){
//Termination method
System.out.println("Thanks for Checking.... Good bye.");
break;
}
}
if(price == 10 || price == 20 || price == 30 || price == 40 || price == 50 || price == 80){
//calling method 2
available(pricing, seating, price);
}else{
System.out.println("Please pick a valid price. Valid prices are $10, $20, $30, $40, $50, and $80");
}
price = 0;
}
}
//Method 2
//arguements take both arrays again and the user price input
//searches through both arrays to determine seating arrangements
public static void available(int[][] pricing, int[][] seating, int input)
{
System.out.println("Checking for availability...");
//If not Q it will check to see if price is found or invalid by searching array
int check = 0;
int arraycheck1 = 0; //helps record location of price
int arraycheck2 = 0; //helps record location of price
for(int i = 0;i < pricing.length; i++){
for(int j = 0; j < pricing[0].length;j++){
if(pricing[i][j] == input){
arraycheck1 = i;
arraycheck2 = j;
pricing[i][j] = 0; // converts that price to zero so it will represent being "sold"
check = 1;
break;
}
}
}
int seat = seating[arraycheck1][arraycheck2]; // finds seat number with the price location
//calling method 3
confirmation(check, seat);
check = 0;
}
//Method 3
// Determines the print statement for confirmation
public static void confirmation(int valid, int seat)
{
//If the price is not valid then it prints not found
if(valid == 0){
System.out.println("No seat at this price is available. Sorry!");
}
//if the input price was valid it prints the seat confirmation
if(valid == 1){
System.out.println("Your seat is Confirmed! Your seat number is " + seat + " Enjoy your movie");
}
}
}
如果输入10。它会说座位已确认,您的座位号是1。如果您再次输入10,它将说座位2。
答案 0 :(得分:0)
问题在于方法2 for循环。为第一个循环分配“ outer:for(”,并在第二个循环中为中断条件添加“ break external”。