2012-05-06 1 views
0

今年の降雨量などを計算するプログラムを作成しています。しかし、私は今、プログラムを変更しようとしているので、配列の値が指定されています(私は基本的にユーザー入力を取り除こうとしています)。配列Java - ユーザーの入力要件を取り除こうとしています

なぜ2番目のコードブロックが機能しないのですか? r.getTotalRainFall、r.getAverageRainFallなどのエラーが底をついています。

私はこの年のアレイを導入しなければならなかったことに注意してください(これは必須です)。

コードブロック#1:

import java.util.*; 

public class Rainfall { 

Scanner in = new Scanner(System.in); 
int month = 12; 
double total = 0; 
double average; 
double months[]; 

public Rainfall() { 
    months = new double[12]; 
} 

public void enterMonthData() { 
    for (int n = 1; n <= month; n++) { 
     System.out.print("Enter the rainfall (in inches) for month #" + n + ": "); 
     months[n - 1] = in.nextDouble(); 

     // Input Validation - Cannot accept a negative number 
     while (months[n - 1] < 0) { 
      System.out.print("Rainfall must be at least 0. Please enter a new value."); 
      months[n - 1] = in.nextDouble(); 
     } 
    } 
} 

public double getTotalRainFall() { 
    total = 0; 
    for (int i = 0; i < 12; i++) { 
     total = total + months[i]; 
    } 
    return total; 
} 

public double getAverageRainFall() { 
    average = total/12; 
    return average; 
} 

/** 
* Returns the index of the month with the highest rainfall. 
*/ 
public int getHighestMonth() { 
    int highest = 0; 
    for (int i = 0; i < 12; i++) { 
     if (months[i] > months[highest]) { 
      highest = i; 
     } 
    } 
    return highest; 
} 

/** 
* Returns the index of the month with the lowest rainfall. 
*/ 
public int getLowestMonth() { 
    int lowest = 0; 
    for (int i = 0; i < 12; i++) { 
     if (months[i] < months[lowest]) { 
      lowest = i; 
     } 
    } 
    return lowest; 
} 

public static void main(String[]args) { 
    Rainfall r = new Rainfall(); 
    r.enterMonthData(); 
    System.out.println("The total rainfall for this year is " + r.getTotalRainFall()); 
    System.out.println("The average rainfall for this year is " + r.getAverageRainFall()); 
    int lowest = r.getLowestMonth(); 
    int highest = r.getHighestMonth(); 
    System.out.println("The month with the highest amount of rain is " + (highest+1) + " with " + r.months[highest] + " inches"); 
    System.out.println("The month with the lowest amount of rain is " + (lowest+1) + " with " + r.months[lowest] + " inches"); 
} 
} 

コードブロック#2:

package rain; 

public class Rain { 

int month = 12; 
double total = 0; 
double average; 
double getRainAt[]; 

public Rain { 
    getRainAt = new double[12]; 
} 

double getTotalRainFall() { 
    total = 0; 
    for (int i = 0; i < 12; i++) { 
     total = total + getRainAt[i]; 
    } 
    return total; 
} 

    double getAverageRainFall() { 
    average = total/12; 
    return average; 
} 

int getHighestMonth() { 
    int high = 0; 
    for (int i = 0; i < 12; i++) { 
     if (getRainAt[i] > getRainAt[high]) { 
      high = i; 
     } 
    } 
    return high; 
} 

int getLowestMonth() { 
    int low = 0; 
    for (int i = 0; i < 12; i++) { 
     if (getRainAt[i] < getRainAt[low]) { 
      low = i; 
     } 
    } 
    return low; 
} 


public static void main(String[] args) { 
    // Create an array of rainfall figures. 
    double[] thisYear = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 
         3.9, 2.6, 2.9, 4.3, 2.4, 3.7 }; 

    int high;  // The high month 
    int low;  // The low month 

    // Create a RainFall object initialized with the figures 
    // stored in the thisYear array. 
    Rainfall r = new Rainfall(thisYear); 

    // Display the statistics. 
    System.out.println("The total rainfall for this year is " + 
        r.getTotalRainFall(); 
    System.out.println("The average rainfall for this year is " + 
        r.getAverageRainFall()); 
    high = r.getHighestMonth(); 
    System.out.println("The month with the highest amount of rain " + 
        "is " + (high+1) + " with " + r.getRainAt(high) + 
        " inches."); 
    low = r.getLowestMonth(); 
    System.out.println("The month with the lowest amount of rain " + 
        "is " + (low+1) + " with " + r.getRainAt(low) + 
        " inches."); 
    } 
    } 
} 

答えて

2

私はリファクタリングしている2クラス

他のすべてのロジックは

