gpt4 book ai didi

java - Getter 和 Setter 方法以及读取 txt 文件

转载 作者:行者123 更新时间:2023-12-01 13:54:40 25 4
gpt4 key购买 nike

本质上,我正在驱动程序类中读取文本文件,但我正在调用另一个类的方法,并包含从另一个类的文本文件中读取的指令,但它不起作用。这是我迄今为止在驱动程序类中所拥有的内容:

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

public class CourseGrade {
static int numberOfStudents;
private int numberOfAssignAndLabs;
private int numberOfTestsAndQuiz;
private static int patZyanteAttend = 3;
static int patZyanteAttendScore = 0;

public void setNumberOfAssignAndLabs() {
numberOfAssignAndLabs = 21;
}

public int getNumberOfAssignAndLabs() {
return numberOfAssignAndLabs;
}

public void setNumberOfTestsAndQuiz() {
numberOfTestsAndQuiz = 4;
}

public int getNumberOfTestsAndQuiz() {
return numberOfTestsAndQuiz;
}

public static void main(String[] args) {
Student myStudent = new Student();

Scanner scanner = new Scanner(System.in);
try {
scanner = new Scanner(new File("grades.txt"));
} catch (FileNotFoundException e) {
System.out
.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
System.exit(0);
}

System.out.println("Name" + "\t" + "\t" + "Assignment Score" + "\t"
+ "Test Score");
numberOfStudents = scanner.nextInt();

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

myStudent.setFirstName();
System.out.print(myStudent.getFirstName() + " ");

myStudent.setLastName();
System.out.print(myStudent.getLastName());

myStudent.setHomeworkScore();
System.out.print(myStudent.getHomeworkScore() + " ");

myStudent.setTestScore();
System.out.print(myStudent.getTestScore());

myStudent.computeGrade();
System.out.println(myStudent.getGrade());

for (int l = 0; l < patZyanteAttend; l++) {
int temp;
temp = scanner.nextInt();
patZyanteAttendScore += temp;
}
}

}
}

这是我要调用的类:

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

public class Student {
private String firstName;
private String lastName;
private String letterGrade;
private int homeworkScore;
private int testScore;

CourseGrade myCourseGrade = new CourseGrade();

Scanner scanner = new Scanner(System.in);

public Student() {
homeworkScore = 0;
testScore = 0;

}

public void setFirstName() {
firstName = scanner.next();

}

public String getFirstName() {
return firstName;
}

public void setLastName() {

lastName = scanner.next();
}

public String getLastName() {
return lastName;
}

public void setHomeworkScore() {
int temp;
for (int j = 0; j < myCourseGrade.getNumberOfAssignAndLabs(); j++) {
temp = scanner.nextInt();
homeworkScore += temp;
}
}

public int getHomeworkScore() {
return homeworkScore;
}

public void setTestScore() {
int temp;
for (int k = 0; k < myCourseGrade.getNumberOfTestsAndQuiz(); k++) {
temp = scanner.nextInt();
testScore += temp;
}
}

public int getTestScore() {
return testScore;
}

public void computeGrade() {

if (homeworkScore <= 599 || testScore <= 149 || homeworkScore <= 719
&& testScore <= 179 || homeworkScore <= 779 && testScore <= 164
|| homeworkScore <= 659 && testScore <= 209) {
letterGrade = "P";
}

if (homeworkScore >= 1140 && testScore >= 180 || homeworkScore >= 1080
&& testScore >= 195 || homeworkScore >= 960 && testScore >= 210
|| homeworkScore >= 900 && testScore >= 225
|| homeworkScore >= 840 && testScore >= 240
|| homeworkScore >= 780 && testScore >= 255
|| homeworkScore >= 720 && testScore >= 285) {
letterGrade = "G";
} else {
letterGrade = "A";
}

}

public String getGrade() {
return letterGrade;
}

}

无论出于何种原因,当我进入 for 循环时程序就会终止

最佳答案

你的 setter 应该接受一个参数,这样它就知道要设置什么值

例如,在学生中:

public void setFirstName(String fName) {
firstName = fName;
}

根据 fName 方法参数的值设置 Student 类中的字段 firstname

当您从 main 调用该方法时,请包含您正在读取的文件中的名字:

myStudent.setFirstName(scanner.next());

您的学生类(class)中不需要扫描仪。

更改所有 setter 以获取参数,并使用 main 中的 Scanner 来提供值。

关于java - Getter 和 Setter 方法以及读取 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686444/

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