2013-10-23 83 views
-4

好吧,我已經構建了這個程序,讀取一個文本文件的檔次並顯示前兩名學生。我應該顯示任何等級的關係,我工作。但是,如果文本文件中沒有關係,則會顯示重複的第二高分數。我似乎無法做到這一點,所以如果沒有聯繫,它會顯示兩個最高的成績,並且如果有關係顯示兩個聯繫。我真的需要幫助。我一直在努力嘗試這麼做,我必須在今晚睡覺之前提交。請有人幫我解決這個問題,這是一個非常大的成績,我無法弄清楚。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

這是什麼靜態塊在那裏?你知道,那個分配'grade'後的那個? – 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; 
     } 
    } 

特別是第二個if語句:它讀取, 「如果最高等於grade2,那麼我們覆蓋highest2與grade2,firstNameHigh2與名字,並用lastNameHigh2 lastName的。」

爲什麼我們需要那個?我們在上述聲明中做了同樣的工作。刪除它。

現在你離開了這一點:

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

它似乎工作 - 我回來傑克遜尼古拉斯·克萊因哈利你前兩名的學生。但是當它有相同的分數時它是否涵蓋了這個情況?

爲了測試這個案例,我把另一個學生列入了你的名單:漩渦鳴人97分。這將確保最高的兩個是克萊恩哈利和漩渦鳴人。

我再次運行它,我得到了漩渦鳴人和傑克遜尼古拉斯作爲我的前兩名學生。那不可能 - 有兩名學生成績平平!

我需要一個等價性檢查添加到if語句 - grade2應大於或等於 Highest2,並grade2應小於或等於最高以確定第二最高得分。

現在,有趣的部分是阻止一個名字擠壓另一個名字。我把這個作爲一個練習留給讀者,但是確實以你的數據佈局的方式,我讀的第一個名字不能是相同的作爲我讀入的高分名字,或者它是相同的人。

現在,只要有一個方法,如果兩個字符串是相等的...

當然,做這樣使你的代碼脆名爲「喬恩」兩個用戶(我約三個工作'他們,所以這會失敗硬核) - 但也許它應該是姓氏,而不是。考慮一下 - 這對讀者來說是一個練習。

+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

我真的想通了,謝謝你真誠。 「現在,如果只有一種方法來看看兩個字符串是否相等。」我真的很感謝你給我答案,並讓我思考。我爲2級做了兩個if語句。一個大於或等於Highest2的地方,一個大於或等於Highest2的地方。但是在if語句中我寫了'&&!lastName.equalsIgnoreCase(lastNameHigh))' – DeathWish