简体   繁体   English

(Java)如果从未满足过for循环中if语句的条件,我该如何打印一条消息,指出“没有值满足此条件”?

[英](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.

相关问题 如果不执行do while循环条件,如何打印消息 - How to print a message if the do while loop condition is not met 如果满足 if 循环内部条件,如何退出 for 循环 - How to exit a for loop if the if loop inside condition is met 在嵌套的for循环内,打印字符串“。”,如果满足条件,则将其替换为其他字符。 MOOC Java第10周“地牢” - Inside nested for loop, print string “.” and replace it to other character if the condition are met. MOOC Java Week 10 'Dungeon' 在 Java 中,如果 3 个值中只有 2 个是 1、2 或 3,我该如何打印? - In Java, how do I print something if only 2 out of 3 values are 1, 2 or 3? 如果满足特定条件,如何更改 Java 中字符串中的元音? - How do I change vowels in a string in Java if a certain condition is met? 从ajax调用获取值后,如何在if条件中打印成功语句? - How do I print the successful statement in the if condition after getting values from an ajax call? 为什么即使不满足循环内设置的条件,我的循环错误消息仍会打印出来 - Why does my looped error message print out even though the condition set inside the loop is not satisfied 如何在java查询的循环中打印“resultList”并使用javascript打印出来? - how do i print a “resultList” in a loop from java query and also print it out using javascript? 找出奇数并满足Java中的条件 - Find out the odd value and meet criteria in java 如何检查是否从未使用过for循环内的if语句? - how to check if an if statement inside of a for loop is never used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM