猿问

在满足条件之前,如何在 Java 程序中创建连续的 For 循环?

我希望在 Java 程序中创建一个连续的增强型 For 循环,以将消息从自动 ping 输出到文本区域,然后在满足条件时停止(IP 地址脱机)。我在一个名为“ip”的字符串数组中有 IP 地址。无需循环即可正常工作,但需要不断迭代,直到没有检测到响应。


尝试使用我包含在代码中的布尔循环 - 但是不会将文本附加到 TextArea 并陷入连续循环。


        for (String ip : listOfhosts) {


        boolean repeatLoop = true;



        try {

            InetAddress i = InetAddress.getByName(ip);

            textArea.append("Sending Ping Request to " + ip + "\n");


            //boolean run = true;

            //while (run){


            if (i.isReachable(100)) { // 1 second limit

                textArea.append("Host is online \n");

            } else {

                textArea.append("\nHOST IS OFFLINE\n");

                //System.exit(0); //If something is offline, system will close. For now.   



                try {


                    Message message = new MimeMessage(session);

                    message.setFrom(new InternetAddress("email@gmail.com"));

                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("email@hotmail.com"));

                    message.setSubject("Test Mail");

                    message.setText("Test Mail," + "\n Sent From sendMail.java application\n");

                    //textArea.append("OK - Online \n");


                    Transport.send(message);


                    textArea.append("Mail Sent to email@email.com \n host " + ip + " is offline \n");


                    return;


                } catch (MessagingException e) {

                    throw new RuntimeException(e);

                }

            }        

        } catch (Exception e) {

            textArea.append("Unknown HOST\n");

            e.printStackTrace();


        }while(repeatLoop);

陷入连续循环 - 使程序崩溃。任何帮助都会很棒!


皈依舞
浏览 163回答 3
3回答

料青山看我应如是

此代码将阻止您的 UI。您应该在单独的线程中运行它,并在检查之间添加一些延迟。一个更简单的解决方案也是使用 Timer例如,每秒检查一次:Timer timer = new Timer();timer.schedule(new TimerTask() {    @Override    public void run() {        // your code    }}, 1000);

慕斯王

有两种简单的方法可以摆脱 Java 中增强的 for 循环。一种是抛出异常,尽管对于正常的流控制,这被认为是异常使用异常。另一种是使用break;which 将使您摆脱最直接的 for 循环。人们并不总是喜欢return中间函数或break中间 for 循环(或continue就此而言),因为它们的行为有点像 goto/jump 语句并降低代码的清晰度。另一方面,这正是 break 的目的,因此没有关于滥用指令的争论。另一种方法是使用普通的 while 循环并放弃普通迭代器的增强型 for 循环。在这种情况下,listOfHosts.iterator(), 将允许您计算.hasNext()和的组合isReachable(...)来决定是否继续循环。

慕慕森

// suppose listOfhosts is a String ArrayList<String> srcHostList = new ArrayList<String>();srcHostList.addAll(Arrays.asList(listOfhosts));List<String> offlineList = new ArrayList<String>();while( srcHostList.size() > 0) {&nbsp; &nbsp; offlineList.clear();&nbsp;&nbsp; &nbsp; for(String ip: srcHostList) {&nbsp; &nbsp; &nbsp; &nbsp;boolean isOnline = true;&nbsp; &nbsp; &nbsp; &nbsp;try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// TODO check online&nbsp; &nbsp; &nbsp; &nbsp;}catch(Exception ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// TODO handle exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isOnline = false;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if(!isOnline) offlineList.add(ip);&nbsp; &nbsp; }&nbsp; &nbsp; srcHostList.removeAll(offlineList);}
随时随地看视频慕课网APP

相关分类

Java
我要回答