当前位置: 首页 > 知识库问答 >
问题:

Java -求解简单阵列地震程序

隆安然
2023-03-14

我有这样的家庭作业问题:

以下数据代表地震的里氏震级数据。编写一个程序来计算和打印任何有效地震数据的平均值。

将里氏震级值存储在名为quakeLevels的双精度数组中。

不幸的是,你的地震仪有时会产生不可靠的读数(如本例中的10.1)。所以你决定去掉最大和最小读数。

您的程序应该执行以下操作:

使用以下数据声明并初始化quakeLevels数组。{ 5.6, 6.2, 4.0, 5.5, 5.7, 6.1,7.4, 8.5, 5.5, 6.3, 6.4, 2.1, 6.9, 4.3, 3.1, 7.0, 10.1 }

确定数组中的最大值和最小值。计算数组内容的平均值,不包括最大值和最小值。打印数组中不包括最大值和最小值的值。打印平均值。

我不能使用数学课,所以这就是为什么所有东西都被写出来打印max和min的原因。

这是我到目前为止的代码:

 public class ARRAYminAndmax0RichterScale
    {
        public static void main(String [] args)
        {
            double [] quakeLevels = { 5.6, 6.2, 4.0, 5.5, 5.7, 6.1 ,7.4, 8.5, 5.5, 6.3, 6.4, 2.1, 6.9, 4.3, 3.1, 7.0, 10.1};    
            double [] quakeLevelsNormalized = new double [(quakeLevels.length - 2)];
            int i;

            int minIndex = 0;  // start with 0th element as min
            for ( i = 1; i < quakeLevels.length; i++) {                 
                if (quakeLevels[i] < quakeLevels[minIndex]) {
                    minIndex = i;                        
                }                    
            }
            System.out.print("Min: " + quakeLevels[minIndex] + "    ");

            int maxIndex = 0;  // start with 0th element as max
            for ( i = 1; i < quakeLevels.length; i++) {                 
                if (quakeLevels[i] > quakeLevels[maxIndex]) {
                    maxIndex = i;                        
                }                    
            }

            System.out.println("Max: " + quakeLevels[maxIndex]);                
           System.out.println("The Richter values, excluding the extrema, are as follows: ");   

            //make a new array excluding the max and min
            for ( i = 1; i < quakeLevels.length - 2; i++ ) {

                if(quakeLevels[i]!= minIndex && quakeLevels[i]!= maxIndex){
                    quakeLevelsNormalized[i] = quakeLevels[i];
                    System.out.printf("%6s\n", quakeLevelsNormalized[i] );
                }                    
            }

          //***THIS LOOP IS HERE TO HELP ME FIGURE OUT THE PROBLEM***
            for( i =0; i < quakeLevelsNormalized.length; i++){
                System.out.println("quakeLevelsNormalized["+i+"] = " + quakeLevelsNormalized[i]);
            }

            //find average of quakeLevelsNormalized
            double arrayTotal = 0;
            double average = 0;
            for (i = 0; i < quakeLevelsNormalized.length; i++) {

                arrayTotal = arrayTotal + quakeLevelsNormalized[ i ];

            }
            average = arrayTotal / quakeLevelsNormalized.length;

            //output

            System.out.println( quakeLevelsNormalized[i-1]);
            System.out.printf("%s%.1f\n","Average Quake Level = ", average);
        }

    }
Min: 2.1    Max: 10.1

The Richter values, excluding the extrema, are as follows: 
   6.2
   4.0
   5.5
   5.7
   6.1
   7.4
   8.5
   5.5
   6.3
   6.4
   2.1
   6.9
   4.3
   3.1
quakeLevelsNormalized[0] = 0.0
quakeLevelsNormalized[1] = 6.2
quakeLevelsNormalized[2] = 4.0
quakeLevelsNormalized[3] = 5.5
quakeLevelsNormalized[4] = 5.7
quakeLevelsNormalized[5] = 6.1
quakeLevelsNormalized[6] = 7.4
quakeLevelsNormalized[7] = 8.5
quakeLevelsNormalized[8] = 5.5
quakeLevelsNormalized[9] = 6.3
quakeLevelsNormalized[10] = 6.4
quakeLevelsNormalized[11] = 2.1
quakeLevelsNormalized[12] = 6.9
quakeLevelsNormalized[13] = 4.3
quakeLevelsNormalized[14] = 3.1
3.1
Average Quake Level = 5.2

所以这显然不是它应该有的样子。为什么它在最后给了我额外的3.1?而它只有14个元素,当它应该有[18减去两个极端]?我是一个初学编程的人——非常感谢任何帮助!!

共有3个答案

景鸿才
2023-03-14

