Java 如何正确嵌套循环

Java 如何正确嵌套循环,java,arrays,loops,if-statement,Java,Arrays,Loops,If Statement,我很难弄明白为什么当我运行代码时,它会跳过所有的if语句,而只是将其置为无效。我有一个2d数组,它的值代表一个电影院,我应该在售票,用户应该输入一笔钱来决定他们坐在哪里,这是他们可以从选择中负担得起的最昂贵的座位。这个人得到的座位应该从任意数字改为0。最后,我需要打印带有零的新数组 这就是我尝试过的: public static void main(String[] args) { Scanner in = new Scanner (System.in); boolean do

我很难弄明白为什么当我运行代码时,它会跳过所有的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;
                    }
                    }
                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);
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
布尔完成=假;
//初始座位表
int[][]表=
{
{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},
};
而(!完成){
int行;int列;
//搜索座位表

对于(row=0;row这是一个更好理解的示例,我在这里介绍了一个案例,因为其他案例基本相同

//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");
        }
    }
}
//这里是私有变量,因此我们可以通过findAvailableSeat()方法查找和预订座位
私有静态int[][]表={{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 }, };
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
布尔完成=假;
//初始座位表
而(!完成){
int行;
int col;
//搜索座位表
System.out.printf(“输入您希望在票据上花费的最大金额:”);
整数金额=in.nextInt();

如果(10这里有一个更好理解的示例,我在这里介绍一个案例,因为其他案例基本相同

//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");
        }
    }
}
//这里是私有变量,因此我们可以通过findAvailableSeat()方法查找和预订座位
私有静态int[][]表={{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 }, };
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
布尔完成=假;
//初始座位表
而(!完成){
int行;
int col;
//搜索座位表
System.out.printf(“输入您希望在票据上花费的最大金额:”);
整数金额=in.nextInt();

如果(10 JavaScript和Java是两种不同的编程语言。谢谢。尽管如此,你能帮我吗?你在2个for循环中循环是不必要的,客户必须非常幸运地得到一个座位,其中行和列的价格在他想要坐的价格范围内,就像在开始时,如果我选择45->,那么我进入第三个,如果不是a,则进入第三个t该点表[0][0]我正在编写一个代码示例,可以更好地解释JavaScript和Java是两种不同的编程语言。谢谢。尽管如此,你能帮我吗?你不需要在2个for循环中循环,客户必须非常幸运地得到一个座位,因为row和col的价格都在他想要坐的位置,如开始时选择45->则进入第三个else if,但此时表[0][0]为10,因此失败。我正在编写一个代码示例,可以更好地解释