Java 无限循环在程序中无法正常工作

Java 无限循环在程序中无法正常工作,java,arrays,loops,multidimensional-array,infinite-loop,Java,Arrays,Loops,Multidimensional Array,Infinite Loop,此程序使用2D数组记录开放和保留的房间。该程序在开始和每次更改数组后显示2D数组。索引值表示房间编号,并为开放房间显示。预订房间显示预订房间的人的姓名。程序将继续运行,直到用户输入“Q” 对我来说,一切似乎都很好,除了在主方法中循环一次之后,即使用户没有输入“Q”,程序也停止运行。我也试过用while(true)来做,但它仍然不起作用。我真的希望有人能帮我 public static void main (String [] args) { Scanner kb = new Scanne

此程序使用2D数组记录开放和保留的房间。该程序在开始和每次更改数组后显示2D数组。索引值表示房间编号,并为开放房间显示。预订房间显示预订房间的人的姓名。程序将继续运行,直到用户输入“Q”

对我来说,一切似乎都很好,除了在主方法中循环一次之后,即使用户没有输入“Q”,程序也停止运行。我也试过用while(true)来做,但它仍然不起作用。我真的希望有人能帮我

public static void main (String [] args)
{
    Scanner kb = new Scanner (in);
    out.print("Enter row size: ");
    int row = kb.nextInt();
    out.print("Enter column size: ");
    int col = kb.nextInt();
    kb.nextLine();
    String [][]rooms = roomInIt(row, col);
    printMatrix(rooms);
    String a="";


    for(;;)
    {
        out.print("Would you like to reserve or release a room? Type Q to exit ");
        a = kb.nextLine();

        if (a.equalsIgnoreCase("reserve"))
        {
            out.print("How many rooms would you like to reserve? ");
            int num = kb.nextInt();
            reserveRooms(rooms, num);
        }
        else if (a.equalsIgnoreCase("release"))
        {
            out.print("How many rooms would you like to release? ");
            int x = kb.nextInt();
            releaseRooms(rooms, x);
        }
        else if (a.equalsIgnoreCase("Q"));
            break;
    }
}

public static void printMatrix(String[][]rooms)
{

    for(int r=0; r<rooms.length; r++)
        {
            for (int c=0; c<rooms[r].length; c++)
                out.print("["+r+","+c+"] "+rooms[r][c]+"\t");
            out.println();
        }
}

public static String[][] roomInIt(int row, int col)
{
    String [][]rooms = new String [row][col];
    for(int r=0; r<rooms.length; r++)
        for (int c=0; c<rooms[r].length; c++)
            rooms[r][c]="open";
    return rooms;
}

public static void reserveRooms(String [][]room, int num)
{

    Scanner kb = new Scanner(in);
    for(int i=0; i<num; i++)
    {   
        out.print("Enter name: ");
        String name = kb.nextLine();
        out.print("Enter the room number (row and column separated by a space) ");
        int r=kb.nextInt();
        int c=kb.nextInt();
        kb.nextLine();
        while (r<0 || r>room.length || c<0 || c>room[r].length || !room[r][c].equals("open"))
        {
            out.print("Invalid room number. Enter another room number (row and column separated by a space) ");
            r=kb.nextInt();
            c=kb.nextInt();
            kb.nextLine();
        }
        room[r][c] = name;
    }
    printMatrix(room);
}

public static void releaseRooms(String [][]room, int x)
{
    int r, c;
    Scanner kb = new Scanner(in);
    for(int i=0; i<x; i++)
    {   
        out.print("Enter name: ");
        String name = kb.nextLine();
        out.print("Enter the room number (row and column separated by a space) ");
        r=kb.nextInt();
        c=kb.nextInt();
        kb.nextLine();
        while(r<0 || r>room.length || c<0 || c>room[r].length)
        {
            out.print("Invalid room number. Enter another room number (row and column separated by a space) ");
            r=kb.nextInt();
            c=kb.nextInt();
            kb.nextLine();
        }
        room[r][c] = "open";

    }
    printMatrix(room);
}
publicstaticvoidmain(字符串[]args)
{
扫描仪kb=新扫描仪(英寸);
打印(“输入行大小:”);
int row=kb.nextInt();
打印(“输入列大小:”);
int col=kb.nextInt();
kb.nextLine();
字符串[][]rooms=roomInIt(行,列);
打印矩阵(房间);
字符串a=“”;
对于(;;)
{
打印(“您想预订还是释放房间?键入Q退出”);
a=kb.nextLine();
如果(a.同等信号(“储备”))
{
打印(“您想预订多少房间?”);
int num=kb.nextInt();
预订房间(房间,数量);
}
否则,如果(a.equalsIgnoreCase(“释放”))
{
打印(“您希望释放多少个房间?”);
int x=kb.nextInt();
释放室(房间,x);
}
否则,如果(a.equalsIgnoreCase(“Q”));
打破
}
}
公共静态void打印矩阵(字符串[][]房间)
{

对于(int r=0;r去掉
else if(a.equalsIgnoreCase(“Q”);


结束语句,这意味着
中断始终完成,而不仅仅是在满足条件时。

如果(a.equalsIgnoreCase(“Q”);


结束语句,这意味着始终完成
中断
,而不仅仅是在满足条件时。

删除分号

改变

else if (a.equalsIgnoreCase("Q"));


删除分号

改变

else if (a.equalsIgnoreCase("Q"));

而不是使用

for(;;)
{
    out.print("Would you like to reserve or release a room? Type Q to exit ");
    a = kb.nextLine();

    if (a.equalsIgnoreCase("reserve"))
    {
        out.print("How many rooms would you like to reserve? ");
        int num = kb.nextInt();
        reserveRooms(rooms, num);
    }
    else if (a.equalsIgnoreCase("release"))
    {
        out.print("How many rooms would you like to release? ");
        int x = kb.nextInt();
        releaseRooms(rooms, x);
    }
    else if (a.equalsIgnoreCase("Q"));
        break;
}
为什么不使用

while (!a.equalsIgnoreCase("Q"))
{
    out.print("Would you like to reserve or release a room? Type Q to exit ");
    a = kb.nextLine();

    if (a.equalsIgnoreCase("reserve"))
    {
        out.print("How many rooms would you like to reserve? ");
        int num = kb.nextInt();
        reserveRooms(rooms, num);
    }
    else if (a.equalsIgnoreCase("release"))
    {
        out.print("How many rooms would you like to release? ");
        int x = kb.nextInt();
        releaseRooms(rooms, x);
    }
    else 
    {
        //do nothing
    }
}
而不是使用

for(;;)
{
    out.print("Would you like to reserve or release a room? Type Q to exit ");
    a = kb.nextLine();

    if (a.equalsIgnoreCase("reserve"))
    {
        out.print("How many rooms would you like to reserve? ");
        int num = kb.nextInt();
        reserveRooms(rooms, num);
    }
    else if (a.equalsIgnoreCase("release"))
    {
        out.print("How many rooms would you like to release? ");
        int x = kb.nextInt();
        releaseRooms(rooms, x);
    }
    else if (a.equalsIgnoreCase("Q"));
        break;
}
为什么不使用

while (!a.equalsIgnoreCase("Q"))
{
    out.print("Would you like to reserve or release a room? Type Q to exit ");
    a = kb.nextLine();

    if (a.equalsIgnoreCase("reserve"))
    {
        out.print("How many rooms would you like to reserve? ");
        int num = kb.nextInt();
        reserveRooms(rooms, num);
    }
    else if (a.equalsIgnoreCase("release"))
    {
        out.print("How many rooms would you like to release? ");
        int x = kb.nextInt();
        releaseRooms(rooms, x);
    }
    else 
    {
        //do nothing
    }
}

非常感谢。那些分号总是用来表示我的意思,哈哈哈。非常感谢。那些分号总是用来表示我的意思,哈哈哈。