gpt4 book ai didi

Java 最小最大值

转载 作者:行者123 更新时间:2023-12-01 23:06:48 25 4
gpt4 key购买 nike

一切正常,我只需要输出表示降雨量最高/最低的季度,而不是实际值。我不确定如何将季度和值联系在一起,以便输出为季度 1、2、3 或 4。

import java.util.Scanner;

public class rainfall
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double[] rainfall = new double[4];
double totalRainfall = 0.0;
double max = 0, min = 0;


for (int i=0; i < 4; i++)
{
System.out.print("Enter rainfall for quarter " + (i+1) + ": ");
rainfall[i] = scan.nextDouble();
totalRainfall += rainfall[i];
if (i == 0)
{
max = min = rainfall[i];
}

{
if (rainfall[i] > max)
max = rainfall[i];
else if (rainfall[i] < min)
min = (i + 1);
//min = rainfall[i];
}
}

System.out.println("Total rainfall = "+totalRainfall);
System.out.println("Average rainfall = "+(totalRainfall / 4.0));
System.out.println("Max quarter rainfall = "+ max);
System.out.println("Min quarter rainfall = " + min);

//System.out.println("Max quarter rainfall = "+ maxQuarter);
//System.out.println("Min quarter rainfall = " + minQuarter);

}//end main
}//end class

最佳答案

仅存储索引而不是值:

if (rainfall[i] > rainfall[max])
max = i;
else if (rainfall[i] < rainfall[min])
min = i;

System.out.println("Max quarter rainfall = " + max + 1);
System.out.println("Min quarter rainfall = " + min + 1);

关于Java 最小最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22611283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com