如何在main方法内部调用另一个方法?

时间:2019-04-02 02:57:59

标签: java arrays methods

我正在尝试调用方法readAndFill();在我的主要方法中。我收到一条错误消息,提示“ readAndFill无法应用于给定类型。readAndFill是一种将数字放入数组的方法。

我已重命名该方法,将readAndFill(double [] array,int numOfNumbers)代替了readAndFill();。

package program9;
import java.util.Scanner;
import java.util.Arrays;
public class Program9 {
static void readAndFill(double[]array, int numOfNumbers) {
    Scanner keyboard = new Scanner(System.in);
    for(int i=0;i < numOfNumbers; i++){
        System.out.print("Enter a number: ");
        array[i] = keyboard.nextDouble();
    }
}
public static void main(String[] args) {
    int numOfNumbers;
    Scanner keyboard = new Scanner(System.in);
    double arrayNumbers;
    System.out.print("How many numbers will be read in the array?: 
");
    numOfNumbers = keyboard.nextInt();
    double[] array = new double[numOfNumbers];
        readAndFill();
}
static void readAndFill(double[]array, int numOfNumbers) {
    Scanner keyboard = new Scanner(System.in);
    for(int i=0;i < numOfNumbers; i++){
        System.out.print("Enter a number: ");
        array[i] = keyboard.nextDouble();
    }
}

2 个答案:

答案 0 :(得分:0)

readAndFill方法需要两个参数。 用下面的内容替换main()中的方法调用。

readAndFill(array,numOfNumbers);

答案 1 :(得分:0)

以这种方式readAndFill(array , numOfNumbers);

调用函数