【发布时间】:2019-06-20 16:49:11
【问题描述】:
我正在尝试创建一个为学生生成成绩报告的程序。我想问以下问题:
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 吗?还是浮子?结帐docs.oracle.com/javase/1.5.0/docs/api/java/util/…
标签: java calculation gpa