2013-02-11 121 views
0

我被要求編寫一些基於某些指令的程序(類)。我覺得我幾乎崩潰了,但我正在做一些愚蠢的事情。我無法弄清楚如何將一個連字符添加到符號常量中,以便當我輸入INSERT_HYPHEN時,它會在訪問器方法中插入一個「 - 」。它說不兼容的類型> :(當我嘗試將本地變量「fullDate」插入到'getFullDate'訪問器方法中,然後放置「fullDate =年+月+日」時,它表示'不兼容的類型!也許是因爲這accesor方法是一個字符串,我想裏面添加它的整數。「我找不到辦法解決它。這裏是我的代碼。這個程序爲什麼不工作

public class Date 
{ 
public static final int INSERT_ZERO = 0; 
public static final char INSET_HYPHEN = -; //ERROR incompatible types 

// instance variables - replace the example below with your own 
private int year; 
private int month; 
private int day; 

/** 
* Default constructor 
*/ 
public Date() 
{ 
    setYear (2013); 
    setMonth (01); 
    setDay (01); 
} 

/** 
* 
*/ 
public Date (int whatIsYear, int whatIsMonth, int whatIsDay) 
{ 
    setYear (whatIsYear); 
    setMonth (whatIsMonth); 
    setDay (whatIsDay); 
} 

/** 
*@return year 
*/ 
public int getYear() 
{ 
    return year; 
} 

/** 
*@return month 
*/ 
public int getMonth() 
{ 
    return month; 
} 

/** 
*@return day 
*/ 
public int getDay() 
{ 
    return day; 
} 

/** 
*@return 
*/ 
public String getFullDate() 
{ 
    String fullDate; 
    if (whatIsMonth < 10); // the year, month, and day all give me incompatible types :(
    { 
     fullDate = year + INSERT_HYPHEN + INSERT_ZERO + month + INSERT_HYPHEN + day; 
    } 
    if (whatIsDay < 10); 
    { 
     fullDate = year + INSERT_HYPHEN + month + INSERT_HYPHEN + INSERT_ZERO + day; 
    } 
    else 
    { 
     fullDate = year + INSERT_HYPHEN + month + INSERT_HYPHEN + day; 
    } 
    return year + month + day; 
} 

/** 
* 
*/ 
public void setYear (int whatIsYear) 
{ 
    if ((whatIsYear >= 1990) && (whatIsYear <= 2013)) 
    { 
     year = whatIsYear; 
    } 
    else 
    { 
     year = 2013; 
    } 
} 

/** 
* 
*/ 
public void setMonth (int whatIsMonth) 
{ 
    if ((whatIsMonth >= 1) && (whatIsMonth <= 12)) 
    { 
     month = whatIsMonth; 
    } 
    else 
    { 
     month = 01; 
    } 
} 

/** 
* 
*/ 
public void setDay (int whatIsDay) 
{ 
    if ((whatIsDay >= 1) && (whatIsDay <= 31)) 
    { 
     day = whatIsDay; 
    } 
    else 
    { 
     day = 01; 
    } 
} 

}

只是一些更多的背景。我正在構建的課程有三個字段,年份,月份和日期,年份可以在1900年到今年之間,包括1月和12月之間,可以在1到12之間,包括天數在內和1到31之間。使用符號con而不是代碼中的「魔術」數字,例如public static final int FIRST_MONTH = 1;

默認構造函數將year設置爲當前年份,將月份設置爲第一個月份,將日期設置爲第一天。非默認構造函數測試每個參數。如果year參數超出了可接受的範圍,它將該字段設置爲當前年份。如果月份參數超出了可接受的範圍,它會將字段設置爲第一個月份。如果day參數超出了可接受的範圍,它將字段設置爲第一天。

每個字段都有一個存取方法和一個增變方法。所有三種增變器方法都檢查其參數的有效性,如果無效,則以與非默認構造器相同的方式設置相應的字段。

這是我遇到麻煩的部分。我必須包含一個名爲「public String getFullDate()」的方法,它返回一個字符串,格式爲:YYYY-MM-DD,例如2012-01-01。帶有單個數字的月份和日期填充前導零。 「

任何幫助任何將不勝感激,即使只是一個想法:)謝謝。

+0

