简体   繁体   English

如何对Java文本文件进行排序,以便获得中位数?

[英]How do I sort a java text file so that I can obtain the median?

I have the code below so far. 到目前为止,我有下面的代码。 I know the median is wrong because I've placed numbers in it and what I really want is for the program to extract these from the file on its own since the numbers may change. 我知道中位数是错误的,因为我已经在其中放置了数字,而我真正想要的是程序可以自行从文件中提取这些数字,因为数字可能会发生变化。 I am not sure how to have the program get the 2 numbers in order to retrieve and calculate the median. 我不确定如何让程序获取2个数字以检索和计算中位数。 Please help. 请帮忙。 I am very new to this and it has taken me all day to get this far! 我对此很陌生,花了我整整一天才走到这一步!

    package trials;

import java.util.Scanner;
import java.io.IOException;
import java.io.File;

public class trials2 {

public static void main(String[] args) throws IOException {
    // This is a Scanner object that reads from the keyboard
    Scanner in = new Scanner(System.in);

    // The following is set to find the file
    System.out.println("Please enter the name of your data file: ");
    String fileName = in.next();

    // The file is then to be scanned
    Scanner fileToRead = new Scanner(new File(fileName));

    // This loop finds the contents within the file
    double sum = 0;
    int numStudents = 0;
    double maxVal = 0, minVal = 0;
    boolean bFirstTime = true;
    double currVal;
    while (fileToRead.hasNext()) { 
        if (fileToRead.hasNextDouble()) {
            numStudents++;
            currVal = fileToRead.nextDouble();

            // The following will find the maximum and minimum values within the file
            if (bFirstTime) {
                maxVal = currVal;
                minVal = currVal;
                bFirstTime = false;
            } else {
                maxVal = Math.max(maxVal,currVal);
                minVal = Math.min(minVal, currVal);
            }

            sum += currVal;
        } else {
            fileToRead.next();
        }   
    }
   // Prints out comments and results
   System.out.println("***Welcome to the Exam Statistics Program!!***");
   System.out.println();
   System.out.println("Minimum = " + minVal);
   System.out.println("Maximum = " + maxVal);   
   System.out.println("Average score: " + sum/numStudents);
   System.out.println();
   System.out.println("Number of scores by letter grade: ");
   System.out.println();       
   System.out.println("There are " + numStudents + " scores");
}
}

There are a few steps to doing this. 有几个步骤可以做到这一点。

  1. At the beginning of your program, create an ArrayList<Double> for storing your values in. 在程序的开头,创建一个ArrayList<Double>用于存储您的值。

  2. Within your main loop, use the list's add method to add each value as you read it. 在主循环中,使用列表的add方法在读取每个值时add其相加。

  3. At the end of your loop, Use Collections.sort to sort your list. 在循环结束时,使用Collections.sort对列表进行排序。

  4. Use the following logic to work out the median. 使用以下逻辑计算中位数。

    • If the size of the list is zero, then there's no median. 如果列表的大小为零,则没有中间值。
    • If the size of the list is odd, then the median is the value at position size() / 2 of the list. 如果列表的大小为奇数,则中位数为列表的位置size() / 2处的值。
    • If the size of the list is even, then the median is the average of the value at position size() / 2 - 1 and size() / 2 of the list. 如果列表的大小是偶数,则中位数是列表的位置size() / 2 - 1size() / 2处的值的平均值。

I deliberately haven't given you code, because I think you're enjoying learning how to do this for yourself. 我故意没有给您代码,因为我认为您喜欢自己学习如何做。 But feel free to post a comment if you need more detail on any particular step. 但是,如果您需要任何特定步骤的更多详细信息,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM