【问题标题】:How do I call method with an array parameter and multiple other parameters in JAVA?如何在 JAVA 中使用数组参数和多个其他参数调用方法?
【发布时间】:2015-08-03 19:32:40
【问题描述】:

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

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() 中调用这个方法,并在不声明的情况下传递一个数组参数?

【问题讨论】:

  • “不声明任何”实际上是什么意思?
  • 你会得到错误,因为你从来没有在你的 main 方法中声明“数字”,你也没有给“数字”数组一个大小。

标签: java arrays methods parameters


【解决方案1】:

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

我认为您想要做的是 return 数组,而不是将其作为输入参数。然而,这不是你的任务,所以我们将在 main.xml 中创建数组。我还更改了您的程序以使用: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]

【讨论】:

  • @Jaydasciencenoob 我修复了方法签名。相信我,这些数字是随机的;它们看起来不是随机的,因为我们调用Arrays.sort。尝试以更高的上限运行它。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    你可以这样称呼它,例如像这样:

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

    【讨论】:

      【解决方案4】:

      如果你改变参数的顺序,你可以很容易地做到这一点:

      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);

      【讨论】:

        【解决方案5】:
        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);
        

        【讨论】:

        • count 仅用于打印
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-05
        • 2020-06-06
        • 2011-06-01
        相关资源
        最近更新 更多