爪哇成绩报告



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

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)

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

我通过在int numberofcourses = scanner.nextInt();行后添加scanner.nextLine();来修复您的错误。 您需要这样做,因为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

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

最新更新