我正在学习java,我们的一个编程任务是创建一个名为month的类,这个类占用一个月的字符串并转换为其对应的int并获取月份数的int并将其转换为字符串或相应的名称。它还比较一个大于或小于另一个的月份数,这个类必须包含一个复制构造函数以及使用构造函数链接。
我的课程编译时没有错误,但问题是当我尝试在我的演示类中运行它时,它只会打印出每个println的May。
我认为问题在于toString,好像我添加了println来打印出打印出正确月份或月份数字的其他方法。下面是我的代码,我试图从学校的一位实验室助理那里得到一些帮助,但他们无法弄清楚问题是什么。提前谢谢!!
public class Month1
{
private static int monthNumber;
public Month1 ()
{
this(1);
}
public Month1(Month1 copy)
{
this(copy.monthNumber);
}
public Month1 (int n)
{
if (n > 12 || n < 1)
monthNumber = 1;
else monthNumber = n;
}
public Month1 (String monthName)
{
this(nameToNumber(monthName));
if (monthName.equals("January"))
monthNumber = 1;
else if (monthName.equals("February"))
monthNumber = 2;
else if (monthName.equals("March"))
monthNumber = 3;
else if (monthName.equals("April"))
monthNumber = 4;
else if (monthName.equals("May"))
monthNumber = 5;
else if (monthName.equals("June"))
monthNumber = 6;
else if (monthName.equals("July"))
monthNumber = 7;
else if (monthName.equals("August"))
monthNumber = 8;
else if (monthName.equals("September"))
monthNumber = 9;
else if (monthName.equals("October"))
monthNumber = 10;
else if (monthName.equals("November"))
monthNumber = 11;
else monthNumber = 12;
}
public static int nameToNumber(String monthName)
{
if (monthName.equals("January"))
monthNumber = 1;
else if (monthName.equals("February"))
monthNumber = 2;
else if (monthName.equals("March"))
monthNumber = 3;
else if (monthName.equals("April"))
monthNumber = 4;
else if (monthName.equals("May"))
monthNumber = 5;
else if (monthName.equals("June"))
monthNumber = 6;
else if (monthName.equals("July"))
monthNumber = 7;
else if (monthName.equals("August"))
monthNumber = 8;
else if (monthName.equals("September"))
monthNumber = 9;
else if (monthName.equals("October"))
monthNumber = 10;
else if (monthName.equals("November"))
monthNumber = 11;
else monthNumber = 12;
return monthNumber;
}
public void setMonthNumber (int n)
{
if (n > 12 || n < 1)
monthNumber = 1;
else monthNumber = n;
}
public int getMonthNumber ()
{
return monthNumber;
}
public String getMonthName (int n)
{
String monthName = "";
if (n == 1)
monthName = "January";
else if (n == 2)
monthName = "February";
else if (n == 3)
monthName = "March";
else if (n == 4)
monthName = "April";
else if (n == 5)
monthName = "May";
else if (n == 6)
monthName = "June";
else if (n == 7)
monthName = "July";
else if (n == 8)
monthName = "August";
else if (n == 9)
monthName = "Septemeber";
else if (n == 10)
monthName = "October";
else if (n == 11)
monthName = "November";
else monthName = "December";
return monthName;
}
public String toString ()
{
return getMonthName(monthNumber);
}
public boolean equals (Month1 m)
{
boolean result = false;
if (monthNumber == m.monthNumber)
result = true;
else result = false;
return result;
}
public boolean greaterThan(Month1 m1)
{
if(monthNumber > m1.monthNumber)
return true;
else
return false;
}
public boolean lessThan(Month1 m1)
{
if(monthNumber < m1.monthNumber)
return true;
else
return false;
}
}
import java.util.*;
public class MonthDemo
{
public static void main (String [] args)
{
Month1 month1 = new Month1(10);
Month1 month2 = new Month1("May");
System.out.println (month1);
System.out.println (month2);
// Test for equality.
if (month1.equals(month2))
System.out.println(month1 + " and " + month2 + " are the same month.");
else
System.out.println(month1 + " and " + month2 + " are NOT equal.");
// Is m1 greater than m2?
if (month1.greaterThan(month2))
System.out.println(month1 + " is greater than " + month2);
else
System.out.println(month1 + " is NOT greater than " + month2);
// Is m1 less than m2?
if (month1.lessThan(month2))
System.out.println(month1 + " is less than " + month2);
else
System.out.println(month1 + " is NOT less than " + month2);
// Use copy constructor
Month1 month3 = new Month1(month2);
// Test for equality.
if(month3.equals(month2))
System.out.println(month3 + " and " + month2 + " are the same month.");
else
System.out.println(month3 + " and " + month2 + " are NOT equal.");
}
}