2013-10-30 45 views
0

本質上,我正在讀取我的驅動程序類中的文本文件,但我從另一個類調用了一個方法,指示從另一個類的文本文件中讀取,但它只是不工作。這是我在我的驅動程序類到目前爲止:Getter和setter方法和讀取txt文件

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循環的程序終止

+0

您是否收到錯誤訊息? – Joe

+0

什麼不工作? – Andrew

+0

它只打印出System.out.println(「Name」+「\ t」+「\ t」+「Assignment Score」+「\ t」 +「Test Score」);,然後什麼也沒有 – user2120893

回答

1

你的制定者應該採取一個參數,所以它知道什麼值來設置

例如,在學生:

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

fName方法參數的值中設置Student類中的字段firstname

當你調用從main的方法,包括從您正在閱讀的文件用的名字:

myStudent.setFirstName(scanner.next()); 

你並不需要在學生類的掃描儀。

更改所有設置器以採用參數,並使用main中的Scanner來提供值。