[英](Java) If the condition for an if statement inside a for loop is never met, how do I print out a message saying “no values meet this criteria”?
So, I have an if statement inside of a for loop. 所以,我有一个for循环的if语句中。 The idea is, if the time difference between the current time and an updated time is greater than 24 hours (86400000 milliseconds), then I print out the claim number.
这个想法是,如果当前时间与更新时间之间的时差大于24小时(86400000毫秒),那么我将打印出索赔编号。
This what my if statement looks like: 我的if语句如下所示:
if(difference>86400000){
System.out.println(singleClaim.getString("claimNumber"));
}
and this is what my output looks like (a list of claim numbers with a certain status that have a time difference of over 24 hours): 这就是我的输出看起来像(带有一定的地位有24小时以上的时间差索赔号码列表):
032394115-01
032398720-01
032395941-01
032398165-01
032395262-01
032395350-01
032392831-01
Now, if there aren't any claim numbers with a certain status that have a time difference of over 24 hours, I want my output to look like this: 现在,如果不存在具有一定的地位有超过24小时的时差任何索赔数字,我想我的输出看起来像这样:
No claim numbers meet this criteria.
How would I add that in there? 我要如何在那添加呢?
I tried doing this: 我尝试这样做:
if(difference>86400000){
System.out.println(singleClaim.getString("claimNumber"));
}
else{
System.out.println("No claim numbers meet this criteria.");
}
and changed the data to make sure no claim numbers had a difference greater than 24 hours but this is what I got as my output (the message displayed over and over again instead of the claim numbers): 和更改的数据,以确保没有要求的数字有差异超过24小时,但是这是我得到了我的输出(显示的消息一遍又一遍,而不是要求号码):
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
You would have to create a flag to tell if a claim has met the conditions. 您将必须创建一个标志来告诉您索赔是否满足条件。 So outside your loop do something like:
因此,在循环外执行以下操作:
boolean claimMet = false;
and in the if-statement: 并在if语句中:
if(difference>86400000){
System.out.println(singleClaim.getString("claimNumber"));
claimMet = true;
}
then after the loop ends: 然后循环结束后:
if (!claimMet) {
System.out.println("No claim numbers meet this criteria.");
}
I'm not sure I understood what you said, but let's try to answer it if I get it right : 我不确定我是否理解您的意思,但是如果我理解正确,请尝试回答:
boolean meetCriteria = false;
for(int difference = 0; ; difference++) {
if(difference>86400000){
meetCriteria = true;
}
}
if(meetCriteria) {
System.out.println(singleClaim.getString("claimNumber"));
} else {
System.out.println("No claim numbers meet this criteria.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.