Java 成绩报告

Grades Report in Java

我正在尝试创建一个为学生生成成绩报告的程序。我想请教以下问题:

1 - 当我 运行 程序时,我遇到了 Exception in thread "main" java.util.InputMismatchException 的问题(详情请见下文)。

2 - 如何将输出格式化为 table?我计划有 5 列:Class、描述、单元、成绩和成绩点。我无法使 table 中的内容与标题(Class、描述、单位等)

对齐
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 a[] = new String[numberofcourses];

        //Arrays for class number, description, units, grade, grades point
        //Here, input class number, description, units, and grades
        for(int i = 0; i < numberofcourses; i++)
        {
            System.out.println("Please enter the #"+(i+1)+" class name: ");
            String ClassName = scanner.nextLine();
            scanner.hasNextLine();
            System.out.println("Please enter the #"+(i+1)+" description: ");
            String Description = scanner.nextLine();
            scanner.hasNextLine();
            System.out.println("Please enter the #"+(i+1)+" units: ");
            int Units = scanner.nextInt();
            scanner.hasNextLine();
            int Grades = scanner.nextInt();
            scanner.hasNextLine();
        }
        System.out.println("Class Grades - "+term+" Term");
        System.out.println("Office Grades");
        System.out.println("Class \t \t Description \t \t Units \t \t Grade \t \t Grade Points");

        for(int i = 0; i < numberofcourses; i++)
        {
        System.out.println(a[i] + "\t \t");
        }
    }
}

输出:

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2016
Please enter the number of courses that you are enrolled in Fall 2016: 
2
Please enter the #1 class name: 
COMPSCI 172
Please enter the #1 description: 
Computer Science
Please enter the #1 units: 
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Project1.main(Project1.java:29)

所以首先你有很多需要修复的地方,包括我即将为你修复的内容。如果您想在最后一起输出所有 类 ,则必须编辑我的代码以将格式化的字符串存储在数组中,然后将它们一起输出。

我通过在您的 int numberofcourses = scanner.nextInt(); 行之后添加 scanner.nextLine(); 来修复您的错误。您需要这样做,因为 scanner.nextInt() 不会消耗行终止符并移动到下一行,所以这意味着您的下一个 nextLine() 会搞砸并尝试读取任何内容。这条额外的行确保 Scanner 将移动到下一行。

继续,您有一大堆使用不当的问题 scanner.hasNextLine();。 returns 一个布尔条件,让您知道下一行是否存在。您目前不使用此布尔条件,只是让它什么也不做。如果你想使用它,你需要在 if 语句或 while 循环中使用它。我删除了所有这些行。

最后使用 String.format() 格式化您的打印​​语句。下面显示了我为输出您的值所做的示例。您可以将其存储到 String 数组中,稍后将它们一起输出。如果您还有其他问题,请告诉我。这是完整的代码(我测试了它 运行 很好):

    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();
    scanner.nextLine();

    //Declaration
    String a[] = new String[numberofcourses];

    //Arrays for class number, description, units, grade, grades point
    //Here, input class number, description, units, and grades
    for(int i = 0; i < numberofcourses; i++)
    {
        System.out.println("Please enter the #"+(i+1)+" class name: ");
        String ClassName = scanner.nextLine();

        System.out.println("Please enter the #"+(i+1)+" description: ");
        String Description = scanner.nextLine();

        System.out.println("Please enter the #"+(i+1)+" units: ");
        int Units = scanner.nextInt();

        System.out.println("Please enter the #"+(i+1)+" grades: ");
        int Grades = scanner.nextInt();

        System.out.println("Class Grades - "+term+" Term");
        System.out.println("Office Grades");
        System.out.println(String.format("Class: %s Description: %s Units: %d Grade: %d Grade Points", ClassName, Description, Units, Grades));
    }

这是我的控制台的样子:

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2012
Please enter the number of courses that you are enrolled in Fall 2012: 
1
Please enter the #1 class name: 
Comp Sci 2001
Please enter the #1 description: 
Computer Science
Please enter the #1 units: 
3
Please enter the #1 grades: 
95
Class Grades - Fall 2012 Term
Office Grades
Class: Comp Sci 2001 Description: Computer Science Units: 3 Grade: 95 Grade Points

显然,输出可以按照您喜欢的任何方式进行格式化,这只是一个快速模型。