簡體   English   中英

如何正確嵌套循環

[英]How to properly nest loops

我很難弄清楚為什么當我運行我的代碼時它會跳過所有的if語句而只是將其置於無效狀態。 我有一個二維數組,值表示電影院,我應該賣票,用戶應該輸入一筆錢,確定他們要坐在哪里,這是他們可以負擔得起的最昂貴的座位選擇。 該人所在的座位應從任何數字更改為0。最后,我需要用零打印新數組。

這是我嘗試過的:

public static void main(String[] args) {

    Scanner in = new Scanner (System.in);
    boolean done = false;
        //initial seating chart
    int [] [] table = 
        {
            {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, 20, 20, 20, 20},
            {20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
            {30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
            {40, 40, 40, 40, 40, 40, 40, 40, 40, 40},
            {40, 40, 40, 40, 40, 40, 40, 40, 40, 40},
            {50, 50, 50, 50, 50, 50, 50, 50, 50, 50},
        };
    while (!done) {
        int row; int col;
        //search seating chart
        for (row=0; row<9; row++) {
            for (col=0; col<8; col++) {
                System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");
                int amount = in.nextInt();
                if (table[row][col]==10 && 10<=amount && amount<20) {
                table[row][col]=0;
                    System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row+1, col+1);
                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
                    if (in.hasNext("y")) {
                        done = false;
                    }
                    else {done = true;
                    }
                    }
                else if (table[row][col]==20 && 20<=amount && amount<30) {
                    table[row][col]=0;
                    System.out.printf("Ticket located at Row %d Seat %d purchased for 20\n", row+1, col+1);
                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
                    if (in.hasNext("y")) {
                        done = false;
                    }
                    else {done = true;
                    }
                    }
                else if (table[row][col]==30 && 30<=amount && amount<40) {
                    table[row][col]=0;
                    System.out.printf("Ticket located at Row %d Seat %d purchased for 30\n", row+1, col+1);
                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
                    if (in.hasNext("y")) {
                        done = false;
                    }
                    else {done = true;
                    }
                    }
                else if (table[row][col]==40 && 40<=amount && amount<50) {
                    table[row][col]=0;
                    System.out.printf("Ticket located at Row %d Seat %d purchased for 40\n", row+1, col+1);
                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
                    if (in.hasNext("y")) {
                        done = false;
                    }
                    else {
                        done = true;
                    }
                    }
                else if (table[row][col]==50 && 50<=amount) {
                    table[row][col]=0;
                    System.out.printf("Ticket located at Row %d Seat %d purchased for 50\n", row+1, col+1);
                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
                    if (in.hasNext("y")) {
                        done = false;
                    }
                    else {done = true;
                    }
                    }
                else {System.out.print("invalid");}
        }}}
        //final seating chart
        System.out.print(table);
}
}

這是一個更好理解的示例,我在這里涵蓋了一種情況,因為其余的情況大致相同

//private variable here so we could find and book seat through method findAvailableSeat()
private static int[][] table = { { 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, 20, 20, 20, 20 },
        { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 },
        { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 }, { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 },
        { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 }, };
public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    boolean done = false;
    // initial seating chart

    while (!done) {
        int row;
        int col;
        // search seating chart
        System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");
        int amount = in.nextInt();
        if (10 <= amount && amount < 20) {
            String location = findAvailableSeat(10);//store the indexes in a variable to show the user later
            if(location != null) {
                String[] locationList = location.split(",");
                row = Integer.parseInt(locationList[0]);
                col = Integer.parseInt(locationList[1]);
                System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row + 1, col + 1);
                System.out.print("Would you like to purchase additional tickets? (Y/N) ");
                String resp = in.next(); //this statement is needed as sometimes with in.next() rushes to user input without printing text
                if (resp.equals("Y")) {
                    done = false;
                } else {
                    done = true;
                }
            }
            else 
                System.out.printf("No available seat found\n");
        }
    }
}

在方法中

private static String findAvailableSeat(int i) {
    // TODO Auto-generated method stub
    for (int row=0; row<9; row++) {
        for (int col=0; col<8; col++) {
            if(table[row][col]==i) {
                //find the seat location, set location to 0 and then return indices to show in user result
                table[row][col] = 0;
                return row + "," + col;
            }
        }
    }
    return null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM