2015-10-16 75 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