首页 > 解决方案 > Java 中的成绩报告

问题描述

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

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)

标签: javacalculationgpa

解决方案


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

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

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


推荐阅读