gpt4 book ai didi

java - Java 成绩报告

转载 作者:行者123 更新时间:2023-12-02 04:10:35 25 4
gpt4 key购买 nike

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

1 - 当我运行程序时,我遇到了问题 Exception in thread "main"java.util.InputMismatchException (请参阅下面的详细信息)。

2 - 如何将输出格式化为表格?我计划有 5 列:类(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();。这会返回一个 boolean 条件,让您知道下一行是否存在。您当前不使用此 boolean 条件,只是让它不执行任何操作。如果您想使用它,则需要在 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

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

关于java - Java 成绩报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com