我有Java作业,这是对
在您的程序中,创建三个类:InputHandlerOutputHandler和Student类。 InputHandler类处理从输入文件中读取信息。它还包含一个Student对象数组,用于在其中存储信息。 OutputHandler类采用通过InputHandler类中的方法提供的学生数组。然后,应在“ results.txt”文件中打印每个学生的整个记录​​以及计算出的平均值。它还应计算并打印标准偏差。最后,Student类包含用于存储学生信息的字段。它还有一个称为getQuizAverage()的方法,该方法计算并返回该特定学生的测验平均值,OutputHandler使用该方法将其打印在结果文件中。

输入文件

input.txt:

99091;穆罕默德; 7; 5.5; 7; 8; 9

99092; Khalid; 7; 5.5; 7; 8; 8

99093; Ali; 7; 5.5; 7; 8; 9

99094; Ahmad; 7; 5.5; 7; 8; 9

Output.txt

99091穆罕默德7 5.5 7 8 9 7.3

99092哈立德7 5.5 7 8 8 7.1

99093阿里7 5.5 7 8 9 7.3

99094艾哈迈德7 5.5 7 8 9 7.3

我只得到一个重复输出的问题

99094艾哈迈德7 5.5 7 8 9 7.3

99094艾哈迈德7 5.5 7 8 9 7.3

99094艾哈迈德7 5.5 7 8 9 7.3

99094艾哈迈德7 5.5 7 8 9 7.3

我的班级我有4个班级,学生,输入处理器,输出处理器,主

public class Student {

    public String Name;
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }
    public double getQuiz1() {
        return quiz1;
    }

    public void setQuiz1(double quiz1) {
        this.quiz1 = quiz1;
    }
    public double getQuiz2() {
        return quiz2;
    }
    public void setQuiz2(double quiz2) {
        this.quiz2 = quiz2;
    }

    public double getQuiz3() {
        return quiz3;
    }

    public void setQuiz3(double quiz3) {
        this.quiz3 = quiz3;
    }

    public double getQuiz4() {
        return quiz4;
    }

    public void setQuiz4(double quiz4) {
        this.quiz4 = quiz4;
    }

    public double getQuiz5() {
        return quiz5;
    }

    public void setQuiz5(double quiz5) {
        this.quiz5 = quiz5;
    }

    public double getAvgQuiz() {
        return avgQuiz;
    }

    public void setAvgQuiz(double avgQuiz) {
        this.avgQuiz = avgQuiz;
    }

    public  String ID;
    public double quiz1;
    public double quiz2;
    public double quiz3;
    public double quiz4;
    public double quiz5;

    public double avgQuiz;

    public double getQuizAverage()
    {
       return ((quiz1+quiz2+quiz3+quiz4+quiz5)/5);
    }

    public Student() {
        // TODO Auto-generated constructor stub
    }

}


InputHandler

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class InputHandler {

    Student[] myStudent = new Student[4];

    public InputHandler(String fileName) {
        this.inputData(fileName);
    }

    private void inputData(String fileName) {

        try {
            File file = new File(fileName);
            FileReader fileReader = new FileReader(file);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;


            while ((line = bufferedReader.readLine())!=null)
            {
            String[] parts = line.split(";");


                for(int i=0;i<4;i++)
                {

                    myStudent[i] = new Student();
                    myStudent[i].setID(parts[0]);
                    myStudent[i].setName(parts[1]);
                    myStudent[i].setQuiz1(Double.parseDouble(parts[2]));
                    myStudent[i].setQuiz2(Double.parseDouble(parts[3]));
                    myStudent[i].setQuiz3(Double.parseDouble(parts[4]));
                    myStudent[i].setQuiz4(Double.parseDouble(parts[5]));
                    myStudent[i].setQuiz5(Double.parseDouble(parts[6]));
                }

        }


            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Student [] getStudents() {
        return myStudent;
    }
}


OutputHandler

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class OutputHandler {

    Student[] myStudent;

    public OutputHandler(Student[] stds) {

        myStudent = stds;
    }

    void writeData() throws IOException {
        FileWriter writt = new FileWriter("output.txt");

        for (int i = 0; i < 4; i++) {

            writt.write(myStudent[i].ID + '\t' + myStudent[i].Name + '\t'
                    + '\t' + myStudent[i].quiz1 + '\t' + myStudent[i].quiz2
                    + '\t' + myStudent[i].quiz3 + '\t' + myStudent[i].quiz4
                    + '\t' + myStudent[i].quiz5 + '\t'+"  "+myStudent[i].getQuizAverage());
            writt.write('\n');

        }

        writt.close();

    }
}


主功能

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {



    public static void main(String[] args) throws IOException {
        InputHandler ih=new InputHandler("input.txt");
        OutputHandler output1 = new OutputHandler(ih.getStudents());
        output1.writeData();

    }

}

最佳答案

您应该在InputHandler类inputData方法内删除一个for循环。因为您逐行阅读学生信息。再次循环是错误的。应该是这样

private void inputData(String fileName) {

    try {
        File file = new File(fileName);
        FileReader fileReader = new FileReader(file);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line;

        int studentIndex = 0;
        while ((line = bufferedReader.readLine())!=null)
        {
           String[] parts = line.split(";");
           myStudent[studentIndex] = new Student();
           myStudent[studentIndex].setID(parts[0]);
           myStudent[studentIndex].setName(parts[1]);
           myStudent[studentIndex].setQuiz1(Double.parseDouble(parts[2]));
           myStudent[studentIndex].setQuiz2(Double.parseDouble(parts[3]));
           myStudent[studentIndex].setQuiz3(Double.parseDouble(parts[4]));
           myStudent[studentIndex].setQuiz4(Double.parseDouble(parts[5]));
           myStudent[studentIndex].setQuiz5(Double.parseDouble(parts[6]));
           studentIndex++;

    }
        fileReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

关于java - Java重复输出的一部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28971000/

10-10 02:47