java Rainfall 类,从数组中查找最大值和最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26416955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rainfall class, finding the max and minimum from an array
提问by Kenny Bowen
I am making a program for a java class of mine that asks this:
我正在为我的一个 java 类制作一个程序,它会问这个:
Write A Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:
编写一个 Rainfall 类,将 12 个月中每个月的总降雨量存储到一个双精度数组中。该程序应该具有返回以下内容的方法:
- The total rainfall for the year
- The average monthly rainfall
- The month with most rain
- The month with least rain
- 全年总降雨量
- 月平均降雨量
- 下雨最多的月份
- 下雨最少的月份
Demonstrate the class in a complete program. (Do not accept negative numbers for monthly rainfall figures)
在完整的程序中演示该类。(每月降雨量数字不接受负数)
import java.util.Scanner;
import java.io.*;
public class apples{
public static void main (String[] args){
Scanner kenny = new Scanner(System.in);
double rain[]=new double[13];
double sum = 0;
double avg =0;
double most =0;
double least =0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
System.out.print("January: ");
rain [0] = kenny.nextDouble();
System.out.print("February: ");
rain [1] = kenny.nextDouble();
System.out.print("March: ");
rain [2] = kenny.nextDouble();
System.out.print("April: ");
rain [4] = kenny.nextDouble();
System.out.print("May: ");
rain [5] = kenny.nextDouble();
System.out.print("June: ");
rain [6] = kenny.nextDouble();
System.out.print("July: ");
rain [7] = kenny.nextDouble();
System.out.print("August: ");
rain [8] = kenny.nextDouble();
System.out.print("September: ");
rain [9] = kenny.nextDouble();
System.out.print("October: ");
rain [10] = kenny.nextDouble();
System.out.print("November: ");
rain [11] = kenny.nextDouble();
System.out.print("December: ");
rain [12] = kenny.nextDouble();
//(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12] ;
avg = (rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12]) / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
System.out.print("The month with the most rain was: ");
}
private static void getMaxValue(double[] rain) {
getMaxValue(rain);
System.out.println(getMaxValue(rain));
System.out.println("The month with the least rain was: ");
}
private static void getMinValue(double[] rain) {
getMinValue(rain);
System.out.println(getMaxValue(rain));
}}
I've got most of it ready to go. I just am wondering how to get the "Max" and "Min" from the numbers that are entered.
我已经准备好了大部分。我只是想知道如何从输入的数字中获取“最大值”和“最小值”。
Any help would be great!!
任何帮助都会很棒!!
回答by Madhawa Priyashantha
you can find max or min by looping array .and change return type void to double so method will return max rain ;
您可以通过循环数组找到最大值或最小值。并将返回类型 void 更改为 double 以便方法将返回最大值雨;
private static double getMaxValue(double[] rain) {
double max=0;
for(double i : rain){
if(i>max){
max=i;
}
}
return max;
}
and use this as;
并将其用作;
System.out.println(getMaxValue(rain));
and same for min;
和 min 相同;
private static double getMinValue(double[] rain) {
double min=Double.MAX_VALUE;
for(double i : rain){
if(i<min){
min=i;
}
}
return min;
}
but in your code there is lot of mistakes
但是在你的代码中有很多错误
1)
1)
double rain[]=new double[13];
this should be
这应该是
double rain[]=new double[12];
because this is array length .so you have 12 months .
因为这是数组长度。所以你有 12 个月。
2) you have missed
2)你错过了
rain [3]
3) you assign to 13 index by it should be 12 .
3)你分配给 13 索引它应该是 12 。
rain [13] = kenny.nextDouble(); --> rain [12] = kenny.nextDouble();
so this is the complete example .
所以这是完整的例子。
public class apples {
public static void main(String[] args) {
Scanner kenny = new Scanner(System.in);
double rain[] = new double[12];
double sum = 0;
double avg = 0;
double most = 0;
double least = 0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
System.out.print("January: ");
rain[0] = kenny.nextDouble();
System.out.print("February: ");
rain[1] = kenny.nextDouble();
System.out.print("March: ");
rain[2] = kenny.nextDouble();
System.out.print("April: ");
rain[3] = kenny.nextDouble();
System.out.print("May: ");
rain[4] = kenny.nextDouble();
System.out.print("June: ");
rain[5] = kenny.nextDouble();
System.out.print("July: ");
rain[6] = kenny.nextDouble();
System.out.print("August: ");
rain[7] = kenny.nextDouble();
System.out.print("September: ");
rain[8] = kenny.nextDouble();
System.out.print("October: ");
rain[9] = kenny.nextDouble();
System.out.print("November: ");
rain[10] = kenny.nextDouble();
System.out.print("December: ");
rain[11] = kenny.nextDouble();
//(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11];
avg = sum / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
most =getMaxValue(rain);
least=getMinValue(rain);
System.out.println("The max rain is: " + most);
System.out.println("The min rain is: " + least);
}
private static double getMaxValue(double[] rain) {
double max = 0;
for (double i : rain) {
if (i > max) {
max = i;
}
}
return max;
}
private static double getMinValue(double[] rain) {
double min = Double.MAX_VALUE;
for (double i : rain) {
System.out.println(i);
if (i < min) {
min = i;
}
}
System.out.println(min);
return min;
}
}
but you can use a array witch contain all the months.the advantage of this is you loop dynamically rather than hard cording .and you can warn when input negative easily .the good approach is follow .
但是你可以使用一个包含所有月份的数组。这样做的好处是你可以动态循环而不是硬编码。并且你可以很容易地在输入负数时发出警告。好的方法是遵循。
public class apples {
public static void main(String[] args) {
Scanner kenny = new Scanner(System.in);
double rain[] = new double[12];
double sum = 0;
double avg = 0;
double most = 0;
double least = 0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
String months[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i=0;i<months.length;i++) {
System.out.println(months[i]+" :");
double val = kenny.nextDouble();
while(val<0){
System.out.println("negatives not allowed ! enter again");
val = kenny.nextDouble();
}
rain[i]=val;
sum+=val;
}
avg = sum / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
most =getMaxValue(rain);
least=getMinValue(rain);
System.out.println("The max rain is: " + most);
System.out.println("The min rain is: " + least);
}
private static double getMaxValue(double[] rain) {
double max = 0;
for (double i : rain) {
if (i > max) {
max = i;
}
}
return max;
}
private static double getMinValue(double[] rain) {
double min = Double.MAX_VALUE;
for (double i : rain) {
System.out.println(i);
if (i < min) {
min = i;
}
}
System.out.println(min);
return min;
}
}
回答by sprinter
Java 8 has a much easier mechanism for manipulating data such as this without any need for looping or temporary variables.
Java 8 有一个更简单的机制来操作这样的数据,而无需循环或临时变量。
If you have Java 8 you can use the following:
如果您有 Java 8,则可以使用以下内容:
double rain[] = {3, 2, 7, 9, 10};
double totalRainfall = Arrays.stream(rain).sum;
double maxRainfall = Arrays.stream(rain).max().getAsDouble();
double minRainfall = Arrays.stream(rain).min().getAsDouble();
double avgRainfall = Arrays.stream(rain).average().getAsDouble();
That's much easier to read and understand than the traditional method.
这比传统方法更容易阅读和理解。