double rainfall[] =new double[6];
String months[]={"January", "February", "March", "April", "May", "June"};
double total =0;
for(int i=0;i<rainfall.length;i++){
rainfall[i] = Double.parseDouble(JOptionPane.showInputDialog("Please enter rainfall for month " + (i +1)));
total += rainfall[i];
}
System.out.println("The 6 mmonthly rainfalls are: ");
for(int i=0;i<rainfall.length;i++){
System.out.println(months[i]+ " : "+ "\t" + rainfall[i]);
}
double average = total/(double)rainfall.length;
System.out.println("Average rainfall: "+average);
double min = rainfall[0];
for(int i=1;i>rainfall.length;i++){
if(rainfall[i]>min)
min =rainfall[i];
}
System.out.println("had the lowest rainfall: "+min);
这是我的代码,示例输出:
6个月的每月降雨量是:
一月:1.0
2月:2.0
3月:3.0
四月:4.0
五月:5.0
六月:6.0
平均降雨量:3.5
降雨量最低的国家:1.0
正如您在示例输出的“ 9”行中看到的那样,在不得不输入降雨量最低的月份之前,我已经“降雨量最低”。我不知道如何连接它们。抱歉,如果这有点混乱,英语不是我的主要语言。预先感谢您的帮助。
最佳答案
您可以通过获取最小降雨量的索引来找到最小降雨月,并且随着降雨量被顺序映射到月份,您可以得到如下所示的最低降雨月...
您的密码
double min = rainfall[0];
for(int i=1;i>rainfall.length;i++){
if(rainfall[i]>min)
min =rainfall[i];
}
更改为
int index = 0;
int min = rainfall[index];
for (int i=1; i<rainfall.length; i++){
if (rainfall[i] < min ){
min = rainfall[i];
index = i;
}
}
System.out.println("Lowest rainfall month is :" month[index]);
System.out.println("had the lowest rainfall: "+min);
关于java - 显示哪个月的降雨量最低-数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39900310/