使用單引號一個char''-''。零應該最好也是字符。 – 2013-02-11 11:56:30

回答

6

你應該使用單引號:

public static final char INSET_HYPHEN = '-'; 
1
fullDate = String.format("%d-%02d-%02d", year, month, day); 
0

char類型只擁有用單引號數據。如果你想使用雙引號那麼你的數據類型應該是string

0

除了你正在試圖訪問一個getFullDate() method

公開日期在構造函數中聲明的局部變量不正確聲明一個字符內的語法錯誤(INT whatIsYear,INT whatIsMonth,INT whatIsDay)

public String getFullDate() 
{ 
    String fullDate; 
    if (whatIsMonth < 10); // the year, month, and day all give me incompatible 
    //rest of the code 
} 

whatIsMonthwhatIsDay是LOC在你的構造函數中定義的變量,你不能在構造函數外使用這些變量。

您的getFullDate()方法應該使用您的實例變量yearmonth和'day'。請注意,int whatIsYear, int whatIsMonth, int whatIsDay只是構造函數參數,僅限於您的構造函數。你不能在構造函數之外訪問它們。

0

你不能添加一個字符串,如果你想使用如上的東西: 那就試試這個

public static final CHARACTER INSET_HYPHEN = new CHARACTER('-'); 

public static final char INSET_HYPHEN = '-'; 
+1

爲什麼你想厭倦創造新的對象?你什麼時候可以用「=」做? – 2013-02-11 12:01:31

0
  1. 使用public static final char INSET_HYPHEN = '-';,而不是public static final char INSET_HYPHEN = -; - 人物應該總是在撇號之間定義。
  2. 命名常量「INSERT_HYPHEN」沒有意義。常量的思想是讓後面的代碼更容易改變它的值,因此常量的名稱應該代表它的值的「滾動」,例如:「SEPARATOR」或「DATE_FORMAT_SEPERATOR」。
  3. 無論如何,如果任何值應該是常數,它是年,月和日的默認值。
  4. 您的默認構造函數幾乎重複了需要年,月和日的構造方法的代碼,在這種情況下,通常最好使用this(默認參數)從默認值中調用第二個。
  5. getFullDate()的邏輯錯誤,反正讓String.format爲你做髒活更好。另外,getFullDate()返回year + month + day,這是一個整數,而不是返回fullDate
  6. 您可能會覺得很困惑,但最好用與目標字段相同的方式命名setter和constructors的參數,而不是創建一個新名稱。爲了避免兩者之間的不明確性,請使用此[.Field_NAME]來引用實例的字段。

此代碼應工作:

public class Date { 
    private static final int DEFAULT_YEAR = 2013; 
    private static final int DEFAULT_MONTH = 1; 
    private static final int DEFAULT_DAY = 1; 
    private static final char SEPARATOR = '-'; 

    private int year; 
    private int month; 
    private int day; 

    /** 
    * Default constructor 
    */ 
    public Date() { 
     this(DEFAULT_YEAR, DEFAULT_MONTH, DEFAULT_DAY); 
    } 

    /** 
    * 
    */ 
    public Date (int year, int month, int day) { 
     setYear(year); 
     setMonth(month); 
     setDay(day); 
    } 

    /** 
    *@return year 
    */ 
    public int getYear() { 
     return year; 
    } 

    /** 
    *@return month 
    */ 
    public int getMonth() { 
     return month; 
    } 

    /** 
    *@return day 
    */ 
    public int getDay() { 
     return day; 
    } 

    /** 
    *@return 
    */ 
    public String getFullDate() { 
     return String.format("%d%c%02d%c%02d", year, SEPARATOR, month, SEPARATOR, day); 
    } 

    /** 
    * 
    */ 
    public void setYear (int year) 
    { 
     this.year = ((year >= 1990) && (year <= DEFAULT_YEAR)) ? year : DEFAULT_YEAR; 
    } 

    /** 
    * 
    */ 
    public void setMonth (int month) 
    { 
     this.month = ((month >= 1) && (month <= 12)) ? month : DEFAULT_MONTH; 
    } 

    /** 
    * 
    */ 
    public void setDay (int day) 
    { 
     this.day = ((day >= 1) && (day <= 31)) ? day : DEFAULT_DAY; 
    } 
}