也许这只是我目前的心态/沮丧,但我的谷歌搜索和 stackoverflow 结果对我没有帮助。
我想验证时间输入的分钟部分是否等于 00 或 30。
我仍在掌握 Java 和一般编程,所以有时我不知道开始搜索的正确术语,所以如果有人提出这个问题并得到回答,我深表歉意。
任何答案都应该介于初学者和中级之间 - 如果我不明白,我会在回复之前尝试自己弄清楚。
这是我最近尝试的一个片段:
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 只是为了测试不同的结果 - 例如,两者都会打印 0 表示上午 9:00,打印 30 表示上午 9:30。
不确定我做错了什么,但即使输入了有效时间,我的警报也总是弹出。
最佳答案
您的条件始终为真。
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 不同,反之亦然。 您应该仅在以下情况下显示弹出窗口:
if ((timePart.getMinute() == 0 || timePart.getMinute() == 30 || ...) == false)
或更短
if (!(timePart.getMinute() == 0 || timePart.getMinute() == 30 ||...))
或者将您的 or 更改为 and:
if (timePart.getMinute() != 0 && timePart.getMinute() != 30 || ...)
关于Java,比较 if 语句中的分钟 - 没有得到期望的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50764791/