【发布时间】:2015-11-24 18:18:38
【问题描述】:
我的程序计算 30 名学生的成绩,他们有六种不同的分数,然后显示每个学生的字母成绩。我已经这样做了,但我的问题是如何计算 A、B、C、D 和 F 的数量。
我希望我的输出是这样的:
A的数量是:
B的数量是:
C 的数量是:
D的数量是:
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();
}
}
【问题讨论】:
-
好的。你想计算等级字母的数量吗?你有什么尝试做的?