我有一个类(class)中每个学生的考试成绩数组。例如,对于 Adam test1=90 test2=92 test3=93,因此平均值为 91.67。然后我将其保存为 Array6
。然后使用该数组,我将使用该数组的 30% 加上最终 (array5
) 的 70% 来制作新数组 courseaverage
。
我已尝试在下面的代码中实现它,但它无法正常工作。谁能提出问题...
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
}
}
}
最佳答案
数组没有 get() 方法,但 ArrayList 有,如果将数组更改为 ArrayLists,则可以使用 get() 检索值。
如果我理解正确的话,你有一个数组来保存每个学生的姓名,并且每个学生的成绩都保存在每个学生的单独数组中?
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);
这会给你“亚当”的平均值,你应该能够用它来找出你的问题的其余部分。
关于java - 查找数组中数字的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145910/