Я написал программу, которая принимает оценки за экзамены и присваивает переменные определенным звездным числам и буквенным оценкам. Программа компилируется, но в переменных я считаю 2 проблемы. Я не могу получить наименьший набранный балл и не могу получить наивысший. Кажется, что обе эти переменные определены правильно, поэтому я надеюсь, что кто-то может помочь вам свежим взглядом.
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Welcome to GradeCalculator!\n\n"); // Start of the program
// All variables are defined below
int students = 0;
int exams = 0;
double classav = 0;
int count = 0;
double lowest = 0;
double highest = 0;
String name;
// Intake and output portions of the program for number of students, and number of exams
System.out.print("Please enter the number of students: ");
students = s.nextInt();
System.out.print("Please enter the number of exams: ");
exams = s.nextInt();
System.out.println("\n----------------------------------------");
String[] examScores = new String [exams];
while(count < students) {
int totalExam=0;
int starnum=0;
char letter='a';
String stars= "";
//New Scanner Statement to fix String "name" problems
Scanner n = new Scanner(System.in);
System.out.print("Enter student " + (count+1) +"'s name\t: ");
name = n.nextLine();
n.nextLine();
System.out.print("Enter exam scores\t: ");
examScores = n.nextLine().split(" ");
for(int i = 0; i < exams; i++){
totalExam += Integer.parseInt(examScores[i]);
}
//for loop used to compare exam grades. printing out proper star counts according to letter grades within the if statements
int studentavg = totalExam / exams;
System.out.println(" Average: " + studentavg);
if(studentavg>=90){
letter = 'A';
stars = "****";
starnum = 4;
}//if1
if(studentavg<90 && studentavg>=80){
letter = 'B';
stars = "***";
starnum = 3;
}//if2
if(studentavg<80 && studentavg>=70){
letter = 'C';
stars = "**";
starnum = 2;
}//if3
if(studentavg<70 && studentavg>=60){
letter = 'D';
stars = "*";
starnum = 1;
}//if4
if(studentavg<60){
letter = 'F';
stars = "";
starnum = 0;
}//if5
System.out.println("Grade statistics for " + name);
System.out.println(" Letter Grade: " + letter);
System.out.println(name + " gets " + starnum + " stars! "+ stars);
System.out.println("\n----------------------------------------");
classav += studentavg;
count++;
}// while loop
System.out.println("Class statistics: ");
System.out.println(" Average: "+ (classav / exams));
System.out.println(" Lowest : " + lowest);
System.out.println(" Highest: " + highest);
System.out.println("Done. Good bye!");
}
}
Результаты сейчас
----------------------------------------
Welcome to GradeCalculator!
Please enter the number of students: 2
Please enter the number of exams: 3
----------------------------------------
Enter student 1's name : Rob
Enter exam scores : 100 99 77
Average: 92
Grade statistics for Rob
Letter Grade: A
Rob gets 4 stars! ****
----------------------------------------
Enter student 2's name : Tom
Enter exam scores : 100 99 77
Average: 92
Grade statistics for Tom
Letter Grade: A
Tom gets 4 stars! ****
----------------------------------------
Class statistics:
Average: 30.666666666666668
Lowest : 0.0
Highest: 0.0
Thank you for using Rob Nelson's Grade Calculator! Good bye! :)
BUILD SUCCESSFUL (total time: 56 seconds)
Ожидаемые результаты
----------------------------------------
Welcome to GradeCalculator!
Please enter the number of students: 2
Please enter the number of exams: 3
----------------------------------------
Enter student 1's name : Rob
Enter exam scores : 100 99 77
Average: 92
Grade statistics for Rob
Letter Grade: A
Rob gets 4 stars! ****
----------------------------------------
Enter student 2's name : Tom
Enter exam scores : 100 99 77
Average: 92
Grade statistics for Tom
Letter Grade: A
Tom gets 4 stars! ****
----------------------------------------
Class statistics:
Average: 92
Lowest : 77
Highest: 100
Thank you for using Rob Nelson's Grade Calculator! Good bye! :)
BUILD SUCCESSFUL (total time: 56 seconds)