2015-10-16 76 views
0
import java.util.Scanner; 
import java.util.Arrays; 
public class P4 { 
    public static void main(String [] args) { 

    Scanner input = new Scanner(System.in); 

    final int NUMMONTHS = 12; 
    final int SORT = 12; 
    int [] plantHeight = new int[SORT]; 
    int [] avgTemp = {46, 48, 49, 50, 51, 53, 54, 55, 56, 55, 51, 47}; 
    int [] avgRain = {5, 3, 3, 1, 1, 0, 0, 0, 0, 1, 3, 4}; 
    int[] newGrowth; 
    newGrowth = new int[NUMMONTHS]; 
    int minTemp; 
    int maxTemp; 
    int minRain; 
    int j; 

    //prompt user input 
    System.out.println("Welcome to the 210 gardening planner!"); 

    System.out.println("Enter minimum temperature for plant:"); 
     minTemp = input.nextInt(); 
    System.out.println("Enter maximum temperature for plant:"); 
     maxTemp = input.nextInt(); 
    System.out.println("Enter minimum rainfall for plant:"); 
     minRain = input.nextInt(); 
    System.out.println("Month" + "\t" + "Temp Rain Growth" + "\t" + "Plant Height"); 

    //Calculate and print plant growth 
    for(j=0; j<NUMMONTHS; j++){ 
     if(avgTemp[j] >= minTemp & maxTemp >= avgTemp[j]){ 
      newGrowth[j] = avgRain[j] - minRain; 
      if(j == 0){ 
      plantHeight[j] = newGrowth[j]; 
      }else{ 
      plantHeight[j] = newGrowth[j] + plantHeight[j-1];} 
     } 
     //if growth is zero, height remains same as previous 
     else{ 
      newGrowth[j] = -1; 
      plantHeight[j] = plantHeight[j] - 1; 
     } 
     //plant height cannot be less than 0 
     if(plantHeight[j]< 0){ 
      plantHeight[j] = 0; 

     } 
     plantHeight[j] = scan.nextInt(); 
    System.out.print(j + "\t" + avgTemp[j] + " " + avgRain[j] + "  "); 

    System.out.println(newGrowth[j] + "   " + plantHeight[j]); 

    } 
    for (int i=0; i<plantHeight; i++) { 
      // find smallest element in elements i to plantHeight 
      int currentMin = plantHeight[i]; 
      int minIndex = i; 

      for (int x=i+1; x < plantHeight; x++) { 
       if (plantHeight[x] < currentMin) { 
        currentMin = plantHeight[x]; 
        minIndex = x; 
       } 
      } 
     if (minIndex != i) { 
        plantHeight[minIndex] = plantHeight[i]; 
        plantHeight[i] = currentMin; 
      } 
     } 
    System.out.println(plantHeight[0]); 




    } 
} 

我根據雨和溫度製作了植物生長圖,但我需要一些幫助,在每月之後存儲植物的高度並打印最高的高度。爲此,我試圖將每個高度存儲到一個數組中,然後按降序排列數組以找到最高值並將其打印出來。如何查找商店並查找數組中的最高值?

回答

0

您可以使用Arrays.sort() inbuild函數進行排序並檢索最後一個數字。

下面是一個簡單的例子來解釋sort。然後,您可以在您的代碼

int[] num = {4,3,6,2,9,3}; 

Arrays.sort(num); 

for (int i: num) { 
    System.out.println(i); 
} 

int max = num[num.length - 1]; 

System.out.println(max); // max is 9 

Demo of Arrays.sort()

實現,因爲你想最大高度和它對應的月份,這將是一個更好的主意,用TreeMap,並有height爲重點和month的價值。由於它實現SortedMap,因此按默認按升序插入密鑰,然後可以檢索最後一個將是最大高度和其對應月份的條目。

TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); 
Random rand = new Random(); 

for (int i = 1; i < 13; i++) { 
    map.put(rand.nextInt(100), i); 
} 

/*for (Map.Entry<Integer, Integer> a: map.entrySet()) { 
    System.out.println(a.getKey() + " " + a.getValue()); 
}*/ 

Map.Entry<Integer, Integer> maxValues = map.lastEntry(); 
int maxHeight = maxValues.getKey(); 
int maxMonth = maxValues.getValue(); 
System.out.println(maxHeight + " " + maxMonth); 

Demo of TreeMap

+0

謝謝!我能夠在我的植物高度中找到最高的整數,但是還有一種方法可以打印出最高植物高度的月份嗎? –

+0

@TommyTran爲什麼不使用'Map'而不是兩個數組?您可以在'TreeMap'上使用'sort'並將其存儲在'key value'對中 – sam

+0

我不確定你的意思。你能給我看一個樣品嗎? –

0

相反的排序,搜索maxIndex:

int maxIndex = 0; 
    int currentMax = plantHeight[0]; 
    for (int i=1; i<plantHeight; i++) { 
     if (plantHeight[i] > currentMax) { 
      maxIndex = i; 
      currentMax = plantHeight[i]; 
     } 
    } 
    System.out.println(plantHeight[maxIndex]); 
    System.out.println(avgTemp[maxIndex]); 
    System.out.println(avtRain[maxIndex]); 

要找到兩者的最小值和最大值:

int minIndex = 0; 
    int maxIndex = 0; 
    int currentMin = plantHeight[0]; 
    int currentMax = currentMin; 
    for (int i=1; i<plantHeight; i++) { 
     if (plantHeight[i] < currentMin) { 
      minIndex = i; 
      currentMin = plantHeight[i]; 
     } 
     else if (plantHeight[i] > currentMax) { 
      maxIndex = i; 
      currentMax = plantHeight[i]; 
     } 
    } 
    System.out.println(plantHeight[minIndex]); 
    System.out.println(avgTemp[minIndex]); 
    System.out.println(avtRain[minIndex]); 
    System.out.println(plantHeight[maxIndex]); 
    System.out.println(avgTemp[maxIndex]); 
    System.out.println(avtRain[maxIndex]); 
+0

這一個仍然需要遍歷。如果添加到'TreeMap',插入是[O(log(n))](http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html),我們知道最大元素將是最後一個元素 – sam

+0

@ sam2090 - true,但爲了創建地圖將n個元素添加到TreeMap需要O(n log(n))。創建數組需要O(n),並且搜索min + max需要O(n),所以總體時間仍然是O(n)(只是常數時間n的差異)。 – rcgldr

+0

是的。我贊同你 :) – sam