Java 如何将两个数组相加并获得总和的平均值?
我尝试将两个数组相加,得到总和的平均值 我该怎么做?我还想生成数组中的答案Java 如何将两个数组相加并获得总和的平均值?,java,arrays,eclipse,average,Java,Arrays,Eclipse,Average,我尝试将两个数组相加,得到总和的平均值 我该怎么做?我还想生成数组中的答案 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");//F
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();
}
公共静态无效部分1(){
双examMarks[]={50,40,60,80,70,11};
双程作业分数[]={65,49,58,77,35,40};
System.out.println(“这些是考试分数和课程作业分数”);//第一行是考试分数,第二行是课程作业分数
计算机标记(examMarks);
计算机成绩1(课程成绩);
}
公共静态无效计算标记(双[]个examMarks)
{
对于(int row=0;row您可以尝试以下方法
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;
}
double examMarks[]={50,40,60,80,70,11};
双程作业分数[]={65,49,58,77,35,40};
double avgMarks[]=新的double[examMarks.length];
对于(inti=0;i,让您的方法返回一个数组
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
}
公共静态双[]计算标记(双[]examMarks)
{
int[]newArray=newint[examMarks.length];
对于(int row=0;row您可以定义一个数组,其长度=array.length+array2.length
并使用System.arraycopy复制值
下面是组合这两个阵列的示例方法
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);
您可以使用如下函数
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;
}
publicstaticvoidtotalmarks(双[]个examMarks,双[]个courseworkmarks){
双倍总额[]=新的双倍[6];
双倍总分=0;
System.out.println(“===================================================================”);
for(int i=0;i
如果你需要的话,你可以把它分成两部分,分别计算总数和平均数我正在尝试添加examMarks和courseworkmarks。注意,我需要添加第一列,然后得到平均数,第二列,然后得到平均数。所以50+65,40+49,60+58等等。你的解决方案现在有什么问题吗?注意:这很好,除非数组是2不同的长度。对于这种情况可能很好,但是一个好的实践应该是确保数组长度不会导致错误ArrayOutOfBoundsException@RUJordan-如果数组的长度不同,那么OP的目标就没有意义。它只针对长度相等的数组定义。“可能适合这种情况”我相信这就是你的意思(examMarks[row]+courseworkmarks[row])/2;是的,你是对的。我刚才只是给出了一个根据OP请求返回数组的更多示例。但对于问题的最终解决方案,OP可能需要这个。谢谢。
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;
}