[英]how can i get the total number of similar grade letters in my student grade calculator program
My program calculates the grades of 30 students with six different scores then displays the letter grades for each student. 我的程序计算30位学生的成绩(六个不同的分数),然后显示每位学生的字母成绩。 I have done this already but my problem is how to count the number of A's, B's,C's,D's and F's.
我已经这样做了,但是我的问题是如何计算A,B,C,D和F的数量。
I want my output to be like this: 我希望我的输出是这样的:
The number of A's is: A的数目为:
The number of B's is: B的数目为:
The number of C's is: C的数目为:
The number of D's is: D的数目为:
The number of F's is: F的数量为:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
public class FinalGrade extends JPanel
{
public static String firstName[]= new String[30];
public static String lastName[] = new String[30];
public static String grade[]=new String[30];
public static int HW1;
public static int HW2;
public static int HW3;
public static int Project;
public static int Midterm;
public static int Final;
public static double Avg_homework;
public static double Avg_exam;
public static double Final_numeric_grade;
public FinalGrade()
{
super(new GridLayout(1,0));
String[] columnNames = {"First Name", "Last Name", "Final Grade"};
Object[][] data = {
{firstName[0], lastName[0], grade[0]},
{firstName[1], lastName[1], grade[1]},
{firstName[2], lastName[2], grade[2]},
{firstName[3], lastName[3], grade[3]},
{firstName[4], lastName[4], grade[4]},
{firstName[5], lastName[5], grade[5]},
{firstName[6], lastName[6], grade[6]},
{firstName[7], lastName[7], grade[7]},
{firstName[8], lastName[8], grade[8]},
{firstName[9], lastName[9], grade[9]},
{firstName[10], lastName[10], grade[10]},
{firstName[11], lastName[11], grade[11]},
{firstName[12], lastName[12], grade[12]},
{firstName[13], lastName[13], grade[13]},
{firstName[14], lastName[14], grade[14]},
{firstName[15], lastName[15], grade[15]},
{firstName[16], lastName[16], grade[16]},
{firstName[17], lastName[17], grade[17]},
{firstName[18], lastName[18], grade[18]},
{firstName[19], lastName[19], grade[19]},
{firstName[20], lastName[20], grade[20]},
{firstName[21], lastName[21], grade[21]},
{firstName[22], lastName[22], grade[22]},
{firstName[23], lastName[23], grade[23]},
{firstName[24], lastName[24], grade[24]},
{firstName[25], lastName[25], grade[25]},
{firstName[26], lastName[26], grade[26]},
{firstName[27], lastName[27], grade[27]},
{firstName[28], lastName[28], grade[28]},
{firstName[29], lastName[29], grade[29]}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(300,400));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Grade Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
FinalGrade newContentPane = new FinalGrade();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws FileNotFoundException
{
Scanner input = new Scanner (new File ("student_grades_input.txt"));
int count = 0;
while (input.hasNext())
{
firstName[count] = input.next();
lastName[count] = input.next();
HW1 = input.nextInt();
HW2 = input.nextInt();
HW3 = input. nextInt();
Project = input.nextInt();
Midterm = input.nextInt();
Final = input.nextInt();
Avg_homework = (HW1 + HW2 + HW3)/3;
Avg_exam = (Midterm + Final)/2;
Final_numeric_grade = 0.45 * Avg_homework + 0.25 * Project + 0.30 * Avg_exam ;
if (Final_numeric_grade > 89)
grade[count] = "A";
else if (Final_numeric_grade > 79 && Final_numeric_grade < 90)
grade[count] = "B";
else if (Final_numeric_grade > 69 && Final_numeric_grade < 80)
grade[count] = "C";
else if (Final_numeric_grade > 59 && Final_numeric_grade < 70)
grade[count] = "D";
else
grade[count] = "F";
count++;
}
input.close();
createAndShowGUI();
}
}
You can use an array to store the counts of the grades where the first index would be "A", the second index would be "B". 您可以使用数组存储成绩的计数,其中第一个索引为“ A”,第二个索引为“ B”。 etc. You can even do this when you set your
grade
array. 等等。您甚至可以在设置
grade
数组时执行此操作。 Something like this should do the trick: 这样的事情应该可以解决问题:
int[] gradeCount = new int[5]; //Define before your while (input.hasNext())
...
if (Final_numeric_grade > 89){
grade[count] = "A";
gradeCount[0]++;}
else if (Final_numeric_grade > 79 && Final_numeric_grade < 90){
grade[count] = "B";
gradeCount[1]++;}
else if (Final_numeric_grade > 69 && Final_numeric_grade < 80){
grade[count] = "C";
gradeCount[2]++;}
else if (Final_numeric_grade > 59 && Final_numeric_grade < 70){
grade[count] = "D";
gradeCount[3]++;}
else{
grade[count] = "F";
gradeCount[4]++;}
Then when you need to access the grade counts do something like this: 然后,当您需要访问成绩计数时,请执行以下操作:
System.out.println("Number of A: " + gradeCount[0]);
System.out.println("Number of B: " + gradeCount[1]);
System.out.println("Number of C: " + gradeCount[2]);
System.out.println("Number of D: " + gradeCount[3]);
System.out.println("Number of F: " + gradeCount[4]);
All you have to do is iterate over grade
array after you have set all your grades and have 6 counters (1 for each letter): 您要做的就是在设置完所有成绩并拥有6个计数器(每个字母1个)之后遍历
grade
数组:
int numberOfA = 0;
int numberOfB = 0;
int numberOfC = 0;
int numberOfD = 0;
int numberOfE = 0;
int numberOfF = 0;
for (int i = 0; i < grade.length; i++) {
if (grade[i].equals("A")) {
numberOfA++;
} else if (grade[i].equals("B")) {
numberOfB++;
} else if (grade[i].equals("C")) {
numberOfC++;
} else if (grade[i].equals("D")) {
numberOfD++;
} else if (grade[i].equals("E")) {
numberOfE++;
} else {
numberOfF++;
}
}
Then simply print or do whatever you need to do with the counters: 然后只需打印或执行您需要对计数器做的任何事情:
System.out.println("Number of A: " + numberOfA);
System.out.println("Number of B: " + numberOfB);
System.out.println("Number of C: " + numberOfC);
System.out.println("Number of D: " + numberOfD);
System.out.println("Number of E: " + numberOfE);
System.out.println("Number of F: " + numberOfF);
Also please follow Java naming conventions 还请遵循Java命名约定
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.
除变量外,所有实例,类和类常量均以小写首字母混合使用。 Internal words start with capital letters.
内部单词以大写字母开头。 Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
变量名称不应以下划线_或美元符号$字符开头,即使两者都允许。
From the above: Variable names should start with lower case while classes names should start with an upper case
从上
Variable names should start with lower case while classes names should start with an upper case
: Variable names should start with lower case while classes names should start with an upper case
Also as @Gonzo stated you can use the counters on the code you already have, this will improve performance because you won't be iterating twice on the same array, you can do it as: 就像@Gonzo 所说的那样,您可以在已有的代码上使用计数器,这将提高性能,因为您不会在同一数组上进行两次迭代,您可以这样做:
if (Final_numeric_grade > 89) { //Be sure to add curly brackets
grade[count] = "A";
numberOfA++;
} //And close them
else if (Final_numeric_grade > 79 && Final_numeric_grade < 90) {
grade[count] = "B";
numberOfB++;
}
//And so on ...
And print them as I said above. 并按我上面所述打印它们。 Just be sure to initialize your variables before incrementing them :)
只需确保在增加变量之前初始化变量即可:)
There can be so many ways to do that. 有很多方法可以做到这一点。 One simple way can be defining counter variables for each grade and increment them in your while loop, when you are determining the grade.
一种简单的方法是为每个成绩定义计数器变量,并在确定成绩时在while循环中增加它们。 Something like :
就像是 :
int gradeACounter=0, gradeBCounter=0;
if (Final_numeric_grade > 89){
grade[count] = "A";
gradeACounter++;
}
else if (Final_numeric_grade > 79 && Final_numeric_grade < 90){
grade[count] = "B";
gradeBCounter++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.