【发布时间】:2014-01-24 19:38:30
【问题描述】:
所以这个方法的重点是得到一个高于 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' 方法中没有声明数组 'blazing',并且从 'above100Degrees' 方法返回的数组也没有保存在任何地方。
-
好的,我要如何使用“炽热”的元素,我想展示它们
-
看起来您的变量
blazing不适用于您的 toString() 方法。从您上面发布的代码来看,blazing似乎只属于above100degrees method。您可以将blazing设为类级别变量,以便在其他地方看到它 -
我假设您对编程(或至少是 Java)很陌生。我认为您会从使用诸如 jetbrains.com/idea 之类的 IDE 中受益。它将为您突出显示这些类型的问题以及许多其他好处。
-
您不能使用
+将数组放入字符串中。请看Arrays.toString。