简体   繁体   中英

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. 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. (the number of 50 dollar seats) this is my entire code.

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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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