BlueJ/Java程序返回一个新数组,该数组包含其参数的按组件求和 [英] BlueJ / Java program returning a new array containing the componentwise sum of its arguments

查看:142
本文介绍了BlueJ/Java程序返回一个新数组,该数组包含其参数的按组件求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码返回一个新数组,其中包含其参数的按分量求和(假设它们的长度相同),例如,如果输入数组为{0,1,2}和{2,2,3},则out out将为{2,3,5},并且如果输入数组具有不同数量的元素,则该方法应返回null.

The following code returns a new array containing the componentwise sum of its arguments (assuming their length is the same), for example if the input arrays are {0,1,2} and {2,2,3} then the out out will be {2,3,5} and if the input arrays have different numbers of elements, the method should return null.

public class Sum
{
    public static double[] sum(double a[], double b[]){

    if (a.length != b.length)
    return null;
    int[] s = new double[a.length];
    for(int i=0; i<a.length; i++)
    s[i] = a[i] + b[i];
    return s;
    }
 public static void main(String[] args) {
        //calling the method to seek if compiles
        double[] results = Sum.sum(new double[] {0,1,2} + {2,2,3});
        //printing the results
        System.out.println(java.util.Arrays.toString(results));
    }
}

正如我之前通常提到的那样,当我在上面运行此程序代码时,我应该以{2,3,5}作为结果,但是我什么也没得到.实际上,我无法在BlueJ中对其进行编译,我一直收到以下错误消息:该行的表达式非法开头:double []结果= Sum.sum(new double [] {0,1,2} + { 2,2,3});

As I have mentioned before normally when I run this program code above I supposed to have {2,3,5} as result, but I am getting nothing. In fact I cannot able to compile it in BlueJ, I am keep getting the error message saying: illegal start of expression for this line: double[] results = Sum.sum(new double[] {0,1,2} + {2,2,3});

所以我假设我确实犯了语法错误,但是我不太确定,如何摆脱它,有什么建议吗?

So I assume I did syntax error, but I am not too sure, how can I get rid of it, any suggestions?

推荐答案

您的代码中存在两个问题:

There are two problems in your code:

  • 仅原始数字类型允许使用+运算符.在这行代码中:

  • The + operator is allowed for primitive numeric types only. In this line of code:

Sum.sum(new double[] {0,1,2} + {2,2,3});

您正在尝试为double[]变量应用+运算符,这是不允许的,因此会出现异常.由于该方法已经接受2个参数double[],因此通过使用逗号,分隔两个double[]来发送这两个double[].

You're trying to apply + operator for double[] variables, which is not allowed, thus getting the exception. Since the method already accepts 2 arguments double[], then send both double[]s by separating them using comma ,.:

Sum.sum(new double[] {0,1,2}, new double[] {2,2,3});

  • 这行代码:

  • This line of code:

    int[] s = new double[a.length];
    

    您不能使用其他原始类型的数组初始化原始类型的数组. int[]double[]是完全不同的类.因此,改为更改变量的类型声明:

    You cannot initialize an array of primitive type with an array of another primitive type. int[] and double[] are complete different classes. So, instead change the type declaration for your variable:

    double[] s = new double[a.length];
    

  • 更多信息:

    • The Java Tutorial. Operators
    • The Java Tutorial. Primitive Data Types
    • The Java Tutorial. Arrays

    这篇关于BlueJ/Java程序返回一个新数组,该数组包含其参数的按组件求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