我有一个赋值,我必须在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
}
}
答案 0 :(得分:8)
您的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
}
}
答案 1 :(得分:0)
当你喊出它应该输出(数字);