0

我的代码:学生班...

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

 public class Student {
    private Scanner scanner;

    //initialize data members
    private String firstName;
    private String lastName;
    private int assignmentScore;
    private int labScore;
    private int quizScore;
    private int attendanceScore;
    private int homeworkScore;
    private int midterm1Score;
    private int midterm2Score;
    private int finalExamScore;
    private int zyanteScore;
    private int patScore;
    private int testScore;
    private String letterGrade;

    public void setScanner() {
        try { //start try statement
            scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
        } //end try statement
        catch (FileNotFoundException e) { //start catch statement
            System.out.println("Error opening file. Please make sure t  that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
            System.exit(0); //system exit 
        } //end catch statement
    }

    public Scanner getScanner(){
        return scanner;
    }

    public void setfirstName(){
        if(scanner.hasNext()){
            firstName = scanner.next();
        }
    }

    public String getFirstName(){
        return firstName;
    }

    public void setLastName(){
        if(scanner.hasNext()){
            lastName = scanner.next();
        }   
    }

    public String getLastName(){
        return lastName;
    }

    public void setAssignmentScore(){
        assignmentScore = 0;
        int i = 0;
        int[] assignment = new int[6];

        while(scanner.hasNext() && i <= 5){
            assignment[i] = scanner.nextInt();
            assignmentScore += assignment[i];
            i++;
        } 
    }

    public int getAssignmentScore(){
        return assignmentScore;
    }

    public void setLabScore(){
        labScore = 0;
        int i = 0;
        int[] lab = new int[15];

        while(scanner.hasNext() && i <=14){
            lab[i] = scanner.nextInt();
            labScore += lab[i];
            i++;
        }
    }

    public int getLabScore(){
        return labScore;
    }

    public void setQuizScore(){
        if(scanner.hasNext()){
            quizScore = scanner.nextInt();
        }
    }

    public int getQuizScore(){
        return quizScore;
    }

    public void setAttendanceScore(){
        if(scanner.hasNext()){
            attendanceScore = scanner.nextInt();
        }
    }

    public int getAttendanceScore(){
        return attendanceScore;
    }

    public int computeTotalHomeworkScore(){
        homeworkScore = (assignmentScore + labScore + attendanceScore);
        return homeworkScore;
    }

    public void setMidterm1Score(){
        if(scanner.hasNext()){
            midterm1Score = scanner.nextInt();
        }
    }

    public int getMidterm1Score(){
        return midterm1Score;
    }

    public void setMidterm2Score(){
        if(scanner.hasNext()){
            midterm2Score = scanner.nextInt();
        }
    }

    public int getMidterm2Score(){
        return midterm2Score;
    }

    public void setFinalExamScore(){
        if(scanner.hasNext()){
            finalExamScore = scanner.nextInt();
        }
    }

    public int getFinalExamScore(){
        return finalExamScore;
    }

    public void setZyanteScore(){
        if(scanner.hasNext()){
            zyanteScore = scanner.nextInt();
        }
    }

    public int getZyanteScore(){
        return zyanteScore;
    }

    public void setPATScore(){
        if(scanner.hasNext()){
            patScore = scanner.nextInt();
        }
    }

    public int getPATScore(){
        return patScore;
    }

    public int computeTotalTestScore(){
        testScore = (midterm1Score + midterm2Score + finalExamScore + quizScore + zyanteScore + patScore);
        return testScore;
    }

    public void computeLetterGrade(){
        if ((testScore <165 && homeworkScore <780) || (testScore <180 && homeworkScore <720) || (testScore <195 && homeworkScore <660) || (testScore <210 && homeworkScore <660) || (homeworkScore <600)){ //if statement that executes if scores earned a failing grade
            letterGrade = "P" ; //sets letterGrade string to P
        } //end if statement

        if (((testScore >= 150 && testScore <= 164) && (homeworkScore >= 780)) || ((testScore >= 165 && testScore <= 179) && (homeworkScore >=720)) || ((testScore >=180 && testScore <=194) && (homeworkScore >= 660 && homeworkScore <= 1139)) || 
            ((testScore >= 195 && testScore <= 209) && (homeworkScore >= 660 && homeworkScore <= 1079)) || ((testScore >= 210 && testScore <= 224) && (homeworkScore>= 600 && homeworkScore <= 959)) || ((testScore >= 225 && testScore <= 239) && (homeworkScore >= 600 && homeworkScore <= 899)) ||
            ((testScore >= 240 && testScore <= 254) && (homeworkScore >= 600 && homeworkScore <= 839)) || ((testScore >= 255 && testScore <= 269) && (homeworkScore >= 600 && homeworkScore <= 779)) || ((testScore >= 270 && testScore <= 284) && (homeworkScore >= 600 && homeworkScore <= 779)) ||
            ((testScore >= 285 && testScore <= 300) && (homeworkScore >= 600 && homeworkScore <= 719))) { //if statement that executes if scores earned an average grade
            letterGrade = "A" ; //sets letterGrade string to A
        } //end if statement

        if (((testScore >= 180 && testScore <= 194) && (homeworkScore >=  1140 && homeworkScore <= 1200)) || ((testScore >= 195 && testScore <= 209) && (homeworkScore >= 1080 && homeworkScore <= 1200)) ||
            ((testScore >= 210 && testScore <= 224) && (homeworkScore >= 960 && homeworkScore <= 1200)) || ((testScore >= 225 && testScore <= 239) && (homeworkScore >= 900 && homeworkScore <= 1200)) ||
            ((testScore >= 240 && testScore <= 254) && (homeworkScore >= 840 && homeworkScore <= 1200)) || ((testScore >= 255 && testScore <= 269) && (homeworkScore >= 780 && homeworkScore <= 1200)) ||
            ((testScore >= 270 && testScore <= 284) && (homeworkScore >= 780 && homeworkScore <= 1200)) || ((testScore >= 285 && testScore <= 300) && (homeworkScore >= 720 && homeworkScore <= 1200))) { //if statement that executes if scores earned a good grade
            letterGrade = "G" ; //sets letterGrade string to G
        } //end if statement
    }

    public String getLetterGrade(){
        return letterGrade;
    }

    public void testing() {
        for (int i = 0; i < 7; i++) {
            if (scanner.hasNext()) {
                String test = scanner.next();
            }
        }
    }
 }      

CourseOffer课程...

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

 public class CourseOffering {
private Scanner scanner;

Student myStudent = new Student();

//initialize data members
private int students;
private String description;
private int allTotalHomeworkScore = 0;
private int allTotalTestScore = 0;
private int totalStudents = 0;
private int numberOfSemesters;
private int studentArrayCount = 0;
private double homeworkAverage;
private double testAverage;

public CourseOffering(Scanner inScanner){
    scanner = inScanner;
}

public void setNumberOfSemesters(){
    if(scanner.hasNext()){
        numberOfSemesters = scanner.nextInt();
    }
}

public int getNumberOfSemesters(){
    return numberOfSemesters;
}

public void setStudents(){
    int[] student = new int[numberOfSemesters];
    student[studentArrayCount] = scanner.nextInt();
    students = student[studentArrayCount];
    studentArrayCount++;
    totalStudents += students;
}

public double computeHomeworkAverage(){
    allTotalHomeworkScore += myStudent.computeTotalHomeworkScore();
    homeworkAverage = (allTotalHomeworkScore / totalStudents);
    return homeworkAverage;
}

public double computeTestAverage(){
    allTotalTestScore += myStudent.computeTotalTestScore();
    testAverage = (allTotalTestScore / totalStudents);
    return testAverage;
}

public void countPassingStudents(){

}
 }

CourseStatistics 类(调用其他类)...

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

 public class CourseStatistics {

public static void main(String[] args) {

    Scanner scanner = null;

    try { //start try statement
        scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
    } //end try statement
    catch (FileNotFoundException e) { //start catch statement
        System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
        System.exit(0); //system exit 
    } //end catch statement

    Student myStudent = new Student();
    CourseOffering myCourse = new CourseOffering(scanner);
    myStudent.setScanner();
    myStudent.getScanner();
    myStudent.testing();
    myCourse.setNumberOfSemesters();
    for(int i = 0; i < 4; i++){
    myStudent.setfirstName();
    myStudent.setLastName();
    myStudent.setAssignmentScore();
    myStudent.setLabScore();
    myStudent.setMidterm1Score();
    myStudent.setMidterm2Score();
    myStudent.setFinalExamScore();
    myStudent.setQuizScore();
    myStudent.setAttendanceScore();
    myStudent.setPATScore();
    myStudent.setZyanteScore();
    myStudent.computeLetterGrade();
    System.out.println(myStudent.getFirstName() + " " + myStudent.getLastName() + " " + myStudent.computeTotalHomeworkScore() + " " + myStudent.computeTotalTestScore() + " " + myStudent.getLetterGrade());
    }
    System.out.println(myCourse.getNumberOfSemesters());
}   
 }

输出是...

Anthony Hopkins 854 284 P
John Smith 730 214 G
Pan Mei 730 267 A
Rafael Vega 801 236 A
3

.txt 文件中的数据对象是 3,即使myCourse.setNumberOfSemesters()不是第一个要扫描的对象,它仍然会扫描到 3 的第一个数据。我如何让两个扫描类混合,这意味着一个继续在哪里另一个离开了。

4

2 回答 2

0

1)你真的应该更具体点什么,在哪里,没有人想研究这么多代码。

2)你应该考虑你的 OOP 方法

OOP 提示 NO.1

如果您有很多对其对象具有相似含义的值,请尝试将它们存储在某个数组/列表中,这样您就不必在出现新值时重写类(如 midter3)。

例如这个:

private int assignmentScore;
private int labScore;
private int quizScore;
private int attendanceScore;
private int homeworkScore;
private int midterm1Score;
private int midterm2Score;
private int finalExamScore;
private int zyanteScore;
private int patScore;
private int testScore;

会更好

private Map<String, Integer> exams = new HashMap<>();

然后你可以这样访问它:

exams.get("midterm1")
于 2013-11-01T16:26:18.833 回答
0

您的代码中有一些逻辑问题。

在您的Student课堂上,您正在打开文件以再次扫描。尝试将setScanner方法更改为:

public void setScanner(Scanner scanner){
    this.scanner = scanner;
}

在你的主要:

myStudent.setScanner(scanner);
于 2013-11-01T16:29:49.007 回答