简体   繁体   English

Java中第一个和最后一个数组元素的总和

[英]Sum of the first and last array elements in Java

I'm using JB IntelliJ IDEA and trying to create a program which finds the sum of the first and last elements of a randomly generated array using the sum() method. 我正在使用JB IntelliJ IDEA,并尝试创建一个程序,该程序使用sum()方法查找随机生成的数组的第一个元素和最后一个元素的sum() My problem is that an error occures, please help me. 我的问题是发生错误,请帮助我。

Here is my code: 这是我的代码:

package com.company;
import java.util.Random;
public class Main {
    public static int sum(int[] array) {
        int x = array[0];
        int y = array[9];
        int z = x + y;
        return z;
    }
    public static void main(String[] args) {
        int[] array = new int[10];
        Random rand = new Random();
        for (int i = 0; i < array.length; i++) {
            int j = rand.nextInt(50);
            System.out.println(sum());
        }
    }
}

and error: 和错误:

Error:(15, 32) java: method sum in class com.company.Main cannot be applied to given types; 错误:(15,32)java:类com.company.Main中的方法sum不能应用于给定类型;
required: int[] 必需:int []
found: no arguments 找到:没有参数
reason: actual and formal argument lists differ in length 原因:实际和正式论点清单的长度不同

** Error:(15, 32) java: method sum in class com.company.Main cannot be applied to given types; ** 错误:(15,32)java:类com.company.Main中的方法sum无法应用于给定类型;

required: int[] 必需:int []

found: no arguments 找到:没有参数

reason: actual and formal argument lists differ in length 原因:实际和正式论点清单的长度不同

Going through the Error itself says everything about the issue: 经历错误本身就说明了有关该问题的所有信息:

required: int[] 必需:int []

found: no arguments 找到:没有参数

It is saying that it required an array of data type int which is missing(no arguments) and that is why actual and formal argument lists differ in length 据说它需要一个数据类型为int的数组,该数组缺少(没有参数),这就是为什么实际和正式参数列表的长度不同的原因

Hence, Sum function requires an array to be passed as a parameter. 因此,求和函数需要将数组作为参数传递。

Also, you are getting random integer value in a integer variable j = rand.nextInt(50); 另外,您正在整数变量j = rand.nextInt(50);中获得随机整数值 but not assigning it to the array which is just wasting the loop to run unnecessarily 10 times. 但没有将其分配给只会浪费循环不必要地运行10次的数组。

Instead of assigning it to j we can directly assign it to the array and fill array with the random integers before passing it to the method sum(array) : 除了将其分配给j之外,我们还可以将其直接分配给数组,并在将其传递给方法sum(array)之前用随机整数填充array:

Try this updated code with the changes needed: 尝试使用此更新的代码进行所需的更改:

package com.company;
import java.util.Random;
public class Main {
    public static int sum(int[] array) {
        int x = array[0];
        int y = array[9];
        int z = x + y;
        return z;
    }
    public static void main(String[] args) {
        int[] array = new int[10];
        Random rand = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(50);
        }
        System.out.println(sum(array));
    }
}

You did not pass the array to the method. 您没有将数组传递给方法。 Change your line 换行

System.out.println(sum());

to

System.out.println(sum(array));

Also you did not put j into array and you call sum before array is filled. 另外,您没有将j放入array ,而是在填充array之前调用sum。

You method sum requires an attribute of the type int[]. 您的sum方法需要一个int []类型的属性。 But in the statement System.out.println(sum()); 但是在语句System.out.println(sum());中 you are not passing an attribute to your method. 您没有将属性传递给您的方法。

Try out this code: 试用以下代码:

    public static int sum(int[] array) {
        int x = array[0];
        int y = array[9];
        int z = x + y;
        return z;
    }
    public static void main(String[] args) {
        int[] array = new int[10];
        Random rand = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(50);
            System.out.println(array[i]);
        }
        System.out.println(sum(array));
    }

You should initialize the array with the random() , and then put the array as an argument to call sum() function. 您应该使用random()初始化array ,然后将array作为调用sum()函数的参数。 The codes as follows: 代码如下:

public static void main(String[] args) {
    int[] array = new int[10];
    Random rand = new Random();
    for (int i = 0; i < array.length; i++) {
        int j = rand.nextInt(50);
        array[i] = j;
    }
    System.out.println(sum(array));
}

And then, you can get the result that you want. 然后,您可以获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM