簡體   English   中英

使用文件輸入查找兩個最高分

[英]Finding the two highest scores using file input

由於我們還沒有介紹數組和列表,因此他說要堅持循環和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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM