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

在JAVA中,如何使用一个数组参数和多个其他参数调用方法?,java,arrays,methods,parameters,Java,Arrays,Methods,Parameters,在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 S

在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);
    }
}
导入java.util.array;
导入java.util.Scanner;
导入java.util.Random;
公共课统计{
静态扫描仪输入=新扫描仪(System.in);
公共静态void生成器域(int[]个数,int低,int高,int计数){//semi-done
//生成[low,high]范围内的随机数,并将其存储在数字数组中
低=1;
System.out.println(“创建500个从1到“+count+”:”)的随机数;
Random rand=新的Random();

如果(high>0&&high,您可以这样称呼它:


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

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

我想你要做的是返回数组,而不是将它作为输入参数。但是,这不是你的赋值,所以我们将在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));
  }
}

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

public static void generateRandoms(int low, int high, int count, int... numbers) { 
    // body of method
}
这允许您通过以下任一方式调用该方法:

  • generateRandoms(0,5,3);
  • generateRandoms(0,5,3,新整数[3]);
  • generateRandoms(0,5,3,0,0,0);

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

    您所说的“不声明任何”实际上是什么意思?您会得到错误,因为您从未声明过“数字”在main方法中,也没有为“numbers”数组指定大小。
    count
    仅用于printed@Jaydasciencenoob我修复了方法签名。相信我,这些数字是随机的;它们看起来不是随机的,因为我们调用
    数组。排序
    。试着用更高的上限运行它。
    
    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);