java中的数组和格式问题

时间:2018-04-17 01:13:02

标签: java arrays

如果有人能帮助我,我真的可以使用一些帮助,我一直在为我的计算机科学入门课程工作,但我已经错过了几个课程和避难所#由于意外的家庭悲剧,我最近有时间去学习,坦白地说,我不知道自己在做什么,现在没有多少时间赶上。我几乎没有错过阵列上的所有内容,也无法访问任何涉及该主题的课堂内容。

这是我现在的代码

c

以下是唯一接近提示的内容我已经做了如何使其工作,截至目前我的构造函数,setDayMaxTemp和displayWeather方法对我来说都不适用。< / p>

检查构造函数

症状:java.lang.NegativeArraySizeException 在WeatherMonth。(WeatherMonth.java:30)

check set day max temp

症状:java.lang.NullPointerException

TestPrelabB失败。方法displayWeatherMonth不正确。您的输出未按规定格式化为exaclty。月份名称为6月,天数为2.这是预期输出:六月日最大值1 -999 2 -999这是您的输出:确保首先打印月份名称。确保打印标题行。确保每一行都有一个数字和一个温度。确保选项卡分隔每行中的数据项,包括标题行。

这是我必须传递的测试代码

 * WeatherMonth.java 
 */

public class WeatherMonth
{

    private int [] maxTemperature;
    private String monthName;
    private int daysInMonth;


    public WeatherMonth()
    {
        monthName = "January";
        daysInMonth = 31;
        int [] maxTamperature = new int [daysInMonth];
        maxTemperature[0] = -999;
    }

    public WeatherMonth(String monthName, int daysInMonth)
    {
        this.monthName = monthName;
        this.daysInMonth = daysInMonth; 
        int days = maxTemperature.length;
        int [] maxTemperature = new int [days];
        maxTemperature[0] = -999;
        //int [] maxTemperature = [-999]
        //int [] maxTemperature.fill(daysInMonth, -999);
    }

    public int [] getMaxTemperature()
    {
        return maxTemperature;
    }

    public String getMonthName()
    {
        return monthName;
    }

    public int getDaysInMonth()
    {
        return daysInMonth;
    }

    public void setDayMaxTemp(int dayOfMonth, int temperature)
    {

    }

    public void setMaxTemperature(int [] maxTemperature)
    {
        this.maxTemperature = maxTemperature;
    }

    public void setMonthName(String monthName)
    {
        this.monthName = monthName;
    }

    public void setDaysInMonth(int daysInMonth)
    {
        this.daysInMonth = daysInMonth;
    }

    public void displayWeatherMonth()
    {

    }

    public void readMaxTempFile(String filename)
    {

    }

    public String toString()
    {
        return "";
    }

}

我真的很想为此寻求帮助而道歉,我现在感到很茫然,甚至不知道从哪里开始。对不起,如果这不是很清楚,或者我在这里要求的话太多了。我欢迎任何可能对我有帮助的事情。

1 个答案:

答案 0 :(得分:1)

我确信此代码存在许多错误,但首先要比较构造函数(添加注释)

public WeatherMonth()
{
    monthName = "January";
    daysInMonth = 31;
    // this array is being initialized OK, but it is shadowing the field of the same name, 
    // so comment it out and replace with maxTamperature = new int [daysInMonth]; 
    int [] maxTamperature = new int [daysInMonth];  
    maxTemperature[0] = -999;
}

public WeatherMonth(String monthName, int daysInMonth)
{
    this.monthName = monthName;
    this.daysInMonth = daysInMonth; 
    int days = maxTemperature.length;      
    // maxTemperature has not been initialized yet, this is meaningless, use the same logic as your other constructor above
    int [] maxTemperature = new int [days];
    maxTemperature[0] = -999;
    //int [] maxTemperature = [-999]
    //int [] maxTemperature.fill(daysInMonth, -999);
}