这个程序为什么不工作
我被要求编写一些基于某些指令的程序(类)。我觉得我几乎崩溃了,但我正在做一些愚蠢的事情。我无法弄清楚如何将一个连字符添加到符号常量中,以便当我输入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。带有单个数字的月份和日期填充前导零。 “
任何帮助任何将不胜感激,即使只是一个想法:)谢谢。
你应该使用单引号:
public static final char INSET_HYPHEN = '-';
fullDate = String.format("%d-%02d-%02d", year, month, day);
char
类型只拥有用单引号数据。如果你想使用双引号那么你的数据类型应该是string
除了你正在试图访问一个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
}
whatIsMonth,whatIsDay是LOC在你的构造函数中定义的变量,你不能在构造函数外使用这些变量。
您的getFullDate()方法应该使用您的实例变量year
,month
和'day'。请注意,int whatIsYear, int whatIsMonth, int whatIsDay
只是构造函数参数,仅限于您的构造函数。你不能在构造函数之外访问它们。
你不能添加一个字符串,如果你想使用如上的东西: 那就试试这个
public static final CHARACTER INSET_HYPHEN = new CHARACTER('-');
或
public static final char INSET_HYPHEN = '-';
为什么你想厌倦创造新的对象?你什么时候可以用“=”做? – 2013-02-11 12:01:31
- 使用
public static final char INSET_HYPHEN = '-';
,而不是public static final char INSET_HYPHEN = -;
- 人物应该总是在撇号之间定义。 - 命名常量“INSERT_HYPHEN”没有意义。常量的思想是让后面的代码更容易改变它的值,因此常量的名称应该代表它的值的“滚动”,例如:“SEPARATOR”或“DATE_FORMAT_SEPERATOR”。
- 无论如何,如果任何值应该是常数,它是年,月和日的默认值。
- 您的默认构造函数几乎重复了需要年,月和日的构造方法的代码,在这种情况下,通常最好使用this(默认参数)从默认值中调用第二个。
-
getFullDate()
的逻辑错误,反正让String.format
为你做脏活更好。另外,getFullDate()
返回year + month + day
,这是一个整数,而不是返回fullDate
。 - 您可能会觉得很困惑,但最好用与目标字段相同的方式命名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;
}
}
使用单引号一个char''-''。零应该最好也是字符。 – 2013-02-11 11:56:30