gpt4 book ai didi

java - 无法制作正确的表格,GPA 计算无法返回正确的成绩

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

我正在尝试编写一个程序来返回与用户输入(A、A-、B+ 等)等效的成绩,然后为用户生成成绩报告。

但是,我遇到了两个问题:

  1. 我的程序不返回任何成绩,“无效成绩”除外。无论我输入 A、B、C、B+ 还是其他内容,都没有关系。

  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 ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String grade[] = new String[numberofcourses];
int Units[] = new int[numberofcourses];
double gradeValue = 0;

//Arrays for class number, description, units, grade, grade point
//Here, input class number, description, units, and grade
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
grade[i] = scanner.nextLine();


Map<String, Double> gradeToScore = new HashMap<>();
gradeToScore.put("A", 4.00);
gradeToScore.put("A-", 3.67);
gradeToScore.put("B+", 3.33);
gradeToScore.put("B", 3.00);
gradeToScore.put("B-", 2.67);
gradeToScore.put("C+", 2.33);
gradeToScore.put("C", 2.00);
gradeToScore.put("D+", 1.33);
gradeToScore.put("D", 1.00);
gradeToScore.put("F", 0.0);
gradeToScore.put("FX", 0.0);
if(gradeToScore.containsKey(grade)) {
gradeValue = gradeToScore.get(grade);
}else{
System.out.println("Invalid Grade");
}
}

//Print out the report

//Print out the heading
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");

//Print out the table
int columns = 5;
int rows = numberofcourses;
String[][] table = new String[rows][columns];
String chain = "";
{
for (int i = 0; i < table.length; i++)
{
for (int c = 0; c < table[0].length; c++)
{
chain += "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints";
}
chain += "|\n";
chain += "|" + ClassName[i] + Description[i] + Units[i] + grade[i] + gradeValue;
}
System.out.println(chain);
}
}
}

这是我对上述代码的结果:

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2019
Please enter the number of courses that you are enrolled in Fall 2019:
1
Please enter your #1 class name:
FIN 301
Please enter your #1 class description:
Personal Finance
Please enter your #1 class units:
3
Please enter your #1 class grade:
A
Invalid Grade
Class Grades - Fall 2019 Term
Office Grades
|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|
|FIN 301Personal Finance3A0.0

注意:

我确实尝试过使用System.out.format,但结果并不好。这是我的代码:

System.out.format("%30s %25s %10s %25s %10s %25 %10 %25 %10 %25", "Class", "|", "Description($)", "|", "Units", "|", "Grade", "|", "Grade Points");

System.out.format 的试用 2...肯定没有更好:

System.out.format("%n%n%n%n%n", "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints");
System.out.format("%n%n%n%n%n", ClassName[i], Description[i], Units[i], grade[i], gradepoint);

必须有某种语法,但我必须在某处读取左对齐、右对齐、居中对齐或我想要填充多少空间。请知道,如果我知道语法,我也会切换到 System.out.format。

最佳答案

好的,我找到了解决第一个问题的方法......这是我的等级转换 block 的部分代码:

    if (grade[i].equals ("A"))
gradeValue= 4.00;
else if (grade[i].equals("A-"))
gradeValue= 3.67;
else if (grade[i].equals("B+"))
gradeValue = 3.33;
else if (grade[i].equals("B"))
gradeValue = 3.00;
else if (grade[i].equals ("B-"))
gradeValue = 2.67;
else if (grade[i].equals("C+"))
gradeValue = 2.33;
else if (grade[i].equals("C"))
gradeValue = 2.00;
else if (grade[i].equals ("D+"))
gradeValue = 1.33;
else if (grade[i].equals ("D"))
gradeValue = 1.00;
else if (grade[i].equals ("F"))
gradeValue = 0;
else if (grade[i].equals ("FX"))
gradeValue = 0;
else
System.out.println ("Invalid Grade");

不过,我仍然不确定如何制作正确的二维数组表。 :(

关于java - 无法制作正确的表格,GPA 计算无法返回正确的成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56707510/

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