如何在用户创建所述数字后创建要填充的空变量?

时间:2017-10-13 01:24:26

标签: java netbeans logic

我知道我解释得非常糟糕,我也很难理解它的逻辑。我所做的是为用户创建一种方式输入在此计算器中创建的房间数(最多5个)。我不知道该怎么做,是创建接下来的几行,作为(最多)5个房间的占位符(?)。我在下面举一个例子。我道歉,对Java很新。

        System.out.print ("Please enter the number of rooms! (Up to 5!) ");

    int roomcount = sc.nextInt();

    String[] places = new String[roomcount];
        for(int i = 0; i < places.length; i++) {
        places[i] = "Place Number: " + i;
        }

        sc.nextLine();

    System.out.println("You said that there were " + roomcount + " rooms!");

    System.out.print ("Please enter the types of rooms (up to " +roomcount+ ") that fill up the " + roomcount + " rooms!\n"
            + "(Rooms like Living, Dining, Bedroom1-2, Kitchen, Bathroom, etc!) \n ") ;

    String roomname1 = sc.next();
    String roomname2 = sc.next(); <------ I know these lines are redundant.
                                          I'm just unsure of how to put the idea into action. 

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以创建一个数组来添加房间类型:

    String[] types = new String[roomcount];
    for(int i = 0; i < types.length; i++) {
        System.out.print ("Please enter the types of room " + places[i]);
        String roomName = sc.next();
        types[i] = roomName;
        System.out.println("Room name: " + roomName);
    }