Java类';月份';-仍然没有得到回报。。。还有其他一些事情
新的一周,新的任务-新的麻烦我需要帮助!任务是: 编写一个名为Month的类。该类应该有一个名为monthNumber的int字段,该字段保存月份的数字。例如,一月是1,二月是2,依此类推。该类还应该有一个名为lastMonthCreated的静态字段,该字段保存上次构造的月份数。此外,提供以下方法: 将monthNumber字段设置为1的无参数构造函数。 接受月数作为参数的构造函数。它应该将monthNumber字段设置为作为参数传递的值。如果传递的值小于1或大于12,则构造函数应将monthNumber设置为1。 接受月份名称(如“一月”或“二月”)作为参数的构造函数。它应该将monthNumber字段设置为正确的对应值。 一种setMonthNumber方法,它接受分配给monthNumber字段的int参数。如果传递的值小于1或大于12,则该方法应将monthNumber设置为1。 返回monthNumber字段中的值的getMonthNumber方法。 返回月份名称的getMonthName方法。例如,如果monthNumber字段包含1,则此方法应返回“一月”。 getLastMonthCreated方法,返回lastMonthCreated字段中的值。 返回与getMonthName方法相同值的toString方法。 接受月对象作为参数的equals方法。如果参数对象包含与调用对象相同的数据,则此方法应返回true。否则,它应该返回false。 接受Month对象作为参数的greaterThan方法。如果调用对象的monthNumber字段大于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。 接受Month对象作为参数的lessThan方法。如果调用对象的monthNumber字段小于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。 通过使用一组三个测试程序(MonthDemo1.java、MonthDemo2.java和MonthDemo3.java)演示Month类,这些程序可以从分配链接下方的MonthDemo文件夹下载 基本上我在这方面有过一次尝试(希望是一次很好的尝试),我在编译的时候遇到了很多错误——从48个到6个 错误: 第47行:意外类型monthNumber=(monthName[(索引+1)+]) ^错误指向索引-所以我尝试创建一个名为index的变量并在使用它之前初始化,但没有任何更改。 必需:变量 类型:值 第91行:不兼容的类型,如果(monthName=monthName[index]),字符串无法转换为字符串[] ^我知道这是什么意思,但不知道如何改变它,以得到我需要的 ^同样的错误再次出现在同一行上 第92行:找不到返回getLastCreated的符号;` ^我在顶部定义了这个,但没有初始化-如果我这样做会有帮助吗?我尝试初始化为0和null,但出现错误 第93行还是一样的 基本上,我在寻找两件事,首先是关于如何处理这些错误的任何提示,一般来说,一个关于我是否在正确的轨道上,或者是否有需要更改的部分使其工作的想法。如果您建议更改,您是否介意建议实际的代码并解释原因,因为我是一个新手,希望从建议中学习,而不仅仅是实现 非常感谢您的帮助——自从启动Java以来,在过去的几周里,我从这个论坛学到了很多 PS-将我的方法的大小写改为小写-现在就把它们改为大写,因为我发现它们更容易看到Java类';月份';-仍然没有得到回报。。。还有其他一些事情,java,arrays,Java,Arrays,新的一周,新的任务-新的麻烦我需要帮助!任务是: 编写一个名为Month的类。该类应该有一个名为monthNumber的int字段,该字段保存月份的数字。例如,一月是1,二月是2,依此类推。该类还应该有一个名为lastMonthCreated的静态字段,该字段保存上次构造的月份数。此外,提供以下方法: 将monthNumber字段设置为1的无参数构造函数。 接受月数作为参数的构造函数。它应该将monthNumber字段设置为作为参数传递的值。如果传递的值小于1或大于12,则构造函数应将month
public class Month
{
private int monthNumber;
String[] monthName = {
"January", "Februry", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"
}; //Months.
int MonthNumberInt = 0;
public static String lastMonthCreated;
/**
A no-arg constructor that sets the monthNumber field to 1.
*/
public Month() {
monthNumber = 1;
}
/**
A constructor that accepts the number of the month as an argument. It
should set the monthNumber field to
the value passed as the argument. If a value less than 1 or greater than
12 is passed, the constructor
should set monthNumber to 1.
*/
public Month(int monthNumber) {
if ((monthNumber < 1) || (monthNumber > 12)) {
monthNumber = 1;
} else {
monthNumber = monthNumber;
}
}
/**
A constructor that accepts the name of the month, such as "January" or
"February" as an argument. It should
set the monthNumber field to the correct corresponding value.
*/
public String Month(String monthName[]) {
int index = 0;
monthNumber = (monthName[(index + 1)++]);
}
/**
A setMonthNumber method that accepts an int argument, which is assigned
to the monthNumber field. If a value
less than 1 or greater than 12 is passed, the method should set
monthNumber to 1.
*/
public int setMonthNumber(int monthNumberInt) {
if ((monthNumber < 1) || (monthNumber > 12)) {
return monthNumber = 1;
}
monthNumberInt = monthNumber;
}
/**
A getMonthNumber method that returns the value in the monthNumber field.
*/
public int getMonthNumber() {
return monthNumber;
}
/**
A getMonthName method that returns the name of the month. For example, if
the monthNumber field contains 1,
then this method should return "January".
*/
public String getMonthName() {
return monthName[monthNumber - 1];
}
/**
A getLastMonthCreated method that returns the value in the
lastMonthCreated field.
*/
public String getLastMonthCreated() {
for (int index = 0; index < monthName.length; index++) //Check through
the array. {
if (monthName = monthName[index]) getLastCreated = monthName[index - 1];
return getLastCreated;
}
}
/**
A toString method that returns the same value as the getMonthName method.
*/
public String toString() {
String str = "monthName: " + getMonthName();
}
/**
An equals method that accepts a Month object as an argument. If the
argument object holds the same data as the
calling object, this method should return true. Otherwise, it should
return false.
*/
public boolean Equals(Month m1) //Month needs to go here.
{
if (monthNumber == m1.getMonthNumber()) return true;
else return false;
}
/**
A greaterThan method that accepts a Month object as an argument. If the
calling object's monthNumber field
is greater than the argument's monthNumber field, this method should
return true. Otherwise, it should
return false.
*/
public boolean GreatThan(Month m1) //Month needs to go here.
{
if (monthNumber > m1.monthNumber) return true;
else return false;
}
/**
A lessThan method that accepts a Month object as an argument. If the
calling object's monthNumber field is
less than the argument's monthNumber field, this method should return
true. Otherwise, it should return
false.
*/
public boolean LesserThan(Month m1) //Month needs to go here.
{
if (monthNumber < m1.monthNumber) return true;
else return false;
}
}
演示3
public class MonthDemo3
{
public static void main(String[] args)
{
// Use the 3rd constructor to create three objects.
Month m1 = new Month("March");
Month m2 = new Month("December");
Month m3 = new Month("Bad Month");
System.out.println("Month " + m1.getMonthNumber() +
" is " + m1);
System.out.println("Month " + m2.getMonthNumber() +
" is " + m2);
System.out.println("Month " + m3.getMonthNumber() +
" is " + m3);
Month m4 = new Month("May");
System.out.println("The last month created" +
" is " + m4.getLastMonthCreated());
}
}
我只能说你很困惑。您的代码有两个问题:
公共字符串Month(字符串monthName[])
monthNumber
和MonthNumberInt
李>
MonthNumberInt
应该是MonthNumberInt
Month
的新实例,因为您不能每个月使用相同的实例StackOverflow
之前,代码应该格式化好,以便其他人可以查看public class Month {
String[] monthName = { "January", "Februry", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" }; //Months.
int monthNumberInt = 0;
public static String lastMonthCreated;
/**
A no-arg constructor that sets the monthNumber field to 1.
*/
public Month()
{
monthNumberInt = 1;
}
/**
A constructor that accepts the number of the month as an argument. It
should set the monthNumber field to
the value passed as the argument. If a value less than 1 or greater than
12 is passed, the constructor
should set monthNumber to 1.
*/
public Month(int monthNumber)
{
if((monthNumber < 1 ) || ( monthNumber > 12)) {
this.monthNumberInt = 1;
} else {
this.monthNumberInt = monthNumber;
}
}
public Month(String monthName)
{
monthNumberInt = monthName.indexOf(monthName);
}
public int getMonthNumberInt() {
return monthNumberInt;
}
public void setMonthNumberInt(int monthNumberInt) {
this.monthNumberInt = monthNumberInt;
}
/**
A toString method that returns the same value as the getMonthName method.
*/
public String toString()
{
return "monthName: " + monthName[monthNumberInt];
}
/**
An equals method that accepts a Month object as an argument. If the
argument object holds the same data as the
calling object, this method should return true. Otherwise, it should
return false.
*/
public boolean Equals(Month m)//Month needs to go here.
{
if(this.monthNumberInt == m.getMonthNumberInt())
return true;
else
return false;
}
/**
A greaterThan method that accepts a Month object as an argument. If the
calling object's monthNumber field
is greater than the argument's monthNumber field, this method should
return true. Otherwise, it should
return false.
*/
public boolean GreatThan(Month m1)//Month needs to go here.
{
if(monthNumberInt > m1.monthNumberInt)
return true;
else
return false;
}
/**
A lessThan method that accepts a Month object as an argument. If the
calling object's monthNumber field is
less than the argument's monthNumber field, this method should return
true. Otherwise, it should return
false.
*/
public boolean LesserThan(Month m1)//Month needs to go here.
{
if(monthNumberInt < m1.monthNumberInt)
return true;
else
return false;
}
}
公共课月{
字符串[]monthName={“一月”、“二月”、“三月”,
“四月”、“五月”、“六月”、“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”};//个月。
int monthNumberInt=0;
公共静态字符串lastMonthCreated;
/**
将monthNumber字段设置为1的无参数构造函数。
*/
公众月
{
monthNumberInt=1;
}
/**
接受月数作为参数的构造函数
应将monthNumber字段设置为
作为参数传递的值。如果值小于1或大于
如果传递了12,则构造函数
应将monthNumber设置为1。
*/
公众月(国际月数)
{
如果((月数<1)| |(月数>12)){
this.monthNumberInt=1;
}否则{
this.monthNumberInt=monthNumber;
}
}
公共月(字符串月名称)
{
monthNumberInt=monthName.indexOf(monthName);
}
public int getMonthNumberInt(){
返回monthNumberInt;
}
公共无效setMonthNumberInt(int monthNumberInt){
this.monthNumberInt=monthNumberInt;
}
/**
返回与getMonthName方法相同值的toString方法。
*/
public class MonthDemo3
{
public static void main(String[] args)
{
// Use the 3rd constructor to create three objects.
Month m1 = new Month("March");
Month m2 = new Month("December");
Month m3 = new Month("Bad Month");
System.out.println("Month " + m1.getMonthNumber() +
" is " + m1);
System.out.println("Month " + m2.getMonthNumber() +
" is " + m2);
System.out.println("Month " + m3.getMonthNumber() +
" is " + m3);
Month m4 = new Month("May");
System.out.println("The last month created" +
" is " + m4.getLastMonthCreated());
}
}
public class Month {
String[] monthName = { "January", "Februry", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" }; //Months.
int monthNumberInt = 0;
public static String lastMonthCreated;
/**
A no-arg constructor that sets the monthNumber field to 1.
*/
public Month()
{
monthNumberInt = 1;
}
/**
A constructor that accepts the number of the month as an argument. It
should set the monthNumber field to
the value passed as the argument. If a value less than 1 or greater than
12 is passed, the constructor
should set monthNumber to 1.
*/
public Month(int monthNumber)
{
if((monthNumber < 1 ) || ( monthNumber > 12)) {
this.monthNumberInt = 1;
} else {
this.monthNumberInt = monthNumber;
}
}
public Month(String monthName)
{
monthNumberInt = monthName.indexOf(monthName);
}
public int getMonthNumberInt() {
return monthNumberInt;
}
public void setMonthNumberInt(int monthNumberInt) {
this.monthNumberInt = monthNumberInt;
}
/**
A toString method that returns the same value as the getMonthName method.
*/
public String toString()
{
return "monthName: " + monthName[monthNumberInt];
}
/**
An equals method that accepts a Month object as an argument. If the
argument object holds the same data as the
calling object, this method should return true. Otherwise, it should
return false.
*/
public boolean Equals(Month m)//Month needs to go here.
{
if(this.monthNumberInt == m.getMonthNumberInt())
return true;
else
return false;
}
/**
A greaterThan method that accepts a Month object as an argument. If the
calling object's monthNumber field
is greater than the argument's monthNumber field, this method should
return true. Otherwise, it should
return false.
*/
public boolean GreatThan(Month m1)//Month needs to go here.
{
if(monthNumberInt > m1.monthNumberInt)
return true;
else
return false;
}
/**
A lessThan method that accepts a Month object as an argument. If the
calling object's monthNumber field is
less than the argument's monthNumber field, this method should return
true. Otherwise, it should return
false.
*/
public boolean LesserThan(Month m1)//Month needs to go here.
{
if(monthNumberInt < m1.monthNumberInt)
return true;
else
return false;
}
}
public static void main(String[] args) {
Month m;
// Set the month number to the values 0 through 12 // (0 is invalid), and display the resulting month name.
for (int i = 0; i <12; i++) {
m = new Month();
m.setMonthNumberInt(i);
System.out.println("Month " + m.getMonthNumberInt() + " is " + m);
}
}
String[] monthName = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"};
Calendar cal = Calendar.getInstance();
String month = monthName[cal.get(Calendar.MONTH)];