由于我们还没有涉及数组和列表,他说要坚持循环和 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);
}
}