【问题标题】:Calculating Avg, Max, and Min using a .txt input file with both integers and strings mixed使用混合了整数和字符串的 .txt 输入文件计算 Avg、Max 和 Min
【发布时间】:2015-02-04 03:56:59
【问题描述】:

请帮忙。我到处看了看,不知道如何完成这个程序。我需要从数据输入文件中计算平均值、最大值和最小值。他们需要看起来像这样:

System.out.println("The average total score of the class is: " + total/27);
System.out.println(maxName + "got the maximum score of: " + maxVal);
System.out.println(minName + "got the maximum score of: " + minVal);

平均而言,我不知道为什么我的 main 中的 while 循环不起作用。它计算的值为零。我也不知道我是怎么做到的,所以 27 不是硬编码的。对于最大值和最小值,我对此一无所知。我什至没有猜测。对于这些我们没有被教过的使用方式,我已经看过很多东西(例如缓冲的东西,或者使用我们还没有学习过的 Collections 类。我只需要一种基本的完成方式这些。我将包括到目前为止的所有内容。感谢您的帮助。谢谢。

输入文件:

9
Andy Borders
200
250
400
John Smith
120
220
330
Alvin Smith
225
300
278
Mike Borell
250
250
500
Jim Jones
325
325
155
Robert Fennel
200
150
350
Craig Fenner
230
220
480
Bill Johnson
120
150
220
Brent Garland
220
240
350

主要:

import java.util.Scanner;
import java.io.*;
public class Project5 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the file name: ");
    String fileName = in.nextLine();
    try {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
        String SnumStudents = inputFile.nextLine();
        int numStudents = Integer.parseInt(SnumStudents);
        Student [] studentList = new Student[numStudents];
        for (int i = 0; i < numStudents; i++)
        {
            String line = inputFile.nextLine();
            String score1 = inputFile.nextLine();
            String score2 = inputFile.nextLine();
            String score3 = inputFile.nextLine();
            studentList [i] = new Student(line, Integer.parseInt(score1), Integer.parseInt(score2), Integer.parseInt(score3));
        }
        System.out.println("Name\t\tScore1\tScore2\tScore3\tTotal");
        System.out.println("---------------------------------------------");
        for (int i=0; i< studentList.length;i++){
            System.out.println(studentList[i].getName() + "\t" + studentList[i].getScore1() + "\t" + studentList[i].getScore2() + "\t" + studentList[i].getScore3() + "\t" + studentList[i].getTotal());
        }
        System.out.println("---------------------------------------------");
        System.out.println("The total number of students in this class is: " + studentList.length);
        int total = 0;
        while (inputFile.hasNextInt())
        {
            int value = inputFile.nextInt();
            total = total + value;
        }
        System.out.println("The average total score of the class is: " + total/27);
        System.out.println(maxName + "got the maximum score of: " + maxVal);
        System.out.println(minName + "got the maximum score of: " + minVal);
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + fileName);
    }
    finally {
    }
    in.close();
}
}

学生班:

public class Student {
private String name;
private int score1;
private int score2;
private int score3;
private int total;
private double average;

public Student(String n, int s1, int s2, int s3){
    name = n;
    score1 = s1;
    score2 = s2;
    score3 = s3;
    total = s1 + s2 + s3;
}
public String getName(){
    return name;
}
public int getScore1(){
    return score1;
}
public int getScore2(){
    return score2;
}
public int getScore3(){
    return score3;
}
public int getTotal(){
    return total;
}
public double computeAverage(){
    return average;
}
}

【问题讨论】:

    标签: java io max average min


    【解决方案1】:

    您的 while 循环永远不会被执行。当您在 for 循环中完成读取文件的行时,没有更多的文件行可供使用。所以,inputFile.hasNextInt() 永远不会是真的。

    27 是硬编码的,因为该行

    System.out.println("The average total score of the class is: " + total/27);
    

    您需要将此处的 27 更改为一些代表总得分的变量。

    您正在存储每个人的所有分数。要找到最小值(最大值),您只需遍历每个值并找到最小(最大)的值并存储它们。

    【讨论】:

    • 你说的每一句话都有道理,但没有多大帮助,因为我什至不知道语法是如何做这些事情的。
    猜你喜欢
    • 2018-05-07
    • 2019-01-07
    • 2019-07-29
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2021-07-15
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多