无限循环在程序

时间:2016-02-01 02:15:25

标签: java arrays loops multidimensional-array infinite-loop

该程序使用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);
}

3 个答案:

答案 0 :(得分:3)

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

上的分号

;结束语句,这意味着break总是完成,而不仅仅是满足条件。

答案 1 :(得分:1)

而不是使用

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
    }
}

答案 2 :(得分:0)

删除分号;

更改

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

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