Java 类 'Month' - 仍然没有得到返回......以及其他一些事情

标签 java arrays

新的一周,新的任务 - 我需要帮助解决新的麻烦!作业是: 编写一个名为 Month 的类。该类应该有一个名为 MonthNumber 的 int 字段,用于保存月份数。例如,一月为 1,二月为 2,依此类推。该类还应该有一个名为lastMonthCreated 的静态字段,它保存上次构造的月份数。另外,提供以下方法:

一个无参数构造函数,将 MonthNumber 字段设置为 1。 接受月份数字作为参数的构造函数。它应该将 MonthNumber 字段设置为作为参数传递的值。如果传递的值小于 1 或大于 12,则构造函数应将 MonthNumber 设置为 1。 接受月份名称(例如“January”或“February”)作为参数的构造函数。它应该将 MonthNumber 字段设置为正确的对应值。 一个接受 int 参数的 setMonthNumber 方法,该参数被分配给 MonthNumber 字段。如果传递的值小于 1 或大于 12,则该方法应将 MonthNumber 设置为 1。 返回 MonthNumber 字段中的值的 getMonthNumber 方法。 返回月份名称的 getMonthName 方法。例如,如果 MonthNumber 字段包含 1,则此方法应返回“January”。 getLastMonthCreated 方法,返回 lastMonthCreated 字段中的值。 返回与 getMonthName 方法相同值的 toString 方法。 接受 Month 对象作为参数的 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 个。

错误:

Line 47: unexpected type monthNumber = (monthName[(index + 1)++]);

^ 错误指向索引 - 所以我尝试创建一个名为索引的变量并在使用它之前进行初始化,但没有任何变化。 必需:变量 类型:值

Line 91: incompatible types, String cannot be converted to String[] if (monthName = monthName[index])

^ 我明白这句话的意思,但不知道如何更改它以获得我需要的内容。

^ 同一行再次出现同样的错误

Line 92: cannot find symbol return getLastCreated;`

^ 我在顶部定义了它,但没有初始化 - 如果我这样做会有帮助吗?我尝试初始化为 0 和 null,但出现错误。

第 93 行再次相同

基本上我正在寻找两件事,首先是有关如何处理这些错误的任何提示,一般来说,我是否在正确的轨道上,或者是否有部分需要更改让它起作用。如果您建议进行更改,您是否介意建议实际代码并解释原因,因为我真的很新,希望从建议中学习而不仅仅是实现。

非常感谢您的所有帮助 - 自从开始使用 Java 以来,在过去的几周里我从这个论坛学到了很多东西!

PS - 将我的方法的大小写更改为较低 - 现在只需将它们放在上面,因为我发现它们更容易看到。

    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;
    }
}

演示1

 public class MonthDemo1
    {
    public static void main(String[] args)
    {
      // Use the no-arg constructor.
      Month m = new Month();
      System.out.println("Month " + m.getMonthNumber() +
                         " is " + 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.setMonthNumber(i);
         System.out.println("Month " + m.getMonthNumber() +
                         " is " + m);
      }
       }
    }

演示2

public class MonthDemo2
    {
    public static void main(String[] args)
    {
      // Use the 2nd constructor to create two objects.
      Month m1 = new Month(10);
      Month m2 = new Month(5);
      System.out.println("Month " + m1.getMonthNumber() +
                         " is " + m1);
      System.out.println("Month " + m2.getMonthNumber() +
                         " is " + m2);

      // Test for equality.
      if (m1.equals(m2))
         System.out.println(m1 + " and " + m2 + " are equal.");
      else
         System.out.println(m1 + " and " + m2 + " are NOT equal.");

      // Is m1 greater than m2?
      if (m1.greaterThan(m2))
         System.out.println(m1 + " is greater than " + m2);
      else
         System.out.println(m1 + " is NOT greater than " + m2);

      // Is m1 less than m2?
      if (m1.lessThan(m2))
         System.out.println(m1 + " is less than " + m2);
      else
         System.out.println(m1 + " is NOT less than " + m2);
      }
     }

演示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());
      }
    }

最佳答案

我只能说你很困惑。您的代码存在几个问题:

  1. 构造函数没有返回类型public String Month(String MonthName[])
  2. 您不需要两个整数 monthNumberMonthNumberInt
  3. 类的字段必须采用驼峰式大小写字母。例如,MonthNumberInt 应为 monthNumberInt
  4. 我已经修复了一些逻辑问题。您需要在循环中创建 Month 的新实例,因为您不能每个月都使用相同的实例。
  5. 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;
    }
}

我测试它为:

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);
    }
    }

输出是:

月份 0 是月份名称:一月 第 1 个月是月份名称:Februry 第 2 个月是月份名称:三月 第 3 个月是月份名称:四月 第 4 个月是月份名称:五月 第 5 个月是月份名称:六月 第 6 个月是月份名称:七月 第 7 个月是月份名称:八月 第 8 个月是月份名称: 九月 第 9 个月是月份名称:十月 第 10 个月是月份名称: 十一月 第 11 个月是月份名称:12 月

关于Java 类 'Month' - 仍然没有得到返回......以及其他一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146618/

相关文章:

java - 将初始 Perm 大小和最大 Perm 大小设置为相同以防止 Full GC

java - MySQL 列名中的重音字符

python - 如何在python中将数组字符串转换为数组

c - 如何在初始化后或循环中第二次将数组的所有元素设置为 0?

java - 从 HashMap 获取最大 Set 大小

java - Guice 子模块阻止继承绑定(bind)

java - 启动 opencsv 时出现问题

javascript - 字符串化数组是一个好习惯吗?

java - 对象数组的 getter 和 setter?

javascript - 在 JavaScript 中对除一个元素之外的数组进行排序