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

时间:2019-06-21 16:46:16

标签: 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");

System.out.format的试用版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。

1 个答案:

答案 0 :(得分:0)

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

    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");

尽管如此,我仍然不确定如何做一个合适的二维数组表。 :(