简体   繁体   中英

In Java, how would I make for loops that iterate through a 2d array, and when a certain value is found, print a line and then exit the loop?

I am working on an assignment that is making a program that adds and subtracts passengers that are on a plane, displayed via a 2d array. I am trying to write code that will check if the seat a passenger requests is reserved or open. I made for loops that iterate through my 2d array that check if the elements of the 2d array have a matching element of an array of Passenger names I made. If there is a name at the seat that the passenger requested, the program should print Denied ; if the seat is open the program should print Confirmed . Right now, my program does this "correctly," but displays Denied or Confirmed a bunch of times. I am not sure how to fix this. I suspect it is because my break; statements only cut off the innermost loop that the statement belongs to.

Here is all of my code(Some parts aren't yet finished but that won't interfere with what I am doing):

import java.util.Scanner;
public class Plane {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        chartMaker();

    } //ends main

    public static void chartMaker() {
        String operator = " ";
        String Pname = " ";
        String SeatNum = " ";
        int runtime = 0;
        int SeatsTaken = 0;
        int TotalSeats = 48;
        int SeatsAvailable = 0;
        String[][] chart = new String[][]{
            {"1", "2", "3", "x", "4", "5", "6"}, 
            {"7", "8", "9", "x", "10", "11", "12"}, 
            {"13", "14", "15", "x", "16", "17", "18"},
            {"19", "20", "21", "x", "22", "23", "24"}, 
            {"25", "26", "27", "x", "28", "29", "30"}, 
            {"31", "32", "33", "x", "34", "35", "36"},
            {"37", "38", "39", "x", "40", "41", "42"}, 
            {"43", "44", "45", "x", "46", "47", "48"}
            };

            for(int i = 0; i < chart.length; i++) //starts printing of chart
            {
                for(int j = 0; j < chart[i].length; j++)
                {
                    System.out.print(chart[i][j] + " ");
                }
                System.out.println();
            }                                   //ends printing of chart

            System.out.println();
            while(runtime < 48) {
            System.out.print("Enter + to add a passenger, - to delete a passenger, or q to quit: ");
            operator = sc.nextLine(); 
            switch(operator) {
            case "+":
                System.out.print("Enter passenger name: ");
                Pname = sc.nextLine();
                System.out.print("Enter seat number: ");
                SeatNum = sc.nextLine();
                SeatChance(Pname, SeatNum, SeatsTaken, TotalSeats, SeatsAvailable);
                SeatsTaken++;
                for(int a = 0; a < chart.length; a++) //prints the updated chart
                {
                    for(int b = 0; b < chart[a].length; b++)
                    {
                        if(SeatNum.contentEquals(chart[a][b]))
                        {
                            chart[a][b] = Pname;
                            System.out.print(chart[a][b] + "   ");
                        }else {
                            System.out.print(chart[a][b] + "   ");
                        }
                    }System.out.println();
                }                                   //ends printing the updated chart
                System.out.println("Passenger: " + Pname);
                System.out.println("Seat Number: " + SeatNum);
                String[] Passengers = new String[SeatsTaken];
                Passengers[SeatsTaken - 1] = Pname;
                System.out.println("Passengers: ");
                for(int j = 0; j < Passengers.length; j++)
                {
                    System.out.println(Passengers[j]);
                }

                for(int x = 0; x < chart.length; x++) //checks if seat is taken
                {
                    for(int c = 0; c < chart[x].length; c++)
                    {
                        for(int g = 0; g < Passengers.length; g++)
                        {
                        if(chart[x][c].contentEquals(Passengers[g]))
                        {
                            System.out.println("Denied");
                            break;

                        }else {
                            System.out.println("Confirmed");
                            break;
                        }
                        }
                    }
                }

                break;
            case "-":
                System.out.println("");

                break;
            case "q":
                runtime = runtime + 48;
                System.out.println("End of Program");
                break;
            } //ends switch statement
            runtime++;
            } //ends while loop for asking user
            /*UpdateChart(chart, Pname, SeatNum);
            */


    } //ends chartMaker
    //seatChance is broken
    public static void SeatChance(String Pname, String SeatNum, int SeatsTaken, int TotalSeats, int SeatsAvailable) {
        double SeatChance = 0.0;
        SeatsAvailable = TotalSeats - SeatsTaken;
        SeatChance = (SeatsAvailable / TotalSeats) * 100;
        System.out.println(Pname + " has a " + SeatChance + "% chance of getting the seat");
    } //ends SeatChance


    /*
    public static void UpdateChart(String[][] chart, String Pname, String SeatNum) {
        for(int a = 0; a < chart.length; a++)
        {
            for(int b = 0; b < chart[a].length; b++)
            {
                if(SeatNum.equals(chart[a][b]))
                {
                    System.out.print(Pname);
                }else {
                    System.out.print(chart[a][b]);
                }
            }
            System.out.println();
        }


    } //ends UpdateChart
*/
} //ends Plane

Here is my code that needs fixed:

    for(int x = 0; x < chart.length; x++) //checks if seat is taken
                {
                    for(int c = 0; c < chart[x].length; c++)
                    {
                        for(int g = 0; g < Passengers.length; g++)
                        {
                        if(chart[x][c].contentEquals(Passengers[g]))
                        {
                            System.out.println("Denied");
                            break;

                        }else {
                            System.out.println("Confirmed");
                            break;
                        }
                        }
                    }
                }

And here is my output:

Passenger: John
Seat Number: 14
Passengers: 
John
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Denied
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed

I am assuming that the random Denied in the output is caused when my 2d array updates the values of the elements in the array, but I am not really sure.

The break statement exits the block and furthermore, ensures that the 'for' or 'while' loop that you're in does not iterate.

Notably, just break; does that to the nearest for/while/switch construct you're in.

For your break statements, the block you want to break out of is not actually the nearest. It also sounds like this would be a lot simpler if your outer loop loops passengers. So, from outer to inner: Passengers, planes, seats. You're also checking if the grid contains the name of the passenger; it sounds like that's wrong, and you just want to know if there's anybody in that seat.

To break out of an outer loop:

label:
for (int i = 0; i < 10; i++) {
   for (int j = 0; j < 10; j++) {
       if (i == 1 && j == 2) break label;
       System.out.print(i + "" + j + " ");
   }
}

this would print 000102030405060708091011.

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