无法制作正确的表格,GPA 计算不会返回正确的成绩

  • 本文关键字:计算 返回 表格 GPA java arrays gpa
  • 更新时间 :
  • 英文 :


我正在尝试制作一个程序来返回与用户输入(A,A-,B +等)等效的成绩,然后为用户生成成绩报告。

但是,我遇到了2个问题:

  1. 我的程序不返回任何成绩,除了"无效成绩"。我是否输入 A、B、C、B+ 或任何东西都没有关系。

  2. 如何制作一个以第一行作为标题的正确二维数组表?我计划有 5 列:类、描述、单位、成绩和绩点。然后在第一行之后,我会让用户在其中输入。使用我当前的代码,程序返回一些非常...时髦。:(

我在下面粘贴了我的代码。

谢谢你们的帮助!

import java.util.*;
public class Project1 {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
//Input the term
System.out.println("Please enter the term of your grade calculation (for example, Fall 2015): ");
String term = scanner.nextLine();
//Input the number of courses that the student is enrolled in
System.out.println("Please enter the number of courses that you are enrolled in "+term+": ");
int numberofcourses = scanner.nextInt();
//Declaration
String ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String grade[] = new String[numberofcourses];
int Units[] = new int[numberofcourses];
double gradeValue = 0;
//Arrays for class number, description, units, grade, grade point
//Here, input class number, description, units, and grade
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
grade[i] = scanner.nextLine();

Map<String, Double> gradeToScore = new HashMap<>();
gradeToScore.put("A", 4.00);
gradeToScore.put("A-", 3.67);
gradeToScore.put("B+", 3.33);
gradeToScore.put("B", 3.00);
gradeToScore.put("B-", 2.67);
gradeToScore.put("C+", 2.33);
gradeToScore.put("C", 2.00);
gradeToScore.put("D+", 1.33);
gradeToScore.put("D", 1.00);
gradeToScore.put("F", 0.0);
gradeToScore.put("FX", 0.0);
if(gradeToScore.containsKey(grade)) {
gradeValue = gradeToScore.get(grade); 
}else{
System.out.println("Invalid Grade");
}
}
//Print out the report
//Print out the heading
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
//Print out the table
int columns = 5;
int rows = numberofcourses;
String[][] table = new String[rows][columns];
String chain = "";
{
for (int i = 0; i < table.length; i++)
{
for (int c = 0; c < table[0].length; c++)
{
chain += "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints";
}
chain += "|n";
chain += "|" + ClassName[i] + Description[i] + Units[i] + grade[i] + gradeValue;
}
System.out.println(chain);
}
}
}

这是我对上述代码的结果:

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2019
Please enter the number of courses that you are enrolled in Fall 2019: 
1
Please enter your #1 class name: 
FIN 301
Please enter your #1 class description: 
Personal Finance
Please enter your #1 class units: 
3
Please enter your #1 class grade: 
A
Invalid Grade
Class Grades - Fall 2019 Term
Office Grades
|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|
|FIN 301Personal Finance3A0.0

注意:

我确实尝试了System.out.format,但结果并没有更好。这是我的代码:

System.out.format("%30s %25s %10s %25s %10s %25 %10 %25 %10 %25", "Class", "|", "Description($)", "|", "Units", "|", "Grade", "|", "Grade Points");

系统格式的试用版 2...肯定没有更好:

System.out.format("%n%n%n%n%n", "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints");
System.out.format("%n%n%n%n%n", ClassName[i], Description[i], Units[i], grade[i], gradepoint);

必须有某种语法,但我必须在某处阅读左对齐、右对齐、居中对齐或我想填充多少空间。请知道,如果我知道语法,我也要切换到System.out.format。

好的,我想出了第一个问题的解决方法...... 这是我的成绩转换块的部分代码:

if (grade[i].equals ("A"))
gradeValue= 4.00;
else if (grade[i].equals("A-"))
gradeValue= 3.67;
else if (grade[i].equals("B+"))
gradeValue = 3.33;
else if (grade[i].equals("B"))
gradeValue = 3.00;
else if (grade[i].equals ("B-"))
gradeValue = 2.67;
else if (grade[i].equals("C+"))
gradeValue = 2.33;
else if (grade[i].equals("C"))
gradeValue = 2.00;
else if (grade[i].equals ("D+"))
gradeValue = 1.33;
else if (grade[i].equals ("D"))
gradeValue = 1.00;
else if (grade[i].equals ("F"))
gradeValue = 0;
else if (grade[i].equals ("FX"))
gradeValue = 0;
else
System.out.println ("Invalid Grade");

不过,我仍然不确定如何制作正确的 2-D 数组表。 :(

最新更新