gpt4 book ai didi

java重复输出的一部分

转载 作者:行者123 更新时间:2023-12-02 04:53:50 26 4
gpt4 key购买 nike

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

输入文件

输入.txt:

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

99092;哈立德;7;5.5;7;8;8

99093;阿里;7;5.5;7;8;9

99094;艾哈迈德;7;5.5;7;8;9

输出.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

我的类(class)我有 4 个类(class),学生,输入处理程序,输出处理程序,主要

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
}

}

输入处理程序

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;
}
}

输出处理程序

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重复输出的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com