简体   繁体   English

Java While Loop检查时间但不执行方法

[英]Java While Loop checks the time but not performing a method

In this code a while loop is supposed to check the time, and if the time is equal to 7 PM, then display a message box. 在这段代码中,一个while循环应该检查时间,如果时间等于7 PM,则显示一个消息框。

public void actionPerformed(ActionEvent arg0) {

    Enable.setEnabled(false);
    Date d = new Date();
    int hrs = d.getHours();
    int mins = d.getMinutes();

        while((1 + 1) == 2) { 
            if(hrs == 19 && mins == 21) {
                JOptionPane.showMessageDialog(frame,
                    "It's 7:21 PM!",
                    "Alerts",
                    JOptionPane.WARNING_MESSAGE);
                System.exit(0);
                break;
            }
        }
}

Pushing a button will do that code. 按下按钮将执行该代码。 If you push the button before it is 7 PM, the GUI will freeze (I don't care about that), and when it turns to 7 PM, it won't display the message box. 如果在晚上7点之前按下按钮,GUI将冻结(我不在乎),并且当它变为晚上7点时,将不会显示消息框。 If you click the button when it is 7 PM, then it will display the message box... 如果您单击该按钮时, 下午7点,那么它会显示消息框...

无需执行while((1 + 1)== 2)来获得无限循环的方法,您可以执行以下操作:while(true)或for(;;)如果没有关于您的小时和时间的任何其他信息或代码,此问题将无法回答。分钟变量

You need to update hrs and mins in your loop otherwise the initialization of them, I assume at the time of pushing the button will always hold the time when the button was pushed. 您需要在循环中更新hrsmins ,否则将对其进行初始化,我假设在按下按钮时始终保持按下按钮的时间。

This is why it works when pushed at 7pm but otherwise will not. 这就是为什么在晚上7点推送时可以正常运行的原因,否则无法正常运行。

So do something like this 所以做这样的事情

while(true) //Equivalent to what you had 
{ 
      if(hrs == 19 && mins == 00) {
           JOptionPane.showMessageDialog(frame,
               "It's 7:00 PM!",
               "Time Alert",
               JOptionPane.WARNING_MESSAGE);
           System.exit(0);
           break;
       }
       //Refresh your hrs and mins here
       Calendar calendar = GregorianCalendar.getInstance(); // Probably dont really want to actually get an instance every time
       hrs = calendar.get(Calendar.HOUR_OF_DAY);
       mins = calendar.get(Calendar.MINUTE);
}

I'm thinking you're using 1+1=2 to have a continuous loop. 我想您正在使用1 + 1 = 2进行连续循环。 you could use while(true){...} instead. 您可以改用while(true){...} Also, the while loop wouldn't be practical for this case. 同样,在这种情况下,while循环不切实际。 There is an example on this page that does something similar (Alarm Clock): http://www.ibm.com/developerworks/java/library/j-schedule/index.html 此页面上有一个类似的示例(闹钟): http : //www.ibm.com/developerworks/java/library/j-schedule/index.html

this was their example. 这就是他们的例子。 I updated it to suite your task. 我更新了它以适合您的任务。

public class AlarmClock {

    private final Scheduler scheduler = new Scheduler();
    private final SimpleDateFormat dateFormat =
        new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
    private final int hourOfDay, minute, second;

    public AlarmClock() {
        this.hourOfDay = 19;
        this.minute = 0;
        this.second = 0;
    }

    public void start() {
        scheduler.schedule(new SchedulerTask() {
            public void run() {
                soundAlarm();
            }
            private void soundAlarm() {
                JOptionPane.showMessageDialog(frame,
                            "It's 7:00 PM!",
                            "Time Alert",
                            JOptionPane.WARNING_MESSAGE);
                        System.exit(0);
                // Start a new thread to sound an alarm...
            }
        }, new DailyIterator(hourOfDay, minute, second));
    }

    public static void main(String[] args) {
        AlarmClock alarmClock = new AlarmClock();
        alarmClock.start();
    }
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM