使用表示电影院座位图的二维数组。使用 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
// arguments 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
// arguments 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。
问题出在方法 2 for 循环上。分配第一个循环 "outer: for(",并为第二个循环中的中断条件添加 "break outer"。