How do I invoke a method with array parameter in java? [closed]

若如初见. 提交于 2020-05-11 14:47:52

问题


I have an assignment in which I have to perform operations on array in Java, I have to make separate functions of each operation, which I will write but I can not figure out how to invoke a method with array parametres. I usually program in c++ but this assignment is in java. If any of you can help me, I'd be really thankful. :)

public class HelloJava {
    static void inpoot() {
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
                System.out.println(numbers[i]); 
        }
    }

    public static void main(String[] args) {
        inpoot();
        outpoot(numbers); //can not find the symbol
    }
}

回答1:


Your inpoot method has to return the int[] array, and then you pass it to outpoot as a parameter:

public class HelloJava {    
    static int[] inpoot() { // this method has to return int[]
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
        return numbers; // return array here
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
            System.out.println(numbers[i]); 
        }
    }

     public static void main(String[] args) {
        int[] numbers = inpoot(); // get the returned array
        outpoot(numbers); // and pass it to outpoot
    }
}



回答2:


When you call outpoot it should be outpoot (numbers);



来源:https://stackoverflow.com/questions/20314305/how-do-i-invoke-a-method-with-array-parameter-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!