【问题标题】:(Java) Trying to number printed items in an if statement(Java) 尝试在 if 语句中为打印的项目编号
【发布时间】:2015-08-02 06:11:35
【问题描述】:

所以,我在 for 循环中有一个 if 语句。这个想法是,如果当前时间和更新时间之间的时间差大于 24 小时(或 86400000 毫秒),那么我打印出索赔编号。

这就是我的 if 语句的样子:

   if(difference>86400000){                 
        System.out.println(singleClaim.getString("claimNumber"));
            } 

这就是我的输出(索赔编号列表):

032394115-01
032398720-01
032395941-01
032398165-01
032395262-01
032395350-01
032392831-01

由于我有很多索赔编号要打印出来,我希望对它们进行编号以使列表更易于阅读。

有点像这样:

1.) 032394115-01
2.) 032398720-01
3.) 032395941-01
4.) 032398165-01
5.) 032395262-01
6.) 032395350-01
7.) 032392831-01

希望比我更有经验的程序员会发现这相当容易。

编辑:有人告诉我,显示整个 for 循环会很有帮助,所以它在下面:

 for(ParseObject singleClaim : resultsList){
        //If the customerStatus is 5, we check to see if the updatedTime            is within 24 Hours.                 
    if(singleClaim.getInt("customerStatus") == 5) {
        //Create a date formatter.
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH':'mm':'ss'.'SSS'Z'");
        //Create a date object for today's date.
        Date currentDate=new Date();
        //Create a string for the date from parse.
        String parseTime = singleClaim.getString("updatedAt");            
        //Initialize the date object for the updatedAt time on Parse.
        Date parseDate = null;
        try {
        //Here, we convert the parseTime string into a date object and format it.
            parseDate = formatter.parse(parseTime);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }           
        //Get the time difference from the current date versus the date on Parse in milliseconds.
        long difference = currentDate.getTime() - parseDate.getTime();


        for(int i=1;;i++ ){
        //If the time difference is more than 24 hours in milliseconds, we print out the claim number.
        if(difference>86400000){                    
        System.out.println(i + ".)" + singleClaim.getString("claimNumber"));
            }     
        else {       
        break;};
        }
        }}
        System.out.println("\n////////////////////////////////////////////////////////");
        System.out.println("Done getting claims with customerStatus of 5 updated over 24 hours ago.");
        System.out.println("///////////////////////////////////////////////////////\n");
        }
        catch(Exception e){
        e.printStackTrace();
        }}}

【问题讨论】:

  • 按照“更易于阅读”的思路,但关于您的源代码:提取一种方法以使您的 if 条件为人类所理解:if (claimIsOlderThen24Hours(difference)) { ...

标签: java arrays list


【解决方案1】:

使用计数器:

if(difference>86400000) {                 
    System.out.println(counter++ + ".)" + singleClaim.getString("claimNumber"));
} 

在循环之前初始化计数器:

int counter = 1;

【讨论】:

    【解决方案2】:

    我也推荐int counter = 1;,但我建议使用printf(String, Object...) 格式化输出。类似的东西

    System.out.printf("%d.) %s%n", counter++, singleClaim.getString("claimNumber"));
    

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多