我需要制作一个程序,根据用户生成的月降雨量数字并对其进行平均,找出最大值和最小值,找到标准偏差等。我似乎已经将大部分内容都放下了但我的“找到了高于和低于平均水平的数字”似乎不起作用。此外,我无法弄清楚如何让用户选择后再做出选择,然后给予所需的信息。任何帮助将不胜感激。 example output
import java.util.Arrays;
import java.util.Scanner;
public class RainfallAnalyzer {
public static void main(String[] args) {
double theRainfall = 0;
double total = 0;
Scanner scan = new Scanner(System.in);
double[] rainfall = new double[12];
System.out.println("Enter the rainfall measurements for this year: ");
for (int i = 0; i < rainfall.length; i++) {
theRainfall = scan.nextDouble();
rainfall[i] = theRainfall;
total += rainfall[i];
}
int choiceentry = -1;
loop:
while (choiceentry !8) {
System.out.println("1. print all rainfall measurements");
System.out.println("2. print the avererage rainfall");
System.out.println("3. print the maximum and minimum rainfall measurements");
System.out.println("4. print the number of months above average rainfall");
System.out.println("5. print the number of months below average rainfall");
System.out.println("6. print the median rainfall measurement");
System.out.println("7. print the standard deviation");
System.out.println("8. exit");
if (scan.hasNextInt()) {
choiceentry = scan.nextInt();
}
switch (choiceentry) {
case 1:
for (int i = 0; i < rainfall.length; i++) {
System.out.println("Month # " + (i + 1) + ": " + rainfall[i]);
}
break loop;
case 2:
total = total / 12;
System.out.println("This year's average rainfall: " + total);
break loop;
case 3:
Arrays.sort(rainfall);
System.out.println("Min value " + rainfall[0]);
System.out.println("Max value " + rainfall[rainfall.length - 1]);
break loop;
case 4:
int numRainfallAbove = 0;
for (int i = 0; i < rainfall.length; i++) {
if (rainfall[i] >= avg) {
numRainfallAbove++;
} else {
}
}
System.out.println("Number of months above the average rainfall: " + numRainfallAbove);
break loop;
case 5:
int numRainfallBelow = 0;
for (int i = 0; i < rainfall.length; i++) {
if (rainfall[i] <= total) {
numRainfallBelow++;
} else {
}
}
System.out.println("Number of months below the average rainfall: " + numRainfallBelow);
break loop;
case 6:
double median;
if (rainfall.length % 2 == 0) {
median = ((double) rainfall[rainfall.length / 2] + (double) rainfall[rainfall.length / 2 - 1]) / 2;
} else {
median = (double) rainfall[rainfall.length / 2];
}
System.out.println("This year's median rainfall: " + median);
break loop;
case 7:
double theDeviation = deviation(rainfall, total);
System.out.println("The standard deviation is " + theDeviation);
break loop;
}
}
}
public static double deviation(double[] rainfall, double total) {
double endResult = 0;
double temporary = 0;
double arraySum = 0;
double average = total;
double sum = 0;
for (int i = 0; i < rainfall.length; i++) {
sum += Math.pow((rainfall[i] - average), 2);
}
temporary = sum / (rainfall.length - 1);
endResult = Math.sqrt(temporary);
return endResult;
}
}
答案 0 :(得分:0)
更改案例4中的代码:
null
对此:
int numRainfallAbove = 0;
for(int i = 0; i < rainfall.length; i++){
if(rainfall[i] >= total){
numRainfallAbove++;
} else{}
}
System.out.println("Number of months above the average rainfall: "+numRainfallAbove);
break loop;
问题在于您将月降雨量与总数进行比较而不是平均值。您需要为您的&#34;月份降雨量低于平均水平的月份做类似的事情。
只要用户不退出,要让它继续要求更多选择,请将while循环更改为:
int numRainfallAbove = 0;
double avg = total / 12;
for (int i = 0; i < rainfall.length; i++) {
if (rainfall[i] >= avg) {
numRainfallAbove++;
}
}
System.out.println("Number of months above the average rainfall: " + numRainfallAbove);
break loop;
除了与您的问题没有直接关系的旁边,请尝试在发布时正确缩进代码,并且不要使用goto语句或指向标签的中断。
答案 1 :(得分:0)
此外,我无法弄清楚如何让用户选择后再进行其他选择,然后获得所需的信息。
你编写代码的方式是(实际上)
loop: while not a valid choice:
get a choice
if it is valid choice":
act on it
break out of loop
(注意“腰带和括号”的方式,你终止循环是......不必要的。最终将终止条件置于while
条件仅。)< / p>
所以你真正需要的是一种方式让用户说“我想立即停止”。所以你可以做出另一个选择,或者你可以说需要无效的选择。
然后你需要改变循环来说
while a valid choice:
或
while not the stop choice (or condition):
有一点,您是否考虑过如果用户输入“STOP”或其他不是数字的内容会发生什么?试试吧。然后尝试解释实际发生的事情。