2016-11-24 72 views
1

我不知道如何顯示字符串,並在同一時間int數組,使其與特定月份顯示字符串和int數組一次

Global: 
static double[] set2014 = new double[6]; 
static String[] months = new String[6]; 

這裏是顯示數組的最大值用於計算最大值的方法:

public static void max(){ 
    initialise(); 
    double max = set2014[0]; 

    for(int i = 1; i < set2014.length; i++){ 
     if(set2014[i] > max){ 
      max = set2014[i]; 
     } 
    } 
    System.out.println("------------------"); 
    System.out.println("Largest figure is " + max); 
} 

例如,輸出將是: 最大數字是:204566

+0

月份在哪裏?它是'set2014'數組的索引嗎? – weston

+0

我不知道我明白你在問什麼。例如,你提到,「與特定月份」 - 但是*什麼*月?在你的代碼中沒有一個月,問題的其餘部分對我來說有點模糊。請考慮添加更多相關的細節和代碼,以幫助我們瞭解您的問題,代碼和問題。 –

+0

請理解,我們關於您的程序正在做什麼,您想要達成什麼或您的問題是什麼的唯一窗口就是您告訴我們並向我們展示的內容。 –

回答

3

維護 三月兩個通過索引鏈接的數組就是我所說的Object Denial。考慮創建一個包含月份和值的類。

public interface MonthValue { //class or interface, I just didn't want to type out the simple implementation 
    String getMonth(); 
    double getDouble(); 
} 

//set2014 now needs to contain MonthValues 
MonthValue max = set2014[0]; 

for(int i = 1; i < set2014.length; i++){ 
    MonthValue current = set2014[i]; 
    if(current.getValue() > max.getValue()){ 
     max = current; 
    } 
} 

System.out.println("Largest figure is " + max.getValue()); 
System.out.println("In month " + max.getMonth()); 

但是,爲了回答你的問題的是:

你可以保持,而不是指數的軌跡:

int maxMonthIndex = 0; 

for(int i = 1; i < set2014.length; i++){ 
    if(set2014[i] > set2014[maxMonthIndex]){ 
     maxMonthIndex = i; 
    } 
} 

System.out.println("Largest figure is " + set2014[maxMonthIndex]); 
System.out.println("In month " + months[maxMonthIndex]); 

順便說一句,set是一個數組的名聲。集合是無序集合,數組有順序。

相關問題