简体   繁体   English

for 循环一遍又一遍地重复

[英]for loop repeats over and over

This is the only part of my program that does not work properly.这是我的程序中唯一不能正常工作的部分。 If the person chose a 50 dollar seat it prints out a 0 to mark it is already sold.如果这个人选择了一个 50 美元的座位,它会打印一个 0 来标记它已经售出。 If there are not anymore seats of that price it should print out "sorry, that seat is unavailable."如果不再有该价格的座位,则应打印出“抱歉,该座位不可用”。 It does print that, but it prints it 8 times.它确实打印了那个,但它打印了 8 次。 (the number of 50 dollar seats) this is my entire code. (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."); 
            }
        }
    }

This is the output:这是输出:

Your seat has been reserved.您的座位已被预订。 A zero will show in the seat you bought.您购买的座位上会显示零。 Would you like to reserve another seat (Y/N)?: y Pick by Seat s or by Price p: (Type Q to cancel.) p Please enter a price for the seat you would like: 50 Sorry, that seat is unavailable.您想预订另一个座位吗(是/否)?: y 按座位 s 或按价格选择 p:(键入 Q 取消。) p 请输入您想要的座位的价格:50 抱歉,该座位不可用. Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Sorry, that seat is unavailable.抱歉,该座位不可用。 Would you like to reserve another seat (Y/N)?:您想预订另一个座位吗(是/否)?:

The problem occurs on the price Method not the number, if the user enters 50 , you are checking the matrix seatsArray[][] for available seats :问题出现在价格方法而不是数字上,如果用户输入50 ,则您正在检查可用座位的矩阵seatsArray[][]

Solution : you need to add a boolean when seat gets reserved so you can set it true and escape the loop which is on the for not the the if解决方案:您需要在预订座位时添加一个boolean ,以便您可以将其设置为true并转义for而不是if上的循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM