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]);
}
}
我根據雨和溫度製作了植物生長圖,但我需要一些幫助,在每月之後存儲植物的高度並打印最高的高度。爲此,我試圖將每個高度存儲到一個數組中,然後按降序排列數組以找到最高值並將其打印出來。如何查找商店並查找數組中的最高值?
謝謝!我能夠在我的植物高度中找到最高的整數,但是還有一種方法可以打印出最高植物高度的月份嗎? –
@TommyTran爲什麼不使用'Map'而不是兩個數組?您可以在'TreeMap'上使用'sort'並將其存儲在'key value'對中 – sam
我不確定你的意思。你能給我看一個樣品嗎? –