降雨クラスはメソッドがある降雨クラスに含まれているのに対し、今雨のクラスはmainメソッドが含まれています - getRainAt()を取得するためにRainfallクラスのRainbaseは与えられた月にRainfallクラスの に2倍の配列を引数として取るコンストラクタを持ちます。したがって、この引数でインスタンス化する必要があります。

今すぐクラスを見て、これがあなたの要件に合っているかどうかを確認してください。

import java.util.*; 

public class Rainfall { 

Scanner in = new Scanner(System.in); 
int month = 12; 
double total = 0; 
double average; 
double months[]; 

public Rainfall(double newmonths[]){ 
    months = newmonths; 
} 

public void enterMonthData() { 
    for (int n = 1; n <= month; n++) { 
     System.out.print("Enter the rainfall (in inches) for month #" + n 
       + ": "); 
     months[n - 1] = in.nextDouble(); 

     // Input Validation - Cannot accept a negative number 
     while (months[n - 1] < 0) { 
      System.out 
        .print("Rainfall must be at least 0. Please enter a new value."); 
      months[n - 1] = in.nextDouble(); 
     } 
    } 
} 

public double getTotalRainFall() { 
    total = 0; 
    for (int i = 0; i < 12; i++) { 
     total = total + months[i]; 
    } 
    return total; 
} 

public double getAverageRainFall() { 
    average = total/12; 
    return average; 
} 

/** 
* get rain given the month number 
*/ 
public double getRainAt(int month){ 
    double rainValue = 0; 
    for (int i = 0; i < months.length; i++) { 
     if(month == i){ 
      rainValue = months[i]; 
      break; 
     } 
    } 
    return rainValue; 
} 

/** 
* Returns the index of the month with the highest rainfall. 
*/ 
public int getHighestMonth() { 
    int highest = 0; 
    for (int i = 0; i < 12; i++) { 
     if (months[i] > months[highest]) { 
      highest = i; 
     } 
    } 
    return highest; 
} 

/** 
* Returns the index of the month with the lowest rainfall. 
*/ 
public int getLowestMonth() { 
    int lowest = 0; 
    for (int i = 0; i < 12; i++) { 
     if (months[i] < months[lowest]) { 
      lowest = i; 
     } 
    } 
    return lowest; 
} 
} 

雨のクラスは

public class Rain { 

public static void main(String[] args) { 
    // Create an array of rainfall figures. 
    double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 
      2.4, 3.7 }; 

    int high; // The high month 
    int low; // The low month 

    // Create a RainFall object initialized with the figures 
    // stored in the thisYear array. 
    Rainfall r = new Rainfall(thisYear); 

    // Display the statistics. 
    System.out.println("The total rainfall for this year is " 
      + r.getTotalRainFall()); 
    System.out.println("The average rainfall for this year is " 
      + r.getAverageRainFall()); 
    high = r.getHighestMonth(); 
    System.out.println("The month with the highest amount of rain " + "is " 
      + (high + 1) + " with " + r.getRainAt(high) + " inches."); 
    low = r.getLowestMonth(); 
    System.out.println("The month with the lowest amount of rain " + "is " 
      + (low + 1) + " with " + r.getRainAt(low) + " inches."); 
    } 
} 

今mainメソッドを持っている、これはそのための

+0

これは完璧に動作します! getRainAtメソッドで余分な詳細を追加する必要がある理由を教えてください。本当にありがとう! – SeekingCharlie

+0

getRainAtメソッドの機能は、月の値を整数として取り、その年の配列として雨量値を持つ月のdouble配列を繰り返し、アレイインデックスに基づいて降水値を返します。 – Sanath

+0

あなたの実装のgetRainAtは二重配列ですが、私はそれに対して特定のメソッドを作成しています。このメソッドを呼び出すと、rainfallsを含むdouble配列を使用して、正しい落葉の値 – Sanath

1

あなたはそれを初期化するgetRainAtクラスを作成している理由はわからない、やって雨のクラスのコンストラクタを使用してみてくださいこの。

この置き換えますと

public class getRainAt { 
    public getRainAt() { 
    getRainAt = new double[12]; 
    } 
} 

を:

public Rain() { 
    getRainAt = new double[12]; 
} 

、あなたが今、代わりに降雨の雨を使用しているので、メインの方法で、それは次のようになります。 雨のR =新雨() ;

+0

感謝に役立ちます願っています。私はそれを更新しましたが、まだエラーが発生しています。変数rに問題はありますか? – SeekingCharlie

+0

それはRainですか?r = new Rainですか? (これを答えに追加しました) –

1

これは単なるコピーエラーだったのかどうかはわかりませんが、2番目のブロックではクラスRainを呼び出しましたが、rをRainfallと宣言しました。

関連する問題