2012-05-10 33 views
1

Goodday大家,类型表达的必须是一个数组类型,但它解析为“温度” -Java

所以,我提出的一类的温度,这有一个构造,使温度。温度是一个由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; 
     } 

现在我想进入另一个类并使用该对象温度来做一些计算。这是它的代码。

//Variabels 

public int volatility; 
private static Temperature temperature; 
private static int intrensicExplosivity; 

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

所以现在我得到的错误:类型的表达式必须是一个数组类型,但它决心“温度”

任何解决方案?

我对Java很新,所以可能只是一些synthax错误,但我找不到它。

在此先感谢。 大卫

+0

对象如果有错误,请张贴异常或错误,请的堆栈跟踪。 – Crazenezz

回答

1

而不是

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

尝试

public static int standardVolatility() { 
    if(tepmerature.temperature[0] == 0) { 
     ^^^^^^^^^^^^ 

注意,在你的第二个片段的temperatureTemperature其本身具有一个int数组称为temperature类型。要访问temperature-Temperature对象的数组,您必须执行temperature.temperature


由于@Marko Topolnik所指出的,你也可能要改变

public int[] temperature; 

public int[] temperature = new int[2]; 

,以腾出空间给这两个温度值。

+0

你也可以建议他需要初始化数组(这是他的下一个错误),或者根本不使用数组:) –

+0

呵呵..好点:-) – aioobe

0

tempetureTempeture这不是一个数组。 你想要的是你的对象实例中的阵列成员temperature(你也叫做tempature)。

无论如何改变行:

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

有了:

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

我劝你使用getter和setter方法,还可以使用该名称不会迷惑你。

0

这里混合了一些变量。

在您的代码块中,temperature指的是您的Temperature类的一个实例,但您认为它指的是温度数组,它是Temperature类的成员。

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

首先创建吸气& setter方法进入温度等级,然后调用temperature.getTempertature()和使用它的第二类。

+0

我改变了你们建议的东西,谢谢你的一切现在作品非常棒!我会给更多的绿色V,但只能有一个:P –

0

那么,你的问题是在这里

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

您正在使用的对象数组。这是错误的。 取而代之,使用GET和set方法从中设置并获取温度。 不要公开你所有的数据,这对于OO编程来说是非常糟糕的。使用这些获得者和设置者。 财产以后这样的:if(temperature.getTemperature()==0) etc.

PS:不要忘记并初始化与新的运营商(Temperature temperature = new Temperature(10,30);

相关问题