Java阵列-尝试删除用户输入要求

Java阵列-尝试删除用户输入要求,java,arrays,Java,Arrays,我正在创建一个程序来计算一年的降雨量等。下面的第一段代码完美地处理了用户输入。但是,我现在正在尝试更改程序,以便指定数组值(我基本上是在尝试消除用户输入) 为什么第二段代码不起作用?我在r.getTotalRainFall、r.getAveragerRainfall等的底部得到错误 请注意,我今年必须介绍阵列(这是必需的) 代码块#1: import java.util.*; 公共级降雨{ 扫描仪输入=新扫描仪(系统输入); 整月=12; 双倍合计=0; 双倍平均; 双月[]; 公众雨量(){

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

为什么第二段代码不起作用?我在r.getTotalRainFall、r.getAveragerRainfall等的底部得到错误

请注意,我今年必须介绍阵列(这是必需的)

代码块#1:

import java.util.*;
公共级降雨{
扫描仪输入=新扫描仪(系统输入);
整月=12;
双倍合计=0;
双倍平均;
双月[];
公众雨量(){
月份=新的双倍[12];
}
公共无效enterMonthData(){
对于(整数n=1;n个月[最高]){
最高=i;
}
}
回报最高;
}
/**
*返回降雨量最低的月份的索引。
*/
公共整数getLowestMonth(){
int最低=0;
对于(int i=0;i<12;i++){
如果(月[i]<月[最低]){
最低=i;
}
}
回报率最低;
}
公共静态void main(字符串[]args){
降雨量r=新降雨量();
r、 enterMonthData();
System.out.println(“今年的总降雨量为”+r.getTotalRainFall());
System.out.println(“今年的平均降雨量为”+r.getAverageRainFall());
int lowest=r.getLowestMonth();
int highest=r.getHighestMonth();
System.out.println(“降雨量最高的月份为“+(最高+1)+”和“+r.months[最高]+”英寸”);
System.out.println(“降雨量最低的月份为“+(最低+1)+”和“+r.months[最低]+”英寸”);
}
}
代码块#2:

包雨;
公共课雨{
整月=12;
双倍合计=0;
双倍平均;
双重getRainAt[];
公共雨{
getRainAt=新双精度[12];
}
双getTotalRainFall(){
总数=0;
对于(int i=0;i<12;i++){
总计=总计+getRainAt[i];
}
返回总数;
}
double getAverageRainFall(){
平均数=总数/12;
收益率平均值;
}
int getHighestMonth(){
int高=0;
对于(int i=0;i<12;i++){
if(getRainAt[i]>getRainAt[high]){
高=i;
}
}
高回报;
}
int getLowestMonth(){
int低=0;
对于(int i=0;i<12;i++){
if(getRainAt[i]
不确定创建getRainAt类只是为了初始化它的原因,请尝试使用Rain类构造函数来完成此操作

替换此项:

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

由于您现在使用的是雨水而不是雨水,因此在主要方法中,它应该是:
Rain r=new Rain();

不确定为什么要创建getRainAt类来初始化它,请尝试使用Rain类构造函数来完成此操作

替换此项:

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

由于您现在使用的是雨水而不是雨水,因此在主要方法中,它应该是:
雨r=新雨();

我不确定这是否只是一个复制错误,但在第二个块中,您调用了类
Rain
,但随后您将r声明为
Rain
,我不确定这是否只是一个复制错误,但在第二个块中,您调用了类
Rain
,但随后您将r声明为
Rain
重新考虑了这两个类

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

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

现在看看这些类,看看这是否符合您的要求

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方法,而所有其他逻辑都包含在Rain类中

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

现在看看这些类,看看这是否符合您的要求

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

希望这有帮助

谢谢。我已经更新了它,但是仍然有错误。变量r有问题吗?应该是Rain r=new Rain吗?(我在答案中添加了这个)谢谢。我已经更新了它,但是仍然有错误。变量r有问题吗?应该是Rain r=new Rain吗?(我在答案中添加了这个)效果非常好!您能解释一下为什么需要使用getRainAt方法添加额外的细节吗?非常感谢!getRainAt方法的作用是,它将月份值作为
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.");
  }
 }