Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

java - method in class cannot be applied to given types

I'm creating a program that generates 100 random integers between 0 and 9 and displays the count for each number. I'm using an array of ten integers, counts, to store the number of 0s, 1s, ..., 9s.)

When I compile the program I get the error:

RandomNumbers.java:9: error: method generateNumbers in class RandomNumbers cannot be applied to given types;
generateNumbers();

required: int[]

found:generateNumbers();

reason: actual and formal argument lists differ in length

I get this error for the lines of code that I call the methods generateNumbers() and displayCounts() in the main method.

    public class RandomNumbers {

       public static void main(String[] args) {

            //declares array for random numbers
        int[] numbers = new int [99];

        //calls the generateNumbers method
        generateNumbers();

        //calls the displayCounts method        
        displayCounts();
    }

    //***************************************************************** 

    private static int generateNumbers(int[] numbers){

        for(int i = 0; i < 100; i++){
            int randomNumber;
            randomNumber = (int)(Math.random() *10);
            numbers[i] = randomNumber;
        return randomNumber;
        }

    }

    //***************************************************************** 

    private static void displayCounts(int[] numbers){
        int[] frequency = new int[10];

        for(int i = 0, size = numbers.length; i < size; i++ ){
            System.out.println((i) + " counts = " + frequency[i]);
        }

    }//end of displayCounts

    }//end of class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

generateNumbers() expects a parameter and you aren't passing one in!

generateNumbers() also returns after it has set the first random number - seems to be some confusion about what it is trying to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...