2013-07-20 3 views
0

이 코드를 실행할 때마다 학생 ID를 묻는 곳으로 이동하고 학생 ID 부분과 숙제 부분을 인쇄합니다. 왜? 나는 이름, 숙제, 실험실, 시험, 토론 및 프로젝트를위한 문자열을 얻으려고 노력하고 있습니다. 그런 다음 숙제, 실험실 및 시험 문자열을 배열로 분할 한 다음 해당 배열을 복식으로 파싱합니다. 내가 그들을 파싱 한 후에 나는 다른 방법으로 그것들을 합산하고 합계를 얻기 위해 프로젝트와 토론으로 합계를 더한다.각 입력에 대해 여러 개의 인쇄. 왜?

import java.util.Scanner; 
import java.io.*; 

public class GradeApplication_Kurth { 
public static void main(String[] args) throws IOException 
{ 
    Student_Kurth one; 
    int choice; 

    boolean test = true; 

    do 
    { 
     Scanner keyboard = new Scanner(System.in); 
     PrintWriter outputFile = new PrintWriter("gradeReport.txt"); 

     System.out.println("Please select an option: \n1. Single Student Grading \n2. Class Grades \n3. Exit"); 
     choice = keyboard.nextInt(); 

     switch (choice) 
     { 
     case 1 : 
      System.out.println("Please enter your Student name: "); 
      String name = keyboard.next(); 
      System.out.println("Please enter you Student ID: "); 
      String id = keyboard.nextLine(); 
      System.out.println("Please enter the 10 homework grades seperated by a space: "); 
      String homework = keyboard.next(); 
      System.out.println("Please enter the 6 lab grades seperated by a space: "); 
      String lab = keyboard.nextLine(); 
      System.out.println("Please enter the 3 exam grades seperated by a space: "); 
      String exam = keyboard.nextLine(); 
      System.out.println("Please enter the discussion grade: "); 
      double discussion = keyboard.nextDouble(); 
      System.out.println("Please enter the project grade: "); 
      double project = keyboard.nextDouble(); 

      one = new Student_Kurth(name, id, homework, lab, exam, discussion, project); 

      outputFile.println(one.toFile()); 
      System.out.println(one); 
      break; 
     case 2 : 
      File myFile = new File("gradeReport.txt"); 
      Scanner inputFile = new Scanner(myFile); 
      while(inputFile.hasNext()) 
      { 
       String str = inputFile.nextLine(); 
       System.out.println("\n" + str); 
      } 
      break; 
     case 3 : 
      test = false; 
      keyboard.close(); 
      outputFile.close(); 
      System.exit(0); 
     } 
    } while (test = true); 
} 
} 

스위치의 끝에서 두 번째 클래스

