java - 从填充数组计算范围时出现问题(Java)

标签 java arrays range

我目前正在为我的计算机访问类(class)做作业。下面我截取了该项目的上下文。我们的老师给了我们代码片段来帮助我们开始。在本例中,他为我们提供了填充用户的 2 个数组的代码。我编写了代码来查找数组中的最小数字(以计算范围)并且它有效。如下图...

上下文:https://i.imgur.com/sReC8ym.png

public static void main(String[] args) {
    double[] values = {81, 52, 10, 50, 18, 4, 7};
    double smallest = values[0];
    for (int i = 0; i < values.length; i++) {
        if (values[i] < smallest) {
            smallest = values[i];

        }
    }
    System.out.println(smallest);
}

但是,当我在项目中实现此代码时,结果始终为 0。我认为问题出在片段代码上。找到最大数字的函数在项目中运行。找到最大的数字并服从最小的数字是可行的,但是最小的数字结果总是0。我已经将整个程序粘贴在下面。抱歉,如果修复工作量太大,您有什么建议吗?谢谢

public static String[] strings;
public static double[] data;

public static void main(String[] args) {

    populatArrays();
    printArrays(strings, data);
    double theAverage = calculateAverage(data);
    System.out.println("The average rainfall is " + theAverage);
    double largest = getLargest(data);
    System.out.println("The largest amount of rainfall is " + largest);
    double smallest = getSmallest(data);
    System.out.println("The smallest amount of rainfall is " + smallest);
    double theRange = getRange(data);
    System.out.println("The rainfall range is " + theRange);
    double theTotal = getTotal(data);
    System.out.println("The total rainfall is " + theTotal);
}

//User input for the array & printing out questions
public static void populatArrays() {

    Scanner input = new Scanner(System.in);
    System.out.println("How many cities would you like to add to the Rainfall calculator?");
    int elements = input.nextInt();
    String[] someStrings = new String[elements];
    double[] someData = new double[elements];

    for (int i = 0; i < someData.length; i++) {
        input = new Scanner(System.in);
        System.out.println("Enter a City");
        someStrings[i] = input.nextLine();
        System.out.println("Enter the annual rainfall");
        someData[i] = input.nextDouble();
    }
    strings = someStrings;
    data = someData;
}

//code printing out the array on screen
public static void printArrays(String[] stringArray, double[] doubleArray) {

    for (int i = 0; i < stringArray.length; i++) {
        System.out.println("City: " + stringArray[i]);
        System.out.println(doubleArray[i] + "mm");

    }
}

//code calculating the mean average
public static double calculateAverage(double[] thenumbers) {

    double sum = 0;
    for (double thenumber : thenumbers) {
        sum += thenumber;
    }

    double result = sum / thenumbers.length;
    return result;

}

//code calculating the largest number in the array
public static double getLargest(double[] thenumbers) {

    double largest = 0;

    for (int i = 0; i < thenumbers.length; i++) {
        if (thenumbers[i] > largest) {
            largest = thenumbers[i];
        }
    }

    return largest;
}

//code calculating the smallest number in the array
public static double getSmallest(double[] thenumbers) {

    double smallest = 0;

    for (int i = 0; i < thenumbers.length; i++) {
        if (thenumbers[i] < smallest) {
            smallest = thenumbers[i];
        }
    }

    return smallest;
}

// code calculating the range
public static double getRange(double[] thenumbers) {

    double largest = getLargest(data);
    double smallest = getSmallest(data);

    double theRange = largest + smallest;

    return theRange;

}

// code calculating the total rainfall in the array
public static double getTotal(double[] thenumbers) {

    double sum = 0;
    for (double thenumber : thenumbers) {
        sum += thenumber;
    }
    double theTotal = sum;
    return theTotal;

}

}

最佳答案

您将最小值的初始值设置为 0

由于没有数据小于 0,因此最小的值永远不会被设置为另一个数字。

我建议不要将值设置为零,而是按照原始代码片段中的操作并将其设置为数组的第一个值

double smallest = thenumbers[0];

您也在 getLargest 函数中执行相同的操作,因此如果列表中的最大数字为负数,它将返回 0。

作为额外的好处,您还可以使用 for in/each 运算符来压缩代码一点 :

//code calculating the smallest number in the array
public static double getSmallest(double[] thenumbers) {

  double smallest = thenumbers[0];

  for (double num : thenumbers) {
    if (num < smallest) {
      smallest = num;
    }
  }

  return smallest;
}

关于java - 从填充数组计算范围时出现问题(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888146/

相关文章:

java - 如何计算一个范围的平均值?

c - 有符号整型的大小范围

java - 无法在 Java Spring 中反序列化 JSONObject(在 JSONArray 内部)

ios - 如何在 swift 中就地改变输入数组?

python - 切片多维 numpy 数组以获得向量

c++ - 原子变量的 vector (数组)

java - 自定义阵列适配器返回空白屏幕?

java - 如何在 lambda 函数中访问方法局部变量?

java - 需要在 git 中有两个不同的分支,忽略不同的文件,但可以轻松 merge

javascript - 我有一个范围输入来更改颜色,但它不起作用