java - 如何使用 Java 匹配 2 个数组并绘制图表

标签 java arrays loops for-loop graph

我正在开发一个学校项目,用户将输入学生人数、科目数量科目名称、学生姓名和成绩输入的每个科目的学生将被保存到一个数组中。最后一部分应使用星号 * 制作一个图表,表示通过的人数。除了图表之外,我能够解决该项目。这是您需要理解的确切问题:

The BIT-CT department asked you to create an application that would stores data to an array. Your program must ask the number of students, number of subjects and subject name. Enter the student name and grade per subject, as average grade per student is displayed. Display a graph that will indicate how many students passed the subject by using asterisk (), passing grade is 75.00, and finally displays the highest average grade among the student.*

示例输出

MIDTERM EXAM: STUDENT RANKING

Enter # of Students:  3
Enter # of Subjects:  2
-----------------------------------------------
Enter your 2 subjects: 
1. Java
2. Python
-----------------------------------------------
1. Name: Jan
   Grade in Java: 90
   Grade in Python: 100
Average: 95.0
-----------------------------------------------
2. Name: Jen
   Grade in Java: 100
   Grade in Python: 65
Average: 82.5
-----------------------------------------------
3. Name: Jane
   Grade in Java: 100
   Grade in Python: 90
Average: 95.0
-----------------------------------------------

Highest average score is: 95.0

----GRAPH-----
Java: ***
Python: **

你能帮我解决图表部分吗?我无法发布整个代码,因为它是一个至少 5 个部分的项目(每个部分有 60 多名学生),我确信他们都在寻找答案,但这是我正在工作的图形部分的代码,假设输入与示例输出相同,

for (int count = 0; count < noOfSubject; count++){
                System.out.print(subjects[count] +": ");
            for (int counter = 0; counter < allGrades.length; counter ++){
                    if (allGrades[counter] >= 75){
                        System.out.print("*");}
                    else{
                        System.out.print("");}}
            System.out.println("");}

输出错误:

 ----GRAPH-----
 Java: *****
 Python: *****

正确输出:

 ----GRAPH-----
 Java: ***
 Python: **

noOfSubject is obviously, number of subjects entered by the user.

subjects is the name of subjects entered by the user.

The allGrades variable is an array consisting of all grades entered regardless of subject. so based on the sample output: allGrades = {90, 100, 100, 65, 100, 90}

我已经问过有关该项目的问题,但不是我正在处理的最后一部分,所以请不要将其与同一问题混淆。谢谢!

最佳答案

由于您将所有成绩转储到单个数组 allGrades 中,因此您的嵌套循环应该从科目的 count 开始按 noOfSubject 计数,如下所示:

for (int counter = count ; counter < allGrades.length ; counter += noOfSubject) {
    ...
}

注意:将所有成绩放在一个数组中并不是一个好主意。您最好按照学生或学科将其组织在二维数组中。

关于java - 如何使用 Java 匹配 2 个数组并绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008018/

相关文章:

c# - 循环删除列表中的多个项目...c#

c - 求给定整数的因数之和并将其除以给定整数

java - JPQL 查询 - 实体包含多头列表

java - Spring Security 权限编程检查

arrays - 相同脚本但具有不同输入参数的 SLURM sbatch 作业数组并行运行

javascript - 如何异步多次调用 promise 函数以用给定数量的值填充数组?

arrays - 多峰的一维寻峰算法

PHP - 在循环的帮助下从表单插入mysql数据库,可能吗?

Java继承。有趣的事情

java - 将来自不同类层次结构路由的两个字段用于 map-filter lambda 表达式