从文本文件中读取分数并排序为数组

时间:2018-10-01 12:54:58

标签: java arrays filereader

我正在尝试使我的考试成绩计划生效。每当我输入路径文件以从文本文件中读取时,程序都会从​​5个数字中读取4个。此外,无论有多少数字,当它不为真时,我的最小值总是显示为0。任何帮助,我们都感激不尽!

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

public class Exam{

 public static void main(String[] args) throws IOException {

    System.out.println("Welcome!" + "\n");

 //File location
    System.out.println("Where is the data file:");
    Scanner userInput = new Scanner(System.in);
    String userFile = userInput.nextLine();

    int i = 0;
    int scores[] = readScores(userFile);

    System.out.println("Minimum score: " + scores[0]);
    System.out.println("Maximum score: " + scores[(scores.length - 1)]);

 //Average Calculation
    double gradesTotal = 0;
    for (i=0; i<scores.length; ++i){
        gradesTotal = gradesTotal + scores[i];
    }
    double mean = gradesTotal/scores.length;
    System.out.println("Average score: " + mean);

 //Mean Calculation
    double median;
    if (scores.length % 2 == 0)
        median = ((scores[(scores.length/2) - 1]) + scores[(scores.length/2)]) / 2;
    else
        median = scores[(scores.length/2)];
    System.out.println("Median score: " + median + "\n");

    //Number of Grades
    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;
    int gradeF = 0;


    for (i=0; i<scores.length; i++)
    {
        if (scores[i] >= 90 && scores[i] <=100){
            gradeA++;
        }
        else if (scores[i] <= 89 && scores[i] >=80){
            gradeB++;
        }
        else if (scores[i] <= 79 && scores[i] >=70){
            gradeC++;
        }
        else if (scores[i] <= 69 && scores[i] >=60){
            gradeD++;
        }
        else if (scores[i] <= 59 && scores[i] >=1){

            gradeF++;
        }
    }

    System.out.println("Scores by letter grade: ");
    System.out.println("A: " + gradeA);
    System.out.println("B: " + gradeB);
    System.out.println("C: " + gradeC);
    System.out.println("D: " + gradeD);
    System.out.println("F: " + gradeF);
}

//Reads the data from the submitted file
private static int[] readScores(String userFile) throws FileNotFoundException
{
    File inputFile = new File(userFile);
    Scanner stats = new Scanner(inputFile);

    try {
        int scores[] = new int[stats.nextInt()];
        int i = 0;
        while (stats.hasNext()){
            scores[i] = stats.nextInt();
            i++;
        }
        System.out.println("\n" + "There are " + (i) + " scores" + "\n");
        Arrays.sort(scores);
        return scores;
    }
    finally {
        stats.close();
    }
 }
}

文本文件:

72
31
13 
39
74

程序输出:

There are 4 scores

Minimum score: 0
Maximum score: 74
Average score: 2.1805555555555554
Median score: 0.0

Number of scores by letter grade: 
A: 0
B: 0
C: 1
D: 0
F: 3

1 个答案:

答案 0 :(得分:2)

int scores[] = new int[stats.nextInt()];

在这里您采用了第一个值,然后在需要的地方没有它。

这也可能是您的计算如此混乱的原因。您创建长度为72的数组,并将该长度用作值的数量。

也许您想使用列表。它使您可以添加任意数量的值,而无需像使用数组时那样指定数字。