java - 如何正确嵌套循环

标签 java arrays loops if-statement

我很难弄清楚为什么当我运行代码时它会跳过所有 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;
}

关于java - 如何正确嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55344025/

相关文章:

java - 替代连续 String.replace

python - 如何将压缩的二进制文件读取为 float 组

python - 运算符结果的 Numpy 总和,无需分配不必要的数组

php - 我的代码只向我显示第一个结果并带来另一个空值

java - 'jvm-1。 8' is not a valid choice for '-目标'

java - 使用java中的公钥实现进行RSA解密

arrays - 将两个 Bash 数组合并到键 :value pairs via a Cartesian product

jquery - 使用 JavaScript (jQuery),如何一次循环浏览页面上的所有 'a' 标记,并为每个标记分配单独的样式?

java - 使用 Apache POI 生成的数据透视表隐藏 Excel 工作簿中的第一个工作表

arrays - KDTree 实现为单个数组/无节点