public class Student_Kurth 
{ 
public String homework; 
public String name; 
public String id; 
public String lab; 
public String exam; 
public double project; 
public double discussion; 
public double[] hw = new double[10]; 
public double[] lb = new double[6]; 
public double[] ex = new double[3]; 
public final double MAX = 680; 
public double percentage; 
public String letterGrade; 

public Student_Kurth() 
{ 
    homework = null; 
    name = null; 
    id = null; 
    lab = null; 
    exam = null; 
    project = 0; 
    discussion = 0; 
} 

public Student_Kurth(String homework, String name, String id, String lab, String exam, double project, double discussion) 
{ 
    this.homework = homework; 
    this.name = name; 
    this.id = id; 
    this.lab = lab; 
    this.exam = exam; 
    this.project = project; 
    this.discussion = discussion; 
} 

public void Homework(String homework) 
{ 
    String delims = " "; 
    String[] tokens = this.homework.split(delims); 
    int tokenCount = tokens.length; 
    for(int i = 0; i < tokenCount; i++) 
    { 
     hw[i] = Double.parseDouble(tokens[i]); 
    } 
} 

public void Lab(String lab) 
{ 
    String delims = " "; 
    String[] tokens = this.lab.split(delims); 
    int tokenCount = tokens.length; 
    for(int i = 0; i < tokenCount; i++) 
    { 
     lb[i] = Double.parseDouble(tokens[i]); 
    } 
} 

public void Exam(String exam) 
{ 
    String delims = " "; 
    String[] tokens = this.exam.split(delims); 
    int tokenCount = tokens.length; 
    for(int i = 0; i < tokenCount; i++) 
    { 
     ex[i] = Double.parseDouble(tokens[i]); 
    } 
} 

public double getHomeworkTotal(double[] hw) 
{ 
    double hwTotal = 0; 
    for(int i = 0; i < hw.length; i++) 
    { 
     hwTotal += hw[i]; 
    } 
    return hwTotal; 
} 

public double getLabTotal(double[] lb) 
{ 
    double lbTotal = 0; 
    for(int i = 0; i < lb.length; i++) 
    { 
     lbTotal += lb[i]; 
    } 
    return lbTotal; 
} 

public double getExamTotal(double[] ex) 
{ 
    double exTotal = 0; 
    for(int i = 0; i < ex.length; i++) 
    { 
     exTotal += ex[i]; 
    } 
    return exTotal; 
} 

public double getTotalScores(double getExamTotal, double getLabTotal, double getHomeworkTotal) 
{ 
    return getExamTotal + getLabTotal + getHomeworkTotal + this.project + this.discussion; 
} 

public double getPercentage(double getTotalScores) 
{ 
    return 100 * getTotalScores/MAX; 
} 

public String getLetterGrade(double getPercentage) 
{ 
    if(getPercentage > 60) 
    { 
     if(getPercentage > 70) 
     { 
      if(getPercentage > 80) 
      { 
       if(getPercentage > 90) 
       { 
        return "A"; 
       } 
       else 
       { 
        return "B"; 
       } 
      } 
      else 
      { 
       return "C"; 
      } 
     } 
     else 
     { 
      return "D"; 
     } 
    } 
    else 
    { 
     return "F"; 
    } 
} 

public void getLetter(String getLetterGrade) 
{ 
    letterGrade = getLetterGrade; 
} 

public void getPercent(double getPercentage) 
{ 
    percentage = getPercentage; 
} 
public String toFile() 
{ 
    String str; 
    str = " " + name + " - " + id + " - " + percentage + " - " + letterGrade; 
    return str; 
} 

public String toString() 
{ 
    String str; 
    str = "Student name: " + name + "\nStudent ID: " + id + "\nTotal Score: " + getTotalScores(getExamTotal(ex), getLabTotal(lb), getHomeworkTotal(hw)) + 
      "\nMax Scores: " + MAX + "Percentage: " + percentage + "Grade: " + letterGrade; 
    return str; 
} 

}

답변

2

, 당신은 당신은 아마

while (test == true) 
에 그 변경하려면

while (test = true) 

이 또한

, 루프에서이 줄을 :

Scanner keyboard = new Scanner(System.in); 
PrintWriter outputFile = new PrintWriter("gradeReport.txt"); 
+3

'while (test)'은 훨씬 명확 해지지 않습니까? 간결? 'while (test == true)'는 여분의 값이나 명확성을 위해 여분의 텍스트를 추가합니다. –

+1

@HovercraftFullOfEels 아마,하지만 실수로 할당 한 내용과 비교하기 위해 그것을 썼다. 나는 개인적으로'while (test)'를 사용한다. – Ermir

1

Ermir의 대답 또한이 라인은 모든 성적을 캡처하지 않습니다 :

System.out.println("Please enter the 10 homework grades seperated by a space: "); 
String homework = keyboard.next(); 

Keyboard.next 만까지 읽고 다음 구분 기호 토큰이므로 공백으로 구분 된 10 개의 성적을 캡처하려면 다음과 같이 전체 행을 캡처해야합니다.

System.out.println("Please enter the 10 homework grades separated by a space: "); 
String homework = keyboard.nextLine(); 
+0

숙제, 시험 및 실험실을 keyboard.nextLine으로 바꿨습니다. 이제 이름을 출력하고 입력을 받아 들인 다음 ID, 숙제 및 실험실이 인쇄되어 1 개의 입력을 요청합니다. – Fireurza

+0

이름을 nextLine()으로 변경하십시오. – MilesHampson

+0

'next()'를 사용하고 이름 뒤에'enter'를 입력하면'next'가 이름을 취하고 nextLine은 StudentId의 행에 아무것도 남지 않기 때문에이 문제가 해결됩니다. – MilesHampson

관련 문제