【问题标题】:Find all matching elements in Array - Java查找数组中的所有匹配元素 - Java
【发布时间】:2020-12-11 15:49:29
【问题描述】:

目前我的代码只给了我第一个匹配的结果。用户输入他们想要的房间价格,此时它只会显示第一个匹配项。在用户向控制台输入“60”的情况下,它应该显示 3 个结果。我想在打印到控制台后我需要另一个 forloop 和 if 语句,但不确定如何执行

public static void secondMain() {
    BufferedReader reader;
    var lines = new ArrayList<String>();
    var rooms = new ArrayList<Room>();
    Room selectedRoom = null;

    try {
        reader = new BufferedReader(new FileReader("rooms.txt"));
        String line = reader.readLine();
        lines.add(line);
        while (line != null) {
            line = reader.readLine();
            lines.add(line);

        }

        reader.close();

        for (int i = 0; i < lines.size() - 1; i++) {
            String[] words = lines.get(i).split(" ");
            var room = new Room();
            room.roomNum = Integer.parseInt(words[0]);
            room.roomType = (words[1]);
            room.roomPrice = Double.parseDouble(words[2]);
            room.hasBalcony = Boolean.parseBoolean(words[3]);
            room.hasLounge = Boolean.parseBoolean(words[4]);
            room.eMail = (words[5]);
            rooms.add(room);
        }

        var searchRoomPrice = input.nextDouble();

        for (int i = 0; i < rooms.size(); i++) {
            if (rooms.get(i).roomPrice == searchRoomPrice) {
                selectedRoom = rooms.get(i);
                break;
            }
        }

        System.out.println("Room Number: " + selectedRoom.roomNum);
        System.out.println("Room Type: " + selectedRoom.roomType);
        System.out.println("Room Price: " + selectedRoom.roomPrice);
        System.out.println("Balcony: " + selectedRoom.hasBalcony);
        System.out.println("Lounge: " + selectedRoom.hasLounge);
        System.out.println("Email: " + selectedRoom.eMail);
        System.out.println("-------------------");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

任何其他需要的信息随时询问

【问题讨论】:

  • 为什么找到第一个条目后要休息?为什么不继续并找到所有满足条件的房间并将打印语句移动到循环中。

标签: java arrays for-loop methods


【解决方案1】:

如果您只想打印信息,请在循环内移动打印命令并删除中断,即

for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         selectedRoom = rooms.get(i);
         System.out.println("Room Number: " + selectedRoom.roomNum);
         System.out.println("Room Type: " + selectedRoom.roomType);
         System.out.println("Room Price: " + selectedRoom.roomPrice);
         System.out.println("Balcony: " + selectedRoom.hasBalcony);
         System.out.println("Lounge: " + selectedRoom.hasLounge);
         System.out.println("Email: " + selectedRoom.eMail);
         System.out.println("-------------------");
    }   
}
            

您还可以使用第一个循环将所有对象保存在一个列表中,然后在第二个循环中遍历该列表并打印信息,即

List<Room> roomList = new ArrayList<Room>();
for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         roomList.add(rooms.get(i));             
    }   
}
for(Room room : roomList){
    System.out.println("Room Number: " + room.roomNum);
    System.out.println("Room Type: " + room.roomType);
    System.out.println("Room Price: " + room.roomPrice);
    System.out.println("Balcony: " + room.hasBalcony);
    System.out.println("Lounge: " + room.hasLounge);
    System.out.println("Email: " + room.eMail);
    System.out.println("-------------------");
}       

【讨论】:

    【解决方案2】:

    我注意到两件事:

    1. 您在此处使用break;
                    if(rooms.get(i).roomPrice == searchRoomPrice){
                        selectedRoom = rooms.get(i);
                        break;
                    }   
    

    所以你在第一场比赛后停止循环。

    1. 这里:
    for (int i = 0; i < lines.size() - 1; i++)
    

    有理由使用负 1 吗?

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 2021-01-15
      • 2019-02-18
      • 1970-01-01
      • 2015-07-01
      • 2017-03-15
      • 2020-06-19
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多