使用Array的元素填充另一个数组 - Java

时间:2016-11-18 17:58:45

标签: java arrays

我正在自学Java,刚学完数组的基础知识,或者我认为。我想创建一个班级成绩单,保持每个学生的成绩和平均成绩。我有一个for循环,要求10名学生中的每一个进入他们的4个考试成绩。一旦我得到这4个等级,我就取平均值。然后我将学生的成绩和平均值存储到一个数组中,每个学生一个数组。我基本上使用for循环为每个学生创建了10个包含这5个元素的数组。我现在想要从每个学生的数组中取第5个元素,即4个等级的平均值,并填充另一个名为averages的数组,以便我可以执行其他计算。这可能与我的逻辑有关吗?我想我可以硬编码10个数组,每个学生1个像这样:

double averages[] = {student1[4], student2[4], ..., student10[4]};

这不是一个不好的方法吗?任何建设性的帮助或指导将不胜感激。请不要发布给出答案的代码,因为我不会从中学习。我只想提示正确的方向。 :)

这是我的代码直到混乱:

import java.util.Scanner;

public class ClassAverages {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        double grade1 = 0.0, grade2 = 0.0, grade3 = 0.0, grade4 = 0.0, average = 0.0;

        // get grades from each of the 10 students
        for (int student = 1; student <= 3; student++) {
            System.out.println("Student " + student);
            System.out.println("---------\n");
            System.out.print("Enter the first grade: ");
            grade1 = keyboard.nextDouble();
            while (grade1 < 0) { // input validation for grade 1
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade1 = keyboard.nextDouble();
            }
            System.out.print("Enter the second grade: ");
            grade2 = keyboard.nextDouble();
            while (grade2 < 0) { // input validation for grade 2
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade2 = keyboard.nextDouble();
            }
            System.out.print("Enter the third grade: ");
            grade3 = keyboard.nextDouble();
            while (grade3 < 0) { // input validation for grade 3
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade3 = keyboard.nextDouble();
            }
            System.out.print("Enter the fourth grade: ");
            grade4 = keyboard.nextDouble();
            System.out.println();
            while (grade4 < 0) { // input validation for grade 4
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade4 = keyboard.nextDouble();
                System.out.println();
            }

            // calculate the current student's average
            average = (grade1 + grade2 + grade3 + grade4) / 4;

            // for each student, 1 to 10, create an array with their 4 grades and average
            double studentX[] = { grade1, grade2, grade3, grade4, average };
            System.out.println("SCORE 1\t\tSCORE 2\t\tSCORE 3\t\tSCORE 4\t\tAVERAGE");
            System.out.print(studentX[0] + "\t\t");
            System.out.print(studentX[1] + "\t\t");
            System.out.print(studentX[2] + "\t\t");
            System.out.print(studentX[3] + "\t\t");
            System.out.print(studentX[4] + "\n");
            System.out.println();

            // I want to use each student's average for each corresponding element in the averages array 
            // create an array of all student's averages
            // double averages[] = {student1average, student2average,...student10average} ???
        }
    }
}

现在输出:

Student 1
---------

Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 100
Enter the fourth grade: 100

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
100.0       100.0       100.0       100.0       100.0

Student 2
---------

Enter the first grade: 90
Enter the second grade: 90
Enter the third grade: 90
Enter the fourth grade: 80

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
90.0        90.0        90.0        80.0        87.5

Student 3
---------

Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 90
Enter the fourth grade: 80

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
100.0       100.0       90.0        80.0        92.5

2 个答案:

答案 0 :(得分:1)

你可以使用for循环。但是,这将要求您拥有一组要迭代的项目。我认为你这样做的最好方法是将你的学生组织成他们自己独立的课程(将该课程的所有成绩作为参数提供给它,并让它在该课程中进行平均计算)。

因此,如果您致电您的班级student,那么您的构造函数可能看起来像student(int grade1, int grade2, int grade3, int grade4)。使用这些参数计算每个学生对象的平均值。

然后在您的ClassAverage类中,根据需要实例化新的student对象,并将它们添加到student个对象的数组中,然后就像迭代student数组一样简单,提取您为每个学生对象创建的average字段(假设您在学生班中为studentName.average字段average命名),只需说明average即可解压缩。

所以到最后你应该有一些看起来像这样的东西。我们假设学生对象数组被称为studentArray

int[] averageArray = new int[studentArray.length];
for (int i = 0; i < studentArray.length; i++){
averageArray[i] = studentArray[i].average;
}
祝你好运!

答案 1 :(得分:1)

数组是存储信息的非常糟糕的形式。主要是因为它们不包含使用此数组中的数据的逻辑,而任何其他代码都可能使该数据无效,因为数组不会检查您为其提供的值。这就是类的用途:在一个地方拥有与之一起使用的数据和代码,并保护数据免受代码中其他地方的更改。

因此,对于您的情况,对于相同类型的字段(例如,学生数组是正常的),数组是正常的,但不适用于那些类型的值(学生成绩数组的数组不正常)。所以你应该在这里做的是使用一个类:

class Student
{
     int grades[] = new int[4];
     public Student(int grade1, grade2, int grade3, int grade4)
     {
          grades[0] = grade1; 
          grades[1]=grade2; 
          grades[2]=grade3; 
          grades[3]=grade4;
     }
     public double average()
     {
         double result = 0;
         for(int i =0; i<grades.count; i++)
           result += grades;
         return result / grades / count;
      }
  // and so on
}

它们允许你在对象中使用逻辑,你可以像这样使用它:

Student albert = new Student(5, 4, 2, 1);
System.out.println(albert.avarage()); // prints 3