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 :(得分: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
由于您需要最大高度及其相应的月份,因此最好使用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);
答案 1 :(得分: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]);