gpt4 book ai didi

java - IF 语句条件

转载 作者:行者123 更新时间:2023-11-29 05:57:06 25 4
gpt4 key购买 nike

这是我的代码,我有一个简单的问题

import java.util.*;
import java.io.*;
import type.lib.GlobalCredit;
import type.lib.CreditCard;
import java.text.SimpleDateFormat;


public class eCheck08A

{
public static void main(String[] args)

{
PrintStream out = System.out;
Scanner in = new Scanner(System.in);

GlobalCredit credit1 = new GlobalCredit().getRandom();

out.print("Enter report range in years ... ");
int range = in.nextInt();
out.println("Cards expiring before " + range + " year(s) from now: ");

SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");

for (CreditCard cc : credit1)
{

Calendar c = Calendar.getInstance();
c.setTime(cc.getExpiryDate());
c.add(Calendar.YEAR, range);
Date newDate = c.getTime();

if (cc.getExpiryDate().compareTo(newDate) < range)
{
if(cc.getExpiryDate().compareTo(newDate) > range)
{
out.print("*");
}
out.print(cc.getNumber());
out.println("\t" + sf.format(cc.getExpiryDate()));

}
}



}
}

它应该是什么样子的输出:

Enter report range in years ... 3
Cards expiring before 3 years from now:

561561-8 20/11/2015
045645-7 22/02/2017
456462-3 16/04/2013 *
546548-5 19/08/2016

当前年份是 2012 年此人输入“3”作为范围。所以 2012-2015 年的任何一年都应该有一个“*”。与上面的输出一样,2013 年有一个“*”。你能告诉我在我的 IF 语句中做错了什么吗?

最佳答案

如果您将 cc.getExpiryDate()当前日期 + 范围 进行比较,您希望 newDate 为:

Calendar c = Calendar.getInstance();
// commenting this line out because getInstance() gives us the current date already
// c.setTime(cc.getExpiryDate());
c.add(Calendar.YEAR, range);
Date newDate = c.getTime();

newDate 比当前日期提前“范围”年。现在您可以开始比较您的 cc.getExpiryDate() 值:

    // expiry date is BEFORE the date + "range" years ahead
if (cc.getExpiryDate().compareTo(newDate) < 0)
{
// the expiry date is AFTER or ON the current date
if(cc.getExpiryDate().compareTo(new Date()) >= 0)
{
out.print("*");
}
}
out.print(cc.getNumber());
out.println("\t" + sf.format(cc.getExpiryDate()));

关于java - IF 语句条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com