0
public class AssignmentChapter8
{
    public static void main(String[] args)
    {
        int randomNumbers = new int[100];
        int counter = 0;

        while(counter < randomNumbers.length)
        {
            randomNumbers[counter] = (int)(Math.random() * 25);
            counter++;
        }

        int oddNumbers[] = new int[100];
        oddNumbers[] = getOddNumbers(randomNumbers);
        int evenNumbers[] = new int[100];
        evenNumbers[] = getEvenNumbers(randomNumbers);

        System.out.println("The odd numbers are:");

        for(int k = 0; k < oddNumbers.length; k++)
            System.out.print("\t" + oddNumbers[k]);

        System.out.println("The even numbers are:");

        for(int l = 0; l < evenNumbers.length; l++)
            System.out.print("\t" + evenNumbers[l]);
    }
    public static int getOddNumbers(int randomNumbers)
    {
        int oddNumbers[] = new int[100];
        int counterA = 0;
        int counterB = 0;
        int counter = 0;
        int placeholder;

        while(counter < randomNumbers.length)
        {
            if(randomNumbers[counterA] % 2 > 0)
            {
                oddNumbers[counterB] = randomNumbers[counterA];
                counterB++;
            }

            counterA++;
            counter++;
        }   

        return oddNumbers;
    }
    public static int getEvenNumbers(int randomNumbers)
    {
        int evenNumbers[] = new int[100];
        int counterA = 0;
        int counterB = 0;
        int counter = 0;
        int placeholder;

        while(counter < randomNumbers.length)
        {
            if(randomNumbers[counterA] % 2 > 0)
            {
                evenNumbers[counterB] = randomNumbers[counterA];
                counterB++;
            }

            counterA++;
            counter++;
        }

        return evenNumbers;
    }
}   

我一直在尝试执行一个程序来对数组中的变量进行排序,但我一直得到一个';' 程序应该从函数中检索数组的数组声明后的行中的预期错误。任何帮助,将不胜感激。

4

2 回答 2

1

This is bad syntax (which causes the ';' expected error ):

oddNumbers[] = getOddNumbers(randomNumbers);

The brackets are not needed. You can do this:

oddNumbers = getOddNumbers(randomNumbers);

Besides that, you have plenty of errors:

int randomNumbers[] = new int[100]; // you need the brackets

Your return value in the method declaration is wrong (you are returning an array, not an int):

public static int[] getEvenNumbers(int randomNumbers)
于 2013-07-10T01:55:38.500 回答
0
   int oddNumbers[] = new int[100];

通过初始化oddNumbers成为一个数组。当您要分配值时,

    oddNumbers={elements of array}

左右两边都应该是数组。

也许您应该使用 IDE 进行编码,那么它可能会帮助您理解一些这样的问题。

于 2013-07-10T02:40:06.427 回答