-4

好的,所以我已经构建了这个程序,它读取成绩的文本文件并显示前两名学生。我应该在成绩中显示任何关系,这是我工作的。但是,如果文本文件中没有平局,则会显示重复的第二高分。我似乎无法让它工作,所以如果没有平局,它会显示两个最高等级,如果有平局则显示两个平局。我真的需要帮助。我已经努力了好几个小时试图做到这一点,我必须在今晚睡觉前提交这个。请有人帮我修复这个代码,这是一个非常大的成绩,我无法弄清楚。

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
4

1 回答 1

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 语句:它的内容是,“如果 high 等于 Grade2,那么我们用grade2 覆盖highest2,用firstName 覆盖firstNameHigh2,用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。这将确保最高的两个是克莱因哈利和漩涡鸣人。

我再次运行它,我得到了漩涡鸣人和杰克逊尼古拉斯作为我的前两名学生。这不可能——有两个学生的分数并列!

我需要在 if 语句中添加一个等价检查——grade2 应该大于或等于Highest2,grade2 应该小于或等于Highest 以确定第二高的分数。

现在,有趣的部分是阻止一个名字挤压另一个名字。我将此作为练习留给读者,但可以肯定的是,根据您的数据布局方式,我读入的名字不能与我读入的高分名字相同,或者是同一个人

现在,如果只有一种方法可以查看两个字符串是否相等...

当然,这样做会让你的代码对两个名为“Jon”的用户来说很脆弱(我和其中三个用户一起工作,所以这会失败)——但也许它应该是姓氏。想一想——这对读者来说是一个练习。

于 2013-10-23T04:44:33.910 回答