2013-11-01 309 views
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(); 
      } 
     } 
    } 
}  

CourseOffering類...

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.第一個數據我如何獲得兩個掃描類混合,意味着一個繼續離開另一個。

回答

0

你在你的代碼有一些邏輯問題。

在您的Student類中,您打開文件以再次掃描。嘗試改變setScanner方法:

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

而在你的主:

myStudent.setScanner(scanner); 
0

1)你真的應該更具體什麼,在哪裏,沒有人想研究這麼多的代碼。

2)你應該想想你的OOP方法

OOP TIP 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")