java - 使用文件输入查找两个最高分数

标签 java

由于我们还没有讨论数组和列表,他说要坚持使用循环和 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"));
    Scanner duplicateScanner = new Scanner(new File("Lab06.txt"));
    int totalstudents = 0;
    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 (lab06txt.hasNext()){
        record ++;
        totalstudents++;
        lastName = lab06txt.next();
        firstName = lab06txt.next();
        grade = lab06txt.nextInt();
        {
            average += grade;
            if (grade >= Highest){
                Highest = grade;
                firstNameHigh = firstName;
                lastNameHigh = lastName;
            }                        

        }

        {
        if ((grade >= 90) && (grade <= 100))
        {
           ACounter++;         
        }
        if ((grade >= 80) && (grade <= 89))
        {
           BCounter++;         
        }
        if ((grade >= 70) && (grade <= 79))
        {
           CCounter++;         
        }
        if ((grade >= 60) && (grade <= 69))
        {
           DCounter++;         
        }
        if ((grade < 60))
        {
           FCounter++;         
        }
        if ((grade < 0) || (grade > 100))
        {
            System.out.print("Score is out of bounds in record " + record + ": " + lastName + " "+ firstName + " " + grade + ".\nProgram ending\n");
            return;

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


    System.out.println("Total students in class:        " +totalstudents);
    System.out.println("Class average:                  " + average/totalstudents);
    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);
    System.out.println("");
    System.out.println("Top Two Students: \n");

    System.out.printf(lastNameHigh + " " + firstNameHigh + "%15d \n", Highest);
    System.out.printf(lastNameHigh2 + " " + firstNameHigh2 + "%15d\n", Highest2);


}

}

最佳答案

代码的两部分:

firstName = firstNameHigh;
lastName = lastNameHigh;

应该是:

firstNameHigh  = firstName;
lastNameHigh = lastName;

关于java - 使用文件输入查找两个最高分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19442353/

相关文章:

java - 如何使用 jar 文件中的 Liquibase 变更日志

java - 选项卡不占据屏幕的整个宽度

java - Vertx多线程verticle实例和工作池大小

并行类层次结构上下文中的 Java 泛型问题

java - 将数据加载到引导表中

java - 如何从 Java 中的动态源创建对象流?

java - 无法弄清楚如何使用 isAnimationFinished() 方法(LibGDX)

java - 限制Java中类的访问

java - Spring 包含在同一表单中的多个 jsp 在 Ajax 回调上给出错误

java - Runtime.exec 无法在 JDK 7u25 上运行