所以这种方法的要点是要获得高于100的温度数组。这是什么问题?当我在toString中返回这个时,它表示blazing []不存在。不正确的逻辑
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
for(int i = 0; i < temps.length; i++)
{
if(temps[i] > 100)
{
blazing[i] = temps[i];
}
}
return blazing;
}
toString方法:
public String toString()
{
String returnString = "The temperature forecast of week " + WEEK + " is logged in as: ";
for(int i = 0; i < temps.length; i++)
{
returnString += "\t" + temps[i] + "\t";
}
returnString += "\n" + "The number of temperatures below freezing is " + getUnderFreeze() + "." + "\n" +
"The largest difference this week was a net change of " + NetChange() + ".";
for(int i = 0; i < blazing.length; i++)
{
returnString += "The temperature above 100 degrees is " + above100Degrees() + "." + "\n" + "\t" + blazing[i] + "\t";
}
return returnString;
}
输出
Forecast.java:122: error: cannot find symbol
for(int i = 0; i < blazing.length; i++)
^
symbol: variable blazing
location: class Forecast
Forecast.java:124: error: cannot find symbol
returnString += "The temperature above 100 degrees is " + above100Degrees() + "." + "\n" + "\t" + blazing[i] + "\t";
^
symbol: variable blazing
location: class Forecast
2 errors
数组'炽热'未在'toString'方法中声明,并且从'above100Degrees'方法返回的数组也不会保存在任何地方。 – AntonH
好吧,那么如何使用“炽热”的元素,我想要显示它们 – PatGreens
看起来像你的变量'blazing'不适用于你的toString()方法。从上面发布的代码中,'blazing'似乎只属于'above100degrees方法'。你可以让'blazing'变成一个类级别的变量,以便在别处可以看到 – Durandal