对不起伙计们,我会重申这个问题。我有一个包含12个元素的数组:
1.6, 2.1, 1.7, 3.9, 3.7, 3.9, 2.9, 4.3, 2.4, 3.7
我想获得此数组中的最大值。问题是如何从我的主类调用方法到我在驱动程序类中创建的对象?
//MAIN CLASS
import java.util.Scanner;
public class Rainfall
{
private double total = 0;
private double average;
private double max;
private double smallest;
private double rain[];
Scanner scan = new Scanner(System.in);
public Rainfall(double...rainfall)
{
double[] rain = {1.6 , 2.1, 1.7, 3.9, 3.7, 3.9, 2.9, 4.3, 2.4, 3.7};
}
public double getTotal()
{
total = 0;
for(int i = 0; i < 12; i++)
{
total = total + rain[i];
}
System.out.println("The total rainfall for the year is: " + total);
return total;
}
public double getAverage()
{
average = total/12;
System.out.println("The average monthly rainfall is: " + average);
return average;
}
public double getMostRain()
{
double max = 0;
int maxind = 0;
for(int i = 0; i < 12; i++)
{
if (rain[i] > max)
{
max = rain[i];
maxind = i;
}
}
System.out.println("The largest amout of rainfall was: " + max +
"inches in month" + (maxind + 1));
return max;
}
public double getLeastRain()
{
double smallest = Double.MAX_VALUE;
int smallind = 0;
for(int n = 0; n < 12; n++)
{
if (rain[n] < smallest)
{
smallest = rain[n];
smallind = n;
}
}
System.out.println("The smallest amout of rainfall was" + smallest +
"inches in month " + (smallind + 1));
return smallest;
}
}
//DriverClass
public class RainfallDriver {
public static void main(String[] args) {
double[] list = {1.6 , 2.1, 1.7, 3.9, 3.7, 3.9, 2.9, 4.3, 2.4, 3.7};
//Counts the total and average from the elements in the array
double total = 0;
double average =0;
for (double element : list)
total += element;
average = total/12;
System.out.println("Total: " + total);
System.out.println("Smallest: " +rain.getLeastRain()); //How can I call the method?
System.out.println("Largest: " + rain.getMostRain()); //How can I call the method?
System.out.println("Average: " + average);
}
}
答案 0 :(得分:1)
如果你可以使用Java 8,那么新的Lambda Expressions可以用一个很好的单行解决问题:
double max = Arrays.stream(rain).max().getAsDouble();
您需要调用尾随getAsDouble()
方法的原因是因为max()
方法返回OptionalDouble,因为Java需要一种方法来处理rain
的可能性}数组可能为空。
同样,您的其他方法可以实现如下:
double min = Arrays.stream(rain).min().getAsDouble();
double average = Arrays.stream(rain).average().getAsDouble();
或在一起:
DoubleStream rainStream = Arrays.stream(rain);
double max = rainStream.max().getAsDouble();
double min = rainStream.min().getAsDouble();
double average = rainSteam.average().getAsDouble();
答案 1 :(得分:0)
我不确定你问的是什么......但是这样的事情会找到rain
数组的最大元素。
int largestNum = 0;
for ( int i = 0; i < 12; i++ )
{
if ( rain [ i ] >= largestNum )
{
largestNum = rain[ i ];
}
}
答案 2 :(得分:0)
我看到它的方式,你不太了解OOP是什么,以及如何使用它:
您正在编写一个名为Rainfall
的类,并且您(正确地)声明其成员(属性)......但您没有初始化它们!您的构造函数声明一个 local 变量,其名称与您的某个类的属性相同,但不会在任何地方使用它。
如果我所看到的是正确的,那么你想要的是这样的:
public class Rainfall {
private double[] rain;
/*
It is a good idea to make the attributes private, and then access them
through methods (getters to get the values, setters to change the values)
*/
// more attributes here
public Rainfall(double[] rain) {
this.rain = rain; // THIS IS THE WAY how you initialize an attribute
}
public double getRain() {
/*
Remember what I said about getters? This is an example
*/
return rain;
}
public void setRain(double[] rain) {
/*
Remember what I said about setters? This is an example
*/
this.rain = rain;
}
// More code...
}
现在,如果你想在另一个类中使用这个数组,你可以这样写:
public class SomeOtherClass {
// Some code
public static void main(String[] args) {
Rainfall rainfall = new Rainfall(); // Declare and instantiate the object
double[] rain = rainfall.getRain(); // Retrieve the value of the attribute
/*
And now... how to get the max value? Here's an example
*/
double maxVal = Double.NEGATIVE_INFINITY;
for(double x : rain) {
if(x > maxVal)
maxVal = x;
}
// More code
}
}
我真的建议你好好阅读The Java Tutorials。