表达式的类型必须是数组类型,但解析为;温度“-JAVA

表达式的类型必须是数组类型,但解析为;温度“-JAVA,java,Java,大家好 所以我创建了一个类Temperature,它有一个构造函数来生成温度。 温度是一个由2个数字组成的数组列表[冷,热] public int hotness; public int coldness; public int[] temperature; public int maxTemperature = 10000; //Constructor Temperature public Temperature(int hotness, int coldness) { /

大家好

所以我创建了一个类Temperature,它有一个构造函数来生成温度。 温度是一个由2个数字组成的数组列表[冷,热]

    public int hotness;
public int coldness;
public int[] temperature;
public int maxTemperature = 10000;


//Constructor Temperature
public Temperature(int hotness, int coldness) {
    /**
     * A constructor for the Array Temperature
     */
    maxTemperature = getMaxTemperature();
        if(hotness <= maxTemperature && coldness <= maxTemperature)
        temperature[0] = coldness;
        temperature[1] = hotness;
        }
现在我得到了一个错误:表达式的类型必须是数组类型,但它被解析为“Temperature”

有什么解决办法吗

我对Java非常陌生,所以可能只是一些synthax错误,但我就是找不到它

提前谢谢。 大卫

而不是

public static int standardVolatility() {
    if(temperature[0] == 0) {
试一试

请注意,第二个代码段中的
temperature
类型为
temperature
,它本身有一个名为
temperature
的int数组。要访问
temperature
对象的
temperature
-数组,必须执行
temperature.temperature


正如@Marko Topolnik所指出的,您可能还需要进行更改

public int[] temperature;


为了给两个温度值留出空间。

tempeture
属于
tempeture
类型,不是数组。 您需要的是对象实例中的数组成员
temperature
(也称为
temperature

换行:

if(temperature[0] == 0) 
.
.
与:

if(temperature.tempature[0] == 0)
.
.

我建议您使用getter和setter,并使用不会让您感到困惑的名称。

您在这里混淆了一些变量

在下面的代码块中,
temperature
指的是
temperature
类的一个实例,但您假定它指的是属于
temperature
类的温度数组

public static int standardVolatility() {
    if(temperature.temperature[0] == 0){
        int standardVolatility = 100 * intrensicExplosivity * (0.10 * temperature[1]);
    }

首先在Temperature类中创建getter和setter方法,然后调用Temperature.gettemperature()并在第二个类中使用它。

好吧,问题就在这里

private static Temperature temperature;
if(temperature[0] == 0){
        int standardVolatility = 100 * intrensicExplosivity * (0.10 * temperature[1]);
}
您正在使用对象作为数组。那是错误的。 相反,使用一个GET-and-set方法来设置和获取温度。 不要将所有数据公开,这对OO编程非常不利。使用那些getter和setter。 例如:
if(temperature.getTemperature()==0)等


PS:不要忘记使用新操作符初始化对象(
Temperature-Temperature=new-Temperature(10,30);

如果出现错误,您还可以建议他需要初始化数组(这是他的下一个错误),或者根本不使用数组:),请发布异常或错误的堆栈跟踪。我更改了你们建议的内容,谢谢你们现在一切都很好!!我会给更多的绿色V,但只能给一个:p
if(temperature.tempature[0] == 0)
.
.
public static int standardVolatility() {
    if(temperature.temperature[0] == 0){
        int standardVolatility = 100 * intrensicExplosivity * (0.10 * temperature[1]);
    }
private static Temperature temperature;
if(temperature[0] == 0){
        int standardVolatility = 100 * intrensicExplosivity * (0.10 * temperature[1]);
}