【问题标题】:Cannot make a proper table and GPA calculation does not return correct grade无法制作正确的表格,GPA 计算不会返回正确的成绩
【发布时间】:2019-06-21 16:46:16
【问题描述】:

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

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

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

  2. 如何制作一个正确的二维数组表,第一行作为标题?我计划有 5 列:Class、Description、Units、Grade 和 Gradepoint。然后在第一行之后,我会让用户在其中输入。使用我当前的代码,程序返回一些非常...时髦的东​​西。 :(

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

感谢你们的帮助!

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。

【问题讨论】:

    标签: java arrays gpa


    【解决方案1】:

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

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

    我仍然不确定如何做一个正确的二维数组表。 :(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多