2013-10-23 4 views
-4

좋아요. 그래요. 성적을위한 텍스트 파일을 읽고 상위 두 명의 학생을 표시하는 프로그램을 만들었습니다. 내가 일하고있는 성적에 관계가 있다는 것을 보여 주어야합니다. 그러나 텍스트 파일에 관계가 없으면 두 번째로 높은 점수가 중복됩니다. 나는 넥타이가 없다면 넥타이 2 개를 표시하고 넥타이가 두 개일 경우 넥타이가 표시되도록 작동하지 않을 수 있습니다. 정말 도움이 필요해. 나는 이것을하기 위해 애쓰는 동안 고심하고 있었고, 오늘 밤에 자러 가기 전에 이것을 제출해야만합니다. 제발 누군가이 코드를 수정하도록 도와주세요. 정말 큰 점수이고, 알아 내지 못합니다.if 프로젝트의 문과 루프

package lab06; 

import java.util.*; 
import java.io.*; 
public class Lab06 { 


public static void main(String[] args) throws Exception { 
    Scanner lab06txt = new Scanner(new File("Lab06.txt")); //declare scanner for lab06.txt   
    //declare variables 
    int grade = 0; 
    int grade2 = 0; 
    int record = 0;  
    int Highest = 0; 
    int Highest2 = 0; 
    int ACounter = 0; 
    int BCounter = 0; 
    int CCounter = 0; 
    int DCounter = 0; 
    int FCounter = 0; 
    double average = 0; 
    String lastName = ""; 
    String lastNameHigh = ""; 
    String lastNameHigh2 = ""; 
    String firstName = ""; 
    String firstNameHigh = ""; 
    String firstNameHigh2 = ""; 

    //while loop for lab06txt 
    while (lab06txt.hasNext()){ 
     record ++; //keep track of number of records 
     lastName = lab06txt.next(); //next string is placed into lastName 
     firstName = lab06txt.next(); //next string is placed into firstName 
     grade = lab06txt.nextInt(); //next integer is placed into grade 
     { 
      average += grade; //accumulate variable grade into variable average 
      if (grade >= Highest) //if statement for determining highest grade 
      { 
       Highest = grade; 
       firstNameHigh = firstName; //places firstName of highest grade record into firstNameHigh 
       lastNameHigh = lastName; //places lastName of highest grade record into lastNameHigh 
      }          
     } 

     { 
     if ((grade >= 90) && (grade <= 100)) //if statements for variable grade 
     { 
      ACounter++;  //increases Acounter by 1 
     } 
     if ((grade >= 80) && (grade <= 89)) 
     { 
      BCounter++;   //increases Bcounter by 1 
     } 
     if ((grade >= 70) && (grade <= 79)) 
     { 
      CCounter++;   //increases Ccounter by 1 
     } 
     if ((grade >= 60) && (grade <= 69)) 
     { 
      DCounter++;   //increases Dcounter by 1 
     } 
     if ((grade < 60)) 
     { 
      FCounter++;   //increases Fcounter by 1 
     } 
     if ((grade < 0) || (grade > 100)) 
     { 
      //if an incorrect error is found in the loop display the faulty record and end the program 
      System.out.print("Score is out of bounds in record " + record + ": " + lastName + " "+ firstName + " " + grade + ".\nProgram ending\n"); 
      return; 

     } 
     } 
    } 
    Scanner SecondHighest = new Scanner(new File("Lab06.txt")); //new scanner for second highest grade 
    while(SecondHighest.hasNext()) 
    { 
     lastName = SecondHighest.next(); 
     firstName = SecondHighest.next(); 
     grade2 = SecondHighest.nextInt();   
     if(grade2 > Highest2 && grade2 < Highest) //if statement to determine second highest grade 
     { 
       Highest2 = grade2; 
       firstNameHigh2 = firstName; 
       lastNameHigh2 = lastName; 
     } 
     if (Highest == grade2){ 
      Highest2 = grade2; 
      firstNameHigh2 = firstName; 
      lastNameHigh2 = lastName; 
     } 
    } 

    System.out.println("Total students in class:  " +record); //display total students in class 
    System.out.printf("Class average:     %.1f\n", average/record); //display class average 
    System.out.println("Grade Counters: "); 
    System.out.println("A's  B's  C's  D's  F's"); 
    System.out.printf(ACounter + "%7d %7d %8d %7d\n", BCounter,CCounter,DCounter,FCounter); //display number of A's B's C's D's and F's 
    System.out.print("\n"); 
    System.out.println("Top Two Students: \n"); 

    System.out.printf(lastNameHigh + " " + firstNameHigh + "%15d \n", Highest); //display highest score and student 
    System.out.printf(lastNameHigh2 + " " + firstNameHigh2 + "%15d\n", Highest2); //display second highest score and student 


} 

} 텍스트 파일의

내용이 형식은 다음과 :

Jackson Nicholas 96 
James Kevin  67 
Jansen David  92 
Johnson Charlene 69 
Jones Nicole  83 
Klein Harry  97 
+0

