【问题标题】:How would I make the application add all the values of the integers in the arraylist so I could find the median of each grade?如何让应用程序将所有整数值添加到数组列表中,以便找到每个年级的中位数?
【发布时间】:2021-07-22 23:26:00
【问题描述】:
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("How many grades do you want to input: ");

        int amount = scanner.nextInt();
        int i = 0;

        ArrayList<Integer> myList = new ArrayList<>();

        Scanner sc = new Scanner(System.in);
        while (i < amount) {
            System.out.print("Enter a grade (between 0 and 100): ");
            myList.add(sc.nextInt());
            i++;
        }

        int highestNumber = Collections.max(myList);
        int lowestNumber = Collections.min(myList);

        System.out.println("The highest grade is: " + highestNumber);
        System.out.println("The lowest grade is: " + lowestNumber);
    }
}

【问题讨论】:

标签: java


【解决方案1】:

您的代码已经这样做了 您只需在主目录末尾添加以下内容即可查看您的列表

System.out.println(myList);

【讨论】:

    【解决方案2】:

    求中位数。

    • 对数组进行排序。
    • 如果数组包含奇数个元素,则取数组的中间值。
    • 如果数组包含偶数,则将中间的两个值相加并除以2

    所以中位数就是上面和下面一样多的值。

    a = {1,2,3,5,1000,2000,3000}
    

    a 中值的中位数是5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      相关资源
      最近更新 更多