0

我正在创建一个计算全年降雨量的程序等。我在下面的第一块代码中完美地处理了用户输入。但是,我现在正在尝试更改程序,以便指定数组值(我基本上是在尝试消除用户输入)。

为什么第二个代码块不起作用?我在底部收到 r.getTotalRainFall、r.getAverageRainFall 等错误。

请注意,我必须在 thisYear 中引入数组(这是必需的)。

代码块 #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.");
    }
  }
}
4

3 回答 3

2

我已经重构了 2 个类

现在 Rain 类只包含 main 方法,而所有其他逻辑都包含在 Rainfall 类中

Rainfall 类有一个方法 - getRainAt() 来获取给定月份的降雨量 Rainfall 类有一个构造函数,该构造函数将双数组作为参数,因此必须使用提供的此参数对其进行实例化。

现在看看这些课程,看看这是否符合您的要求。

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;
}
}

Rain类现在只有main方法

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.");
  }
 }

希望这可以帮助

于 2012-05-06T08:39:08.893 回答
1

不知道为什么要创建一个 getRainAt 类来初始化它,请尝试使用 Rain 类构造函数来执行此操作。

替换这个:

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

和:

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

并且由于您现在使用的是 Rain 而不是 Rainfall,因此在 main 方法中应该是: Rain r = new Rain();

于 2012-05-06T06:20:11.807 回答
1

我不确定这是否只是一个复制错误,但在第二个块中你调用了 class Rain,但是你将 r 声明为Rainfall.

于 2012-05-06T06:44:35.397 回答