만약 학년을위한 것이고 그것을 이해할 수 없다면, 당신은 그것을 알아 내기 위해 등급을 매겨서는 안됩니다. – tyler

+0

^하지만 그것을 알아 내고 싶습니다. 무엇이 잘못되었는지 알고 싶습니다. 하루 종일 저를 미치게했습니다.학년이 아니더라도, 이것은 나를 귀찮게 할 것이고, 나는 결과를 얻지 않고 이미 고칠 시간을 보냈습니다. 그래서 내가 여기 왔어. – DeathWish

+0

거기에 정적 블록이 무엇입니까? 학년을 정한 후에? – Makoto

답변

0

당신이 코드를해야 정리의 양에도 불구을,이 블록은 ... 보였다 이상한 .

while(SecondHighest.hasNext()) 
    { 
     lastName = SecondHighest.next(); 
     firstName = SecondHighest.next(); 
     grade2 = SecondHighest.nextInt();   
     if(grade2 > Highest2 && grade2 < Highest) //if statement to determine second highest grade 
     { 
       Highest2 = grade2; 
       firstNameHigh2 = firstName; 
       lastNameHigh2 = lastName; 
     } 
     if (Highest == grade2){ 
      Highest2 = grade2; 
      firstNameHigh2 = firstName; 
      lastNameHigh2 = lastName; 
     } 
    } 

특히 두 번째 문은 경우 : 그것은 읽는다 "가장 높은 grade2 같으면, 우리는 firstName을 가진 grade2, firstNameHigh2와 highest2 덮어하고,이 lastName과 lastNameHigh2."

왜 필요한가요? 우리는 위의 성명서에서 같은 작업을하고 있습니다. 그것을 삭제하십시오.

는 이제이 남아있어 :

while(SecondHighest.hasNext()) { 
    lastName = SecondHighest.next(); 
    firstName = SecondHighest.next(); 
    grade2 = SecondHighest.nextInt();   
    if(grade2 > Highest2 && grade2 < Highest) { 
     Highest2 = grade2; 
     firstNameHigh2 = firstName; 
     lastNameHigh2 = lastName; 
    } 
} 

일 듯 - 나는 당신의 상위 두 학생으로 잭슨 니콜라스와 클라인 해리를 다시 얻을. 그러나 동등한 점수가있는 경우에 해당합니까?

이 경우를 테스트하기 위해 나는 다른 학생을 목록에 포함 시켰습니다. 점수가 97 인 Naruto Uzumaki. 이렇게하면 최고 2 명이 Klein Harry와 Naruto Uzumaki가 될 것입니다.

나는 다시 달리고 나루토 우즈 마키와 잭슨 니콜라스를 상위 2 명의 학생으로 얻는다. 그럴 수는 없습니다 - 점수가 매겨진 두 명의 학생이 있습니다!

나는 문 경우에 등가 검사를 추가 할 필요가 - grade2이 보다 크거나 Highest2 동일해야하고, grade2는 두 번째로 높은 점수를 결정하기 위해보다 거나 같은 최고를해야한다.

자, 재미있는 부분은 다른 이름을 스쿼시하지 않게하는 것입니다. 저는 이것을 독자에게 연습으로 남겨 둡니다. 그러나 데이터를 배치하는 방법에 관해서는 내가 읽은 이름을 과 같게과 같이 읽거나 읽는 점수가 같을 수는 없습니다 사람. 이제이 "존"라는 두 개의 사용자 부서지기 쉬운 코드를 만드는처럼 그 일을 두 문자열이 동일했다 있는지 확인하는 방법 ... 물론

을, 거기 만 (나는의 약 3 작업하는 경우

'em, 그래서 이것은 하드 코어에서 실패 할 것입니다.) -하지만 어쩌면 대신 성을 사용해야합니다. 그것에 대해 생각하십시오 - 그것은 독자에게 운동입니다.

+0

나는 당신이 말했던 일을했지만 아직 넥타이가 없으면 스캐너가 텍스트 파일을 읽고 두 번째로 높은 등급이 복제 될 것이고 가장 높은 점수를 두 번 표시하게됩니다. 나는 성을위한 두 개의 문자열이 다음을 수행하여 동등한 지 아닌지 확인함으로써이를 변경하려고 시도했다. – DeathWish

+0

'while (SecondHighest.hasNext()) { if (! lastNameHigh.equals (lastNameHigh2)) { tie = false; } lastName = SecondHighest.next(); firstName = SecondHighest.next(); grade2 = SecondHighest.nextInt(); if (grade2> Highest2 && grade2 DeathWish

+0

마코토 덕분에 실제로 알아 냈습니다. "이제는 두 문자열이 동등한지를 볼 수있는 방법이 있다면 ..."정말로 나에게 답을주고 그것에 대해 생각하게 해줘서 고맙습니다. grade2에 대해 if 문을 두 개 만들었습니다. 하나는 Highest2보다 크거나 같고 하나는 단지 Highest2보다 큽니다. 하지만 if 문 안에는'&&! lastName.equalsIgnoreCase (lastNameHigh))' – DeathWish