简体   繁体   English

在 Java 中,我将如何进行循环遍历二维数组,当找到某个值时,打印一行然后退出循环?

[英]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 ;如果乘客要求的座位上有姓名,程序应打印Denied if the seat is open the program should print Confirmed .如果座位打开,程序应打印Confirmed Right now, my program does this "correctly," but displays Denied or Confirmed a bunch of times.现在,我的程序“正确”地执行了此操作,但多次显示DeniedConfirmed I am not sure how to fix this.我不知道如何解决这个问题。 I suspect it is because my break;我怀疑是因为我的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:这是我的 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.我假设 output 中的随机Denied是在我的二维数组更新数组中元素的值时引起的,但我不太确定。

The break statement exits the block and furthermore, ensures that the 'for' or 'while' loop that you're in does not iterate. break语句退出块,此外,确保您所在的“for”或“while”循环不会迭代。

Notably, just break;值得注意的是,只是break; does that to the nearest for/while/switch construct you're in.对您所在的最近的 for/while/switch 结构执行此操作。

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.这将打印 000102030405060708091011。

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

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