2014-07-21 45 views
-1

對不起,我會重申這個問題。我有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); 
} 

} 
+1

這不是一類,它的兩個方法。也沒有''main''。請使用所有相關信息更新您的問題。用語言標記你的問題也會有幫助,這是Java嗎? – aruisdante

+0

@aruisdante我有一堆我想在我的Driver類中調用的方法。在哪裏以及如何創建一個包含12個元素的數組,以便我可以從主類中調用方法..對不起,我的代碼太雜亂了......我已經在這個相當長一段時間了這讓我瘋狂! – misheekoh

+1

@misheekoh:Re:「對不起,我的代碼太混亂了」:你不需要道歉。只是把它清理乾淨,沒有人會在意它過去是混亂的。 – ruakh

回答

0

我看到它的方式,你不太瞭解什麼是OOP,以及如何使用它:

你正在編寫一個名爲Rainfall類,而你(正確)宣佈其成員(屬性)...但你並沒有初始化它們!您的構造函數聲明一個本地變量,其名稱與您的類的某個屬性相同,但不會在任何地方使用它。

如果我所看到的是正確的,那麼你想要的東西是這樣的:

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

0

我不知道你在問什麼......但是像這樣的工作,以找到rain陣列的最大元素。

int largestNum = 0; 
for (int i = 0; i < 12; i++) 
{ 
    if (rain [ i ] >= largestNum) 
     { 
      largestNum = rain[ i ]; 
     }    
} 
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(); 
相關問題