也许这只是我目前的精神状态/沮丧,但我的谷歌搜索和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只是为了测试不同的结果--例如,当上午9:00时,两者都会打印0;当上午9:30时,都会打印30。
不确定我做错了什么,但我的警报总是弹出,即使输入了有效的时间。
发布于 2018-06-09 00:30:31
你的条件总是正确的。
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 ||...))
或将或更改为and:
if (timePart.getMinute() != 0 && timePart.getMinute() != 30 || ...)
发布于 2018-06-09 00:31:27
更改为and比较-而不是or:
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();
}
发布于 2018-06-09 01:17:56
要检查分钟数是否可以被30
整除,可以使用%
运算符。这样可以避免同时检查0
和30
的分钟值
else if(timePart.getMinute() % 30 != 0
|| appointment.getSqlStartDateTime().toLocalDateTime().atZone(ZoneId.systemDefault()).getMinute() % 30 != 0) {
...
}
https://stackoverflow.com/questions/50764791
复制相似问题