Java成绩报告

时间:2019-06-20 16:49:11

标签: java calculation gpa

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

1-运行程序时,我在线程“ main” java.util.InputMismatchException中遇到异常(请参见下面的详细信息)。

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

1 个答案:

答案 0 :(得分:0)

因此,首先,您有很多需要解决的地方,包括我将要为您解决的地方。如果要在末尾一起输出所有类,则必须编辑我的代码以将格式化的字符串存储在数组中,然后将它们全部一起输出。

我在您的scanner.nextLine();行之后添加了int numberofcourses = scanner.nextInt();,以解决您的错误。您需要执行此操作,因为scanner.nextInt()不会消耗行终止符并移至下一行,因此这意味着您的下一个nextLine()会混乱并且尝试不读取任何内容。此额外的行可确保Scanner移至下一行。

继续前进,您有一堆不正确使用的scanner.hasNextLine();。这将返回一个布尔条件,让您知道下一行是否存在。您当前不使用此布尔条件,而只是不执行任何操作。如果要使用它,则需要在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

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