数组Java - 试图删除用户输入要求

时间:2012-05-06 06:12:09

标签: java arrays

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

为什么第二块代码不起作用?我在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.");
    }
  }
}

3 个答案:

答案 0 :(得分:2)

我重新考虑了2个班级

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

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

立即查看课程,看看这是否符合您的要求。

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 class现在只有主要方法

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

希望这会有所帮助

答案 1 :(得分: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();

答案 2 :(得分:1)

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