首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java,在if语句中比较分钟-未获得所需的结果

Java,在if语句中比较分钟-未获得所需的结果
EN

Stack Overflow用户
提问于 2018-06-09 00:22:21
回答 4查看 56关注 0票数 0

也许这只是我目前的精神状态/沮丧,但我的谷歌搜索和stackoverflow结果对我没有帮助。

我想验证时间输入的分钟部分是否等于00或30。

我还在掌握Java和一般的编程,所以有时我不知道正确的术语来开始搜索,所以如果有人问了我这个问题并回答了我,我向你道歉。

任何答案都应该在初学者和中级之间-如果我不明白,我会在回复之前尝试自己弄清楚。

这是我最近一次尝试的片段:

代码语言:javascript
复制
else if(timePart.getMinute() != 0 || 
        timePart.getMinute() != 30 ||
        appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 0 ||
        appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 30)
{
    System.out.println(timePart.getMinute());
    System.out.println(appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute());
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("Correct the minutes");
    alert.setHeaderText(null);
    alert.setContentText("You are only allowed to enter the start and end time minutes on the hour or half hour (ie :00 or :30).");
    alert.showAndWait();
}

我添加System.out只是为了测试不同的结果--例如,当上午9:00时,两者都会打印0;当上午9:30时,都会打印30。

不确定我做错了什么,但我的警报总是弹出,即使输入了有效的时间。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-09 00:30:31

你的条件总是正确的。

代码语言:javascript
复制
    if (timePart.getMinute() != 0 || 
        timePart.getMinute() != 30 || 
        appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 0 ||
        appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 30 )

它总是弹出,因为分钟总是不同于0或不同于30。

例如,当timePart.getMinute()为0时,它就不同于30,反之亦然。只有在以下情况下才应显示弹出窗口:

代码语言:javascript
复制
  if ((timePart.getMinute() == 0 || timePart.getMinute() == 30 || ...) == false)

或更短

代码语言:javascript
复制
 if (!(timePart.getMinute() == 0 || timePart.getMinute() == 30 ||...))

或将或更改为and:

代码语言:javascript
复制
 if (timePart.getMinute() != 0 && timePart.getMinute() != 30 || ...)
票数 3
EN

Stack Overflow用户

发布于 2018-06-09 00:31:27

更改为and比较-而不是or:

代码语言:javascript
复制
else if(timePart.getMinute() != 0 && timePart.getMinute() != 30 &&
                    appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 0 &&
                    appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute() != 30
                    )
            {
                System.out.println(timePart.getMinute());
                System.out.println(appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.systemDefault()).getMinute());
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Correct the minutes");
                alert.setHeaderText(null);
                alert.setContentText("You are only allowed to enter the start and end time minutes on the hour or half hour (ie :00 or :30).");
                alert.showAndWait();
            }
票数 1
EN

Stack Overflow用户

发布于 2018-06-09 01:17:56

要检查分钟数是否可以被30整除,可以使用%运算符。这样可以避免同时检查030的分钟值

代码语言:javascript
复制
else if(timePart.getMinute() % 30 != 0
        || appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.systemDefault()).getMinute() % 30 != 0) {
    ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50764791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档