变量以某种方式失去价值? [英] Variables lose their value somehow?

查看:48
本文介绍了变量以某种方式失去价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行该程序时,结果始终为null, 0 0.为什么在调用getDay()方法并将其打印在屏幕上时monthNamedayyear的值不显示.

When I try to run this program, the result is always null, 0 0. Why do the values of monthName, day, and year not show up when the getDay() method is invoked and printed on the screen.

public class Assignment1 {

    public static void main(String[] args) {

        //creates an array of type Date filled with two LongDate objects
        Date [] collectionOfDates = { new LongDate("February",2,1996), new LongDate("February",13,1999) };

        // loops through the array and displays output of getDate() for each object
        for( int i = 0; i < collectionOfDates.length; i++ ) {
            System.out.println( collectionOfDates[i].getDate() );
        }

    }

}

供您参考,LongDate类是Date类的子类,其中包含方法editDay()editYear()以及其他几种方法. LongDate方法在下面列出.

For your information, the LongDate class is a subclass of the Date class which contains methods editDay() and editYear() along with several others. The LongDate method is listed below.

非常感谢任何帮助,谢谢.另外,如果您需要更多信息,请随时发表评论.

Any help greatly appreciated, Thanks. Also, feel free to comment if you want more information.

public class LongDate extends Date {

    private String monthName;
    private int day;
    private int year;

    public LongDate() {

    }

    public LongDate(String m, int d, int y) {

        super.editday(d);
        super.edityear(y);
        editMonth(m);

    }

    public void setDate(String m, int d, int y) {

        monthName = m;
        day = d;
        year = y;

    }


    public String getDate() {

        StringBuilder fullDate = new StringBuilder();
        fullDate.append(monthName);
        fullDate.append(" ");
        fullDate.append(day);
        fullDate.append(", ");
        fullDate.append(year);

        return fullDate.toString();
    }

    public String getShortDate() {

        int month = 0;

        if (monthName == "January") {
            month = 1;
        } else if (monthName == "February") {
            month = 2;
        } else if (monthName == "March") {
            month = 3;
        } else if (monthName == "April") {
            month = 4;
        } else if (monthName == "May") {
            month = 5;
        } else if (monthName == "June") {
            month = 6;
        } else if (monthName == "July") {
            month = 7;
        } else if (monthName == "August") {
            month = 8;
        } else if (monthName == "September") {
            month = 9;
        } else if (monthName == "October") {
            month = 10;
        } else if (monthName == "November") {
            month = 11;
        } else if (monthName == "December") {
            month = 12;
        }

        StringBuilder shortDate = new StringBuilder();
        shortDate.append(month);
        shortDate.append("/");
        shortDate.append(day);
        shortDate.append("/");
        shortDate.append(year);

        return shortDate.toString();
    }

    protected String editMonth(String m) {

        // asks person to try again if month is not capitalized and spelled properly
        if (m != "January" && m != "February" && m != "March" && m != "April" && m != "May" && m != "June" && m != "July" && m != "August" && m != "September" && m != "October" && m != "November" && m != "December") {
            m = Input.getString( "Invalid month. Please type the month again." );
            return m;
        } else
            return m;
    }
}

推荐答案

LongDate的构造函数中没有任何内容可设置getDate()读取的字段(monthNamedayyear).

There's nothing in the constructor of LongDate which sets the fields (monthName, day, and year) that getDate() reads.

我假设Date#editDay()Date#editYear()函数看起来与LongDate#editMonth()类似.请注意, editMonth()不会为monthName字段分配值!

I assume that the Date#editDay() and Date#editYear() functions look similar to LongDate#editMonth(). Note that editMonth() does not assign a value to the monthName field!

这篇关于变量以某种方式失去价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