2012-05-10 4 views
1

굿데이의 모든 사람들이, 유형은 배열 형식이어야하지만 "온도"-JAVA이

그래서 나는 온도를 만들 수있는 생성자가 클래스 온도를 제작하기로 결심. 온도는 2 개의 숫자 [냉기, 매운]의 arraylist입니다.

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 에러 일 겁니다.하지만 찾을 수는 없습니다.

미리 감사드립니다. 대신

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

의 데이비드

+0

와 객체를 초기화하기 잊지 마세요 예외 또는 오류하시기 바랍니다의 스택 추적을 게시하시기 바랍니다. – Crazenezz

답변

1

는 두 번째 조각의 temperature 유형 자체가 temperature라는 INT 배열을 가지고 Temperature의 것을

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

참고하십시오. Temperature 개체의 temperature 배열에 액세스하려면 temperature.temperature을 수행해야합니다. @Marko Topolnik가 지적 하듯이


은 또한 변경할 수있는 2 개 개의 온도 값에 대한 공간을 확보하기 위해

public int[] temperature; 

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

.

+0

당신은 그에게 배열을 초기화 할 필요가 있다고 조언 할 수도 있습니다. (그 다음 오류입니다.) –

+0

Hehe .. 좋은 지적 :-) – aioobe

0

tempeture은 배열이 아닌 Tempeture입니다. 원하는 것은 객체 인스턴스 (tempature이라고도 함) 안에 배열 구성원 temperature입니다. 내가 getter 및 setter를 사용하고도 늘 당신을 혼란스럽게 이름을 사용하기를 권합니다

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

:

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

으로 :

어쨌든 라인을 변경합니다.

0

여기에 일부 변수가 섞여 있습니다.

아래의 코드 블록에서 temperatureTemperature 클래스의 인스턴스를 나타내지 만 Temperature 클래스의 멤버 인 온도 배열을 가리키는 것으로 가정했습니다.

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

먼저 흡착제를 온도 & 클래스로 설정 메소드를 만들고 호출 temperature.getTempertature() 두 번째 클래스의 사용.

+0

여러분이 제안한 것들을 바 꾸었습니다. 지금 훌륭한 작품! 나는 더 많은 초록색 V를 줄 것이지만 오직 한가지 만 할 수있다 : p –

0

음, 문제는 당신은 배열로 객체를 사용하는

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

여기에있다. 그건 틀렸어요. 대신 GET을 사용하고 온도를 설정하고 얻는 방법을 설정하십시오. 모든 데이터를 공개하지 마십시오. OO 프로그래밍에 매우 좋지 않습니다. 그 getter 및 setter를 사용하십시오. if(temperature.getTemperature()==0) etc.

PS : 같은 뭔가를해야만 오류가있는 경우 새 연산자 (Temperature temperature = new Temperature(10,30);)