【问题标题】:How to add an array to an array?如何将数组添加到数组中?
【发布时间】:2013-11-08 05:52:40
【问题描述】:

我设法将考试和课程作业的分数加起来得到平均分。我意识到这是需要发生的事情。

computed module mark = ((coursework mark * coursework weighting) + (examination mark * (100 - coursework weighting))) / 100

所以,我需要制作 2 个数组(如果我是正确的),每个数组都有每个模块的权重,然后进行这些计算。如何将数组添加到已经存在的数组中? 这是我目前所拥有的:

public static void main (String [] args)
{
    computeResults();
}
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();
    }
public static void computeResults()
{
     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;

        System.out.println(avgMarks[i]);
        }
}

}

【问题讨论】:

  • 我认为您在computeResults 的最后一个for 循环中正确添加了两个数组。您面临的问题是什么?你有什么错误吗?还是意想不到的结果?
  • 我得到了平均值而不是所有它们的总数并为每个返回它,现在我需要得到它的总数,添加所有 6 个不同的结果并产生 100 个结果,例如全年总分

标签: java arrays eclipse


【解决方案1】:

这样就好了:

public static void main (String [] args)
{
    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);
    System.out.println ("These are the final marks");
    computeResults(examMarks, 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("\n");
    }


public static void computeResults(double[] examMarks, double[] courseworkmarks)
{

        double avgMarks[] =new double[examMarks.length];

        for(int i=0;i<avgMarks.length;i++){
        int cwWeighting=40;
            avgMarks[i]=(examMarks[i]*(100-cwWeighting)+courseworkmarks[i]*cwWeighting)/100;

        System.out.print (avgMarks[i] +"\t");
        }
}

【讨论】:

  • 还有一个问题,我有什么办法可以为我拥有的 2 个数组做一个捷径吗?就像它们在第 1 部分和 computeMarks 方法中一样。有没有办法做一个快捷方式?所以当我需要更改标记时,我不必同时更改两者? (只回答这个线程)
  • 你是说你想从文件中读入它们吗?如果是这样,这完全是一个新问题,您可能希望将其作为一个新问题发布,或者查看本网站上已经存在的大量类似问题。
  • 谢谢老兄,太好了。它使它变得更简单和整洁:D
  • 我在其中添加了一些代码,以便打印出数字含义的标题。再次感谢您的帮助
  • 还有一件事,很抱歉再次打扰您。
猜你喜欢
  • 1970-01-01
  • 2012-01-17
  • 2019-02-11
  • 1970-01-01
  • 2020-02-22
  • 2011-01-10
  • 2012-05-31
  • 2021-09-13
  • 2014-08-05
相关资源
最近更新 更多