2012-07-25 152 views
1

這是我的代碼,我有一個簡單的問題,它應該是什麼樣子IF語句條件

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聲明中做錯了嗎?

+1

檢查['Date.compareTo']的javadocs(http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#compareTo%28java.util.Date% 29),它不能保證是>或<範圍的值,只是>或<0 – 2012-07-25 06:04:07

+1

我是* sooo *添加[tag:homework]標籤... – Bohemian 2012-07-25 06:13:12

+0

我們是否有類似的問題幾天之前? – MadProgrammer 2012-07-25 06:32:47

回答

1

如果你是比較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())); 
1

compareTo方法不會返回您所期望的。如果第一個參數較少,則只能保證返回一個負數,如果它們較大則保證爲正,如果相等則保證爲零。

編輯:這裏是你如何可以改變它,這樣你的代碼工作:

Date now = new Date(System.currentTimeMillis()); 
Date endDate = new Date(now.getTime()); 
endDate.SetYear(endDate.getYear() + 3); 
if (cc.getExpiryDate().after(now) && cc.ExpiryDate.before(endDate)) { 
// do stuff. 
} 

你將不得不小心處理邊緣情形(你應該包括和區間的兩端等),但這應該做爲方法。

+0

嘖嘖,我們在這裏試圖不告訴他答案;) – MadProgrammer 2012-07-25 06:14:14

+1

@MadProgrammer我通常避免只把答案告訴給家庭作業。這似乎是人們必須在工作中完成的完全有效的任務。如果我對某些語言沒有經驗,但我需要知道如何處理它,我寧願得到一個答案而不是小費,因爲理解小費可能需要更長的時間,然後自己提出一個解決方案。 – 2012-07-25 06:18:10

+0

乾草,這裏沒有問題,但學習如何解決問題的最好方法之一是先從一點開始,看看它在哪裏。 JMO – MadProgrammer 2012-07-25 06:32:09

1

我覺得你的整個邏輯是關閉的。您應該根據日期today + nYears而不是expiryDate + nYears比較信用卡到期日。

看看Date.afterDate.equalsDate.before

0

看Date.compareTo()方法的Java文檔...

返回: 值0,如果該參數日期等於這個日期;如果此日期在Date參數之前,則值小於0;如果此Date在Date參數之後,則值大於0。

但是這並不能爲您提供幾年的差異。它會給你只有-1,0或1.

作爲一個解決方案,你需要提取一年的日期,然後進行比較。