【问题标题】:Read Scores from text file and sort into array从文本文件中读取分数并排序到数组中
【发布时间】:2018-10-01 12:54:58
【问题描述】:

我正在努力让我的考试成绩计划发挥作用。每当我输入要从文本文件中读取的路径文件时,程序都会读取 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

【问题讨论】:

  • 仔细看看你在分数文件的第一行做了什么。

标签: java arrays filereader


【解决方案1】:
int scores[] = new int[stats.nextInt()];

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

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

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

【讨论】:

  • 如何将其更改为列表?
  • 简单,List list = new LinkedList();创建一个(链表不是唯一的列表类型,有很多)list.add(stats.nextInt());将下一个int添加到列表中list.size();获取大小列表的list.toArray(); 为您提供列表的数组。您可以在阅读所有标记后执行此操作,因此您不必更改其他代码。哦,你将不得不import java.util.LinkedList;有关列表的更多信息,请参阅:docs.oracle.com/javase/7/docs/api/java/util/List.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
相关资源
最近更新 更多