gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:20:17 25 4
gpt4 key购买 nike

由于我们还没有讨论数组和列表,他说要坚持使用循环和 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com