我正在尝试将2个数组加在一起并获得总和的平均值 我该怎么做呢?我想在数组中产生答案。
public static void part1 (){
double examMarks [] = {50,40,60,80,70,11};
double courseworkmarks [] = {65,49,58,77,35,40};
System.out.println ("These are the exam marks and the course work marks");//First row is the exam marks, second row is the course work marks
computeMarks (examMarks);
computeMarks1 (courseworkmarks);
}
public static void computeMarks(double[] examMarks)
{
for (int row=0;row<examMarks.length;row++){
System.out.print (examMarks[row] +"\t");
}
System.out.println();
}
public static void computeMarks1(double[] courseworkmarks)
{
for (int row=0;row<courseworkmarks.length;row++){
System.out.print (courseworkmarks[row] +"\t");
}
System.out.println();
}
答案 0 :(得分:2)
您可以尝试以下内容
double examMarks [] = {50,40,60,80,70,11};
double courseworkmarks [] = {65,49,58,77,35,40};
double avgMarks[] =new double[examMarks.length];
for(int i=0;i<avgMarks.length;i++){
avgMarks[i]=(examMarks[i]+courseworkmarks[i])/2;
}
答案 1 :(得分:0)
让你的方法返回一个数组。你可以做这样的事情
public static double[] computeMarks(double[] examMarks)
{
int[] newArray = new int[examMarks.length];
for (int row=0;row<examMarks.length;row++){
newArray[i] = (examMarks[row] + courseworkmarks[row]) / 2;
}
return newArray
}
打印
public static void main(String[] args){
double[] array = computeMarks(yourArray);
for (int i = 0; i < array.length; i++){
System.out.println(array[i] + "\t");
}
}
答案 2 :(得分:0)
您可以使用length = array.length + array2.length;
定义数组使用 System.arraycopy 复制值。
以下是组合2个数组的示例方法。
public double[] copyTwoArrays(double[] arrayOne, double[] arrayTwo)
{
if(null == arrayOne || arrayOne.length == 0)
{
return arrayTwo;
}
if( null == arrayTwo || arrayTwo.length == 0)
{
return arrayOne;
}
double[] result = new double[arrayOne.length + arrayTwo.length];
System.arraycopy(arrayOne, 0, result, 0, arrayOne.length);
System.arraycopy(arrayTwo, 0, result, arrayOne.length, arrayTwo.length);
return result;
}
以你的阵列为例:
double examMarks [] = {50,40,60,80,70,11};
double courseworkmarks [] = {65,49,58,77,35,40};
您可以使用以下代码填充组合数组。
double[] result = someInstance.copyTwoArrays(examMarks, courseworkmarks);
答案 3 :(得分:0)
您可以使用
之类的功能 public static void totalMarks(double[] examMarks, double[] courseworkmarks){
double total[] = new double[6];
double totalMarks = 0;
System.out.println("================================================");
for(int i = 0;i < examMarks.length;i++){
total[i]=examMarks[i] + courseworkmarks[i];
totalMarks = totalMarks+total[i];
System.out.print(total[i]+"\t");
}
System.out.println("========================================");
System.out.println("total marks are "+totalMarks);
System.out.println("average is "+(totalMarks/examMarks.length));
// total;
}
如果您需要,可以将其分为两部分,总计和平均值