4

我有一个任务,我必须在 Java 中对数组执行操作,我必须为每个操作创建单独的函数,我将编写这些函数,但我不知道如何使用数组参数调用方法。我通常用 c++ 编程,但这个作业是用 java 编写的。如果你们中的任何人可以帮助我,我将非常感激。:)

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
    }
}
4

2 回答 2

10

您的inpoot方法必须返回int[]数组,然后将其outpoot作为参数传递给:

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
    }
}
于 2013-12-01T16:40:59.787 回答
0

当您调用 outpoot 时,它应该是 outpoot(数字);

于 2013-12-01T16:23:31.093 回答