如何在JAVA中使用数组参数和多个其他参数调用方法?

时间:2015-08-03 19:32:40

标签: java arrays methods parameters

在Java中,如何使用数组参数和其他多个参数调用方法?在这种情况下,我尝试在generateRandoms(int[] numbers, int low, int high, int count){}方法中调用方法main()

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;

public class Statistics_19711 {

    static Scanner input = new Scanner(System.in);

    public static void generateRandoms(int[] numbers, int low, int high, int count){        // semi-done
        //Generate random numbers within the range [low, high] and store them in numbers array
        low = 1;
        System.out.println("Creating 500 random numbers from 1 to " + count + ":");
        Random rand = new Random();
        if (high > 0 && high <= 100000) {
            for (int i = 0; i < numbers.length; i++){
                numbers[i] = rand.nextInt();
                System.out.print(i+ ((i-(0-1))%10==0 ? "\n" : " "));
            }
        } else {
            System.out.println("Input outside of range. Try Again.");
        }
        Arrays.sort(numbers);
    }

    public static void main(String[] args) {
        //the main() method should call the above methods. The main() method is then only method that does the input and output operation.
        System.out.println("This program creates random numbers and calculates some statistics.");
        System.out.println("Enter the upper limit of all generated random numbers:");
        int high = input.nextInt();
        System.out.println("Enter the count(maximum of 100000) of random numbers:");
        int count = input.nextInt();

        generateRandoms(numbers, 1, high, count);
    }
}

int[]个数字是方法generateRandoms()中的局部变量。我们如何在main()中调用此方法并传递数组参数而不声明任何?

5 个答案:

答案 0 :(得分:1)

好像你的方法试图创建一个数组。你现在声明它的方式,数组被作为输入参数给出,这就是为什么它现在对你没有意义。

我认为你想要做的是return数组,而不是它作为输入参数。但是,这不是你的任务,所以我们将把数组放在main中。我还更改了您的程序,使用以下代码生成您范围内的随机数:numbers[i] = rand.nextInt(high) + low;

这是固定代码:

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;

public class Statistics_19711 {

  static Scanner input = new Scanner(System.in);

  public static void generateRandoms(int[] numbers, int low, int high, int count) { // semi-done
    // Generate random numbers within the range [low, high] and store them in
    // numbers array
    low = 1;
    System.out.println("Creating 500 random numbers from 1 to " + count + ":");
    Random rand = new Random();
    if (high > 0 && high <= 100000) {
      for (int i = 0; i < numbers.length; i++) {
        numbers[i] = rand.nextInt(high) + low;
        System.out.print(i + ((i - (0 - 1)) % 10 == 0 ? "\n" : " "));
      }
    } else {
      System.out.println("Input outside of range. Try Again.");
    }
    Arrays.sort(numbers);
  }

  public static void main(String[] args) {
    // the main() method should call the above methods. The main() method is
    // then only method that does the input and output operation.
    System.out
        .println("This program creates random numbers and calculates some statistics.");
    System.out
        .println("Enter the upper limit of all generated random numbers:");
    int high = input.nextInt();
    System.out.println("Enter the count(maximum of 100000) of random numbers:");
    int count = input.nextInt();
    int[] numbers = new int[count];
    int[] generatedRandoms = generateRandoms(numbers, 1, high, count);
    System.out.println(Arrays.toString(generatedRandoms));
  }
}

输出:

This program creates random numbers and calculates some statistics.
Enter the upper limit of all generated random numbers:
20
Enter the count(maximum of 100000) of random numbers:
50
Creating 500 random numbers from 1 to 50:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
[1, 1, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 11, 11, 11, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 20]

答案 1 :(得分:1)

看起来您正在尝试使用int []作为输出参数,这在Java中是不可能的。相反,您应该使用int []作为返回值,并在generateRandoms方法中创建数组。

答案 2 :(得分:0)

你可以打电话给他,例如像这样:

generateRandoms(new int[]{1,2,3}, 1, high, count);

答案 3 :(得分:0)

如果更改参数的顺序,则可以轻松完成此操作:

public static void generateRandoms(int low, int high, int count, int... numbers) { 
    // body of method
}

这允许您通过以下任何方式调用方法:

  1. generateRandoms(0, 5, 3);
  2. generateRandoms(0, 5, 3, new int[3]);
  3. generateRandoms(0, 5, 3, 0, 0, 0);

答案 4 :(得分:0)

int capacity = input.nextInt(); // the capacity of the array
int high = input.nextInt();
int count = input.nextInt();
int[] numbers = new int [capacity];

generateRandoms(numbers, 1, high, count);