좋아요. 그래요. 성적을위한 텍스트 파일을 읽고 상위 두 명의 학생을 표시하는 프로그램을 만들었습니다. 내가 일하고있는 성적에 관계가 있다는 것을 보여 주어야합니다. 그러나 텍스트 파일에 관계가 없으면 두 번째로 높은 점수가 중복됩니다. 나는 넥타이가 없다면 넥타이 2 개를 표시하고 넥타이가 두 개일 경우 넥타이가 표시되도록 작동하지 않을 수 있습니다. 정말 도움이 필요해. 나는 이것을하기 위해 애쓰는 동안 고심하고 있었고, 오늘 밤에 자러 가기 전에 이것을 제출해야만합니다. 제발 누군가이 코드를 수정하도록 도와주세요. 정말 큰 점수이고, 알아 내지 못합니다.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")); //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
만약 학년을위한 것이고 그것을 이해할 수 없다면, 당신은 그것을 알아 내기 위해 등급을 매겨서는 안됩니다. – tyler
^하지만 그것을 알아 내고 싶습니다. 무엇이 잘못되었는지 알고 싶습니다. 하루 종일 저를 미치게했습니다.학년이 아니더라도, 이것은 나를 귀찮게 할 것이고, 나는 결과를 얻지 않고 이미 고칠 시간을 보냈습니다. 그래서 내가 여기 왔어. – DeathWish
거기에 정적 블록이 무엇입니까? 학년을 정한 후에? – Makoto