一些可能的方法可以帮助您:

    /**
     * Get the largest number in the array
     */
    public double getMax(double[] array) {
            double max = Double.MIN_VALUE;
            for (double n : array) {
                    if (n > max) max = n;
            }
            return max;
    }

    /**
     * Get the smallest number in the array
     */
    public double getMin(double[] array) {
            double min = Double.MAX_VALUE;
            for (double n : array) {
                    if (n < min) min = n;
            }
            return min;
    }

    /**
     * Remove the specified number from the array, return a new array with the number removed
     */
    public double[] removeFromArray(double[] array, double number) {
            int count = 0;
            for (double n : array) {
                    if (n==number) count++;
            }
            double[] result = new double[array.length - count];
            int index = 0;
            for (double n : array) {
                    if (n!=number) result[index++] = n;
            }
            return result;
    }

    /**
     * Work out the mean of all the numbers in an array
     */
    public double averageOfArray(double[] array) {
            double total = 0;
            for (double n : array) {
                    total += n;
            }
            return total / array.length;
    }
吕淮晨
2023-03-14

我想你可能在比较中有一个错误。

if(quakeLevels[i]!=最小索引

在此行中,您询问索引 i 处的值是否与最小/最大索引相同。这不符合您的要求,您需要直接将i与maxIndex和minIndex进行比较。

尹英华
2023-03-14

我认为您甚至不需要quakeLevelsNormalized数组。这是我的解决方案,无论如何,这个逻辑可以改进:

public class ARRAYminAndmax0RichterScale {
    public static void main(String[] args) {
        double[] quakeLevels = { 5.6, 6.2, 4.0, 5.5, 5.7, 6.1, 7.4, 8.5, 5.5,
                6.3, 6.4, 2.1, 6.9, 4.3, 3.1, 7.0, 10.1 };
        int i;
        int minIndex = 0; // start with 0th element as min
        for (i = 1; i < quakeLevels.length; i++) {
            if (quakeLevels[i] < quakeLevels[minIndex]) {
                minIndex = i;
            }
        }
        System.out.print("Min: " + quakeLevels[minIndex] + "    ");
        int maxIndex = 0; // start with 0th element as max
        for (i = 1; i < quakeLevels.length; i++) {
            if (quakeLevels[i] > quakeLevels[maxIndex]) {
                maxIndex = i;
            }
        }
        System.out.println("Max: " + quakeLevels[maxIndex]);
        System.out.println();
        System.out.println("The Richter values, excluding the extrema, are as follows: ");
        double arrayTotal = 0;
        // make a new array excluding the max and min
        for (i = 0; i < quakeLevels.length; i++) {
            if (i != minIndex && i != maxIndex) {
                System.out.printf("%6s\n", quakeLevels[i]);
                arrayTotal += quakeLevels[i];
            }
        }
        double average = arrayTotal / (quakeLevels.length - 2);
        // output
        System.out.printf("%s%.1f\n", "Average Quake Level = ", average);
    }

}

希望这对你有帮助。

 类似资料:
  • 任务是: 编写一个程序,为在XYZ书店购买任意两本书的会员提供20%的折扣。(提示:将常量变量用于20%的折扣。) 我已经做了编码,但是不能提示书名,然后显示折扣价,请在下面看到我的编码,根据需要修改。

  • 问题内容: 我有一个列表,我只想在满足特定条件的条目上使用特定功能-保留其他条目不变。 例如:假设我只想将偶数个元素乘以2。 想要的结果: 但是产量(它还充当过滤器)。 正确的解决方法是什么? 问题答案: 使用条件表达式: (数学怪人的注:您也可以使用 但无论如何我还是更喜欢第一种选择;)

  • 例4.5.6...2意味着4个意向转到第一个邮箱,5转到第二个邮箱,以此类推。 我如何给出正确的单元数组形式的输入。由于数据和算法都来自同一个库,我想不知何故,我已经在一个形式中的数据很容易转换成单元数组!

  • 本文向大家介绍MATLAB单元阵列,包括了MATLAB单元阵列的使用技巧和注意事项,需要的朋友参考一下 示例 同一类的元素通常可以连接成数组(有一些罕见的例外,例如函数句柄)。数值标量(默认为class double)可以存储在矩阵中。 char在MATLAB中属于类的字符也可以使用类似的语法存储在数组中。这样的数组类似于许多其他编程语言中的字符串。 请注意,尽管两者都使用方括号[和],结果类却不

  • 问题内容: 我需要将几个数组合并为一个数组。描述我要寻找的内容的最好方法是将阵列“交织”为单个阵列。 例如,从数组#1中取出第一项,然后追加到最终数组中。从数组2获取项目1,然后追加到最终数组。从数组#1获得第二项并追加…等。 最终的数组如下所示: array#1.element#1 array#2.element#1。。。 “踢球者”是各个阵列可以具有各种长度。 是否有更好的数据结构要使用? 问