如何正确嵌套循环

我很难弄清楚为什么当我运行我的代码时,它会跳过所有的if语句,只是把无效。我有一个2d数组,其值表示电影院,我应该出售门票,用户应该输入一笔钱来确定他们将坐在哪里,这是他们从选择中可以负担得起的最昂贵的座位。该人获得的座位应从任何数字更改为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;

                    }

                    }


长风秋雁
浏览 86回答 1
1回答

ABOUTYOU

这是一个更好地理解的示例,我在这里涵盖了一个案例,因为其余的都相当相同//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 },&nbsp; &nbsp; &nbsp; &nbsp; { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 },&nbsp; &nbsp; &nbsp; &nbsp; { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 },&nbsp; &nbsp; &nbsp; &nbsp; { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 }, { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 },&nbsp; &nbsp; &nbsp; &nbsp; { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 }, };public static void main(String[] args) {&nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; boolean done = false;&nbsp; &nbsp; // initial seating chart&nbsp; &nbsp; while (!done) {&nbsp; &nbsp; &nbsp; &nbsp; int row;&nbsp; &nbsp; &nbsp; &nbsp; int col;&nbsp; &nbsp; &nbsp; &nbsp; // search seating chart&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");&nbsp; &nbsp; &nbsp; &nbsp; int amount = in.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (10 <= amount && amount < 20) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String location = findAvailableSeat(10);//store the indexes in a variable to show the user later&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(location != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] locationList = location.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = Integer.parseInt(locationList[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; col = Integer.parseInt(locationList[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row + 1, col + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Would you like to purchase additional tickets? (Y/N) ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String resp = in.next(); //this statement is needed as sometimes with in.next() rushes to user input without printing text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (resp.equals("Y")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("No available seat found\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在方法中,我们有private static String findAvailableSeat(int i) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; for (int row=0; row<9; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int col=0; col<8; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(table[row][col]==i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //find the seat location, set location to 0 and then return indices to show in user result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[row][col] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return row + "," + col;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java