2017-05-07 33 views
-1

我有兩個不同的問題,我需要幫助,在這段代碼中,第一個是convertToFah。我需要在構造函數中使用數組,並使用轉換爲華氏(攝氏度(雙值)* 9.0/5.0 + 32)的公式轉換每個雙精度值並返回它(我總是會得到一個像不能從double當我嘗試時加倍[])。第二個是我必須將maxMonth/minMonth設置爲與最大/最小值相同索引中的任何月份。 (所以如果最大值是10,它在數組[3],那麼這個月將是4月)。任何幫助將是美好的。 (順便說一句,我只是還沒有得到又的toString,這樣是不是真的在那一刻出現問題。)由於陣列而卡在我的代碼中

public class Temp { 
private String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 

public Temp(){ 
    double array[],avg = 0, max = 0, min = 0; 
    String maxMonth= null, minMonth = null; 
    } 
public double convertToFah(double array[]){ 
    for(int index = 0; index < array.length; index++){ 

    } 
} 
public double calAvg(double avg, double array[]){ 
    double sum = 0.0; 

    for(int index = 0; index < array.length; index++){ 
       sum = sum + array[index]; 
    } 

    return avg = sum /array.length; 

} 
public double findMin(double array[], double min, String minMonth){ 
    for (int index = 0; index < array.length; index++) { 
     if (array[index] < min){ 
      min = array[index]; 
     } 
    } 

    return min; 
} 
public double findMax(double array[], double max, String maxMonth){ 
    for (int index = 0; index < array.length; index++) { 
     if (array[index] > max){ 
      max = array[index]; 
      return max; 
     } 
    } 

    return max; 

} 

}

+2

「我有兩個不同的問題,我需要這個代碼的幫助」 - 請在每個帖子中詢問一個*問題,並且僅包含與該問題相關的代碼 - 編輯您的帖子以包含一個[mcve]一個問題。 –

+2

請問一個問題。 「我需要_X_」不是一個問題。 –

+1

您似乎對參數和返回值感到困惑。例如,在您的方法calAvg中,該方法將一個值數組作爲輸入(參數),並假定將這些值的平均值計算爲輸出(返回值)。爲什麼它將平均值作爲參數?另外,convertToFah應該如何處理它的論點,它應該返回什麼。我的建議是:在開始實施它們之前,先寫下方法的javadoc。 Desribe每個論點都是什麼,以及該方法返回的結果。 –

回答

0

不知道什麼是你的第二個問題是,但我相信你的第一一個是轉換溫度並將其存儲回陣列。試試下面的代碼:

for(int index = 0; index < array.length; index++){ 
    array[index] = array[index] * 9.0/5.0 + 32; 
} 
+0

問題是我得到一個錯誤,說''不能從double []轉換爲double''或類似的東西。 (編輯:nvm我不得不修複方法,謝謝你的幫助,雖然!) –

+0

在我身邊運作良好,哪一行顯示錯誤。 –

0

你不能只寫「雙規」在函數返回三個值(平均,最小值,最大值),你應該做的「雙重[]」,這樣你就可以返回同樣的事情你會得到。 所以這個功能將是:

public double[] convertToFah(double array[]){ 

    double[] fahValue; 

    for(int index = 0; index < array.length; index++){ 
     fahValue[index] = (array[index] * 1.8) + 32; // this is what i found on the internet, as i didn't really get what you wrote 
    } 

    return fahValue; 
} 

然後爲你的月問題。如果我是對的,你想得到幾個月的最低和最高溫度值。如果是這樣,那麼你的功能非常好,只有很少的錯誤。

這裏有修正的功能:

public double findMin(double array[]){ 

    double min = 999; // I set here the value to a high number to get a smaller one after 

    for (int index = 0; index < array.length; index++) { 

     // if the value is smaller than the previous smallest 
     if (array[index] < min){ 
      min = array[index]; 
     } 
    } 

    return min; 
} 


public double findMax(double array[]){ 

    double max = 0; 

    for (int index = 0; index < array.length; index++) { 
     if (array[index] > max){ 
      max = array[index]; 
      // return max; <- delete this line, let the return after the for function 
     } 
    } 

    return max; 
} 

如果您有更多的問題,無論是什麼,我已經寫或者別的什麼,不要猶豫,問:)

1

更改返回類型public double convertToFah(double array[])double[],因爲你要返回一個溫度轉換爲華氏溫度的雙數組。

public double[] convertToFah(double array[]) // returns double array 

現在,如果你想獲得一個月最大/最小溫度,然後修改findMin & findMax方法返回最大/最小溫度的index,那麼你可以使用index找到month

public int findMin(double array[], double min, String minMonth){ 
    int min_index = 0; 
    for (int index = 0; index < array.length; index++) { 
     if (array[index] < min){ 
      min = array[index]; 
      min_index = index; 
     } 
    } 
    return min_index; 
} 

同爲findMax ...

public int findMax(double array[], double max, String maxMonth){ 
    int max_index = 0; 
    for (int index = 0; index < array.length; index++) { 
     if (array[index] < max){ 
      max = array[index]; 
      max_index = index; 
     } 
    } 
    return max_index; 
} 

那麼你可以不喜歡minMonth = months[findMin(array, min, minMonth)];獲得最小的月份。