我想打印包含以下信息的列表:
| 学科|等级|平均|
| --------------|--------------|--------------|
| 纪律1|1级、2级、3级、|平均纪律1|
| 纪律2|1级、2级、3级、|平均纪律1|
问题是我只做了一份成绩单,我不知道如何为每一门学科都做一份新的清单。
我试过这个:
package graduation;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GraduationGrade {
public static void main(String[] args) {
// TODO Auto-generated method stub
List <String> disciplines = new ArrayList<>();
List <Integer> disciplineGrades = new ArrayList<>();
List <Integer> disciplineAverage = new ArrayList<>();
int i=0;
while(true) {
System.out.println("Please enter a discipline or type EXIT");
Scanner sc = new Scanner(System.in);
String discipline = sc.next();
if(discipline.equals("EXIT")) {
System.out.println(" DISCIPLINE GRADES AVERAGE ");
for (int j = 0; j < disciplines.size(); j++) {
System.out.println((j + 1) + " " + disciplines.get(j) + " " + disciplineGrades.get(j)+ " " + disciplineAverage.get(j));
}
break;
} else {
System.out.println("Please enter how many grades do you have for " + discipline);
sc = new Scanner(System.in);
int gradeNumbers = sc.nextInt();
do {
System.out.println("Please enter your grades for " + discipline + " one by one (between 4 and 10)");
sc = new Scanner(System.in);
int grades = sc.nextInt();
int sum = disciplineGrades.stream().mapToInt(Integer::intValue).sum();
int average= sum/gradeNumbers;
disciplines.add(discipline);
disciplineGrades.add(grades);
disciplineAverage.add(average);
i++;
} while(i<gradeNumbers);
}
}
}
}
当我运行程序时,这是我得到的:
Please enter a discipline or type EXIT
Math
Please enter how many grades do you have for Math
4
Please enter your grades for Math one by one (between 4 and 10)
4
Please enter your grades for Math one by one (between 4 and 10)
5
Please enter your grades for Math one by one (between 4 and 10)
6
Please enter your grades for Math one by one (between 4 and 10)
7
Please enter a discipline or type EXIT
Biology
Please enter how many grades do you have for Biology
10
Please enter your grades for Biology one by one (between 4 and 10)
8
Please enter your grades for Biology one by one (between 4 and 10)
6
Please enter your grades for Biology one by one (between 4 and 10)
7
Please enter your grades for Biology one by one (between 4 and 10)
5
Please enter your grades for Biology one by one (between 4 and 10)
4
Please enter your grades for Biology one by one (between 4 and 10)
6
Please enter a discipline or type EXIT
EXIT
DISCIPLINE GRADES AVERAGE
1 Math 4 0
2 Math 5 1
3 Math 6 2
4 Math 7 3
5 Biology 8 2
6 Biology 6 3
7 Biology 7 3
8 Biology 5 4
9 Biology 4 4
10 Biology 6 5
2条答案
按热度按时间ruarlubt1#
源代码的问题在于,它将每个学科的所有成绩存储在一个列表(
disciplineGrades
)中。因此,在计算平均值时,它使用所有成绩的总和,而不是特定学科的成绩总和。要解决这个问题,需要创建一个列表列表,分别存储每个学科的成绩,可以修改代码如下:
这段代码创建一个名为
disciplineGrades
的List<List<Integer>>
来保存每个学科的成绩。当添加一个新学科时,将创建一个新的空成绩列表并添加到disciplineGrades
中。然后,当用户输入该学科的成绩时,这些成绩将添加到disciplineGrades
中的相应列表中。当程序完成接受输入时,通过迭代
disciplineGrades
列表,对每个学科的分数求和,然后除以分数,计算每个学科的平均分数。平均值存储在一个单独的List<Integer>
中,称为disciplineAverages
。最后,当用户输入“EXIT”时,程序通过迭代
disciplines
、disciplineGrades
和disciplineAverages
列表,检索每个学科的相应值,并以所需格式打印出学科、成绩和平均值的表。最终输出如下。
rt4zxlrg2#
首先,不要将Scanner声明放入
while
循环中。在每次while
循环迭代时,您都要声明一个新的Scanner对象。您只需要一个Scanner,因此在成员区域中声明它,以便它可以在整个类中全局使用。您还声明了一个新的Scanner for ever prompt(sc = new Scanner(System.in);
)......尽量不要这样做。不要使用一堆List,而是使用一个Discipline List(
List<Discipline>
)。创建一个Discipline对象类,并将其用于您的oneList。在所有提示中验证用户的输入也是一个好主意,这样就不会遇到任何错误。你会注意到在提供的可运行代码中,我喜欢只对所有字母,数字或字母数字输入提示使用Scanner#nextLine()方法。然而,是个人的选择,因为我发现它是最灵活的,我不需要利用 Try/Catch 机制进行提示验证。
下面的可运行代码使用了一个Discipline类。虽然在下面的演示代码中,它是一个 * 内部类 ,但它实际上应该是一个*.java**类文件。下面是可运行代码。请务必阅读代码中的所有注解。如果您愿意,您可以在完成这些注解时删除它们,因为太多的注解可能会相当显眼。
运行此代码时,条目可能看起来像....