简体   繁体   English

查找数组中的数字平均值

[英]Finding the average of numbers in an array

I have an array of test grades for each student in a class. 我为班上的每个学生准备了一系列考试成绩 As an example, for Adam test1=90 test2=92 test3=93 , so the average would be 91.67 . 例如,对于Adam test1 = 90 test2 = 92 test3 = 93 ,所以平均值为91.67 I would then save this as Array6 . 然后我将其保存为Array6 Then using that array, I would take 30% of that plus 70% of the final ( array5 ) to make new array courseaverage . 然后使用那个数组,我将占用30% ,加上70%的final( array5 )来制作新的数组courseaverage

I have tried to implement this in my code below, but it doesn't work correctly. 我试图在下面的代码中实现它,但它无法正常工作。 Could anyone please suggest the problem... 有人可以建议问题......

public class Proj5 {
    public static void main (String[] args) {        
        String[] Array1 = {new String ("Adam"),new String ("Smith"),new String ("Jones"),new String ("Becky"),new String ("Taylor")};       
        Integer[] Array2 = {new Integer(90),new Integer(89),new Integer(86),new Integer(76),new Integer(95)};        
        Integer[] Array3 = {new Integer(92),new Integer(79),new Integer(85),new Integer(90),new Integer(87)};
        Integer[] Array4 = {new Integer(93),new Integer(80),new Integer(90),new Integer(87),new Integer(92)};
        Integer[] Array5 = {new Integer(90),new Integer(77),new Integer(86),new Integer(92),new Integer(89)};

        System.out.println("Name Test1 Test2 Test3 Final Average Grade");

        for (int column = 0; column<Array1.length; column++){
            System.out.printf("%s ", Array1[column]);
            System.out.printf("%s   ", Array2[column]);
            System.out.printf("%s    ", Array3[column]);
            System.out.printf("%s    ", Array4[column]);
            System.out.printf("%s  ", Array5[column]);
            System.out.println(); //start new line of output
        }
    }
}

An array does not have a get() method but an ArrayList does so if you change your arrays to ArrayLists then you can use get() to retrieve the value. 数组没有get()方法,但是如果将数组更改为ArrayLists,则ArrayList会这样做,那么您可以使用get()来检索值。

If I understand correctly you have an array that holds the names of each student and each student's grades are held in a seperate array for each student? 如果我理解正确,你有一个数组,其中包含每个学生的姓名,每个学生的成绩都是针对每个学生的单独分数?

String[] students= {"Adam","Smith","Jones"}; 
int[] adamGrades = {90,92,93};
double gradesTotal = 0.00;
for(int i=0; i < adamGrades.length; i++){
  gradesTotal += adamGrades[i];
}
double avg = (gradesTotal/adamGrades.length);
System.out.print(avg);

That would give you the average for "Adam" and you should be able to use that to figure out the rest of your problem. 这会给你“亚当”的平均值,你应该能够用它来找出问题的其余部分。

To not give away the exact answer (since this is homework) you need to take the array, add up all the elements and divide that sum by the number of elements in the array. 为了不泄露确切的答案(因为这是家庭作业),您需要获取数组,将所有元素相加并将该总和除以数组中的元素数。 This could be done easily in a function call accepting the array and returning the average. 这可以在接受数组并返回平均值的函数调用中轻松完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM