for {循环重复'

时间:2015-11-21 14:44:54

标签: java loops if-statement

这是我的程序中唯一无法正常工作的部分。如果这个人选择了一个50美元的座位,它会打印出0来标记它已经售出。如果没有该价格的座位,则应该打印出来"抱歉,该座位不可用。"它会打印出来,但会打印8次。 (50美元座位的数量)这是我的整个代码。

import java.util.*;

public class Theater {
    /**
    * @param args the command line arguments
    */   
    static int[][] seatsArray;
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        seatsArray = 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 Arena.");    

        System.out.println("row 1, column 1, is the bottom front row.");
        System.out.println("A zero meanss tha seat is sold.");
        seats(seatsArray);
        char n = 'Y';
        while ((n == 'Y') || (n == 'y'))
        {
            System.out.println("Pick by Seat s or by Price p: ");
            System.out.println("(Type Q to cancel.)");
            char seat = input.next().charAt(0);

            switch (seat)
            {
                case'S':case's':
                {
                    number(seatsArray);
                    break;
                }
                case'P':case'p':
                {
                    price(seatsArray);
                    break; 
                }
                case'Q':case'q':
                { 
                    System.out.print("Thank you for choosing The Arena");
                    System.exit(0);
                }
                default:
                {
                    System.out.println("Error: Please Try Again.");
                }
            }
            System.out.print("Would you like to reserve another seat (Y/N)?: ");
            n = input.next().charAt(0);
        }
        System.out.print("Thank you for choosing the Arena.");
    }

    public static void seats(int seatsArray[][])
    {
        for (int[] seatsArray1 : seatsArray)
        {
            for (int j = 0; j < seatsArray1.length; j++) 
            {
                if (j>0)
                System.out.print("\t");
                System.out.print(seatsArray1[j]);
            }
        System.out.println();
        }
    }

    public static void price(int seatsArray[][])
    {
        System.out.print("Please enter a price for the seat you would like: ");
        int price = input.nextInt();

        // boolean found = false;
        out: for (int i=0;i<9;i++)
        for (int j=0;j<10;j++)
            if (seatsArray[i][j]==price)
            {
                seatsArray[i][j] = 0; 
                seats(seatsArray);
                System.out.println("Your seat has been reserved. A zero will show in the seat you bought.");break out;
            }
            else if(seatsArray[i][j]== 0)
            {
                System.out.println("Sorry, that seat is unavailable.");
            }
        }

        public static void number(int seatsArray[][])
        {
            System.out.println("Enter a row, then enter a seat number.");
            System.out.print("What row would you like to sit on?:");
            int i = input.nextInt();
            i = Math.abs(i-9);
            System.out.print("What seat of that row do you want?:");
            int j = input.nextInt();
            j -= 1;

            if (seatsArray[i][j]!=0)
            {
                seatsArray[i][j] = 0;
                seats(seatsArray);
                System.out.println("Your seat has been reserved. A zero will show in the seat you bought.");
            }
            else 
            {
                System.out.println("Sorry, that seat is unavailable."); 
            }
        }
    }

这是输出:

  

您的座位已被保留。您购买的座位将显示零。   您想预订另一个座位(Y / N)吗?:按座位或按座位选择   价格p :( Q型取消。)p请输入您的座位价格   我想:50对不起,那个座位不可用。对不起,那个座位是   不可用。对不起,那个座位不可用。对不起,那个座位是   不可用。对不起,那个座位不可用。对不起,那个座位是   不可用。对不起,那个座位不可用。对不起,那个座位是   不可用。您想预订另一个座位(是/否)?:

1 个答案:

答案 0 :(得分:0)

问题出现在价格方法而不是数字上,如果用户输入50,您正在检查矩阵seatsArray[][]是否有可用席位:

解决方案:您需要在座位获得保留时添加boolean,以便您可以设置true并转义for上的循环if