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

        for(int x = 0; x < randomNumbers.length; x++)
            randomNumbers[x] = (int)(Math.random() * 25);

        for(int y = 0; y < randomNumbers.length; y++)
            if(randomNumbers[y] % 2 > 0)
                oddCount += 1;
            else
                evenCount+=1;


        int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);
        int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers[]);

        System.out.println();
        System.out.println("The list of odd numbers is:");
        System.out.println();

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

        System.out.println();
        System.out.println("The list of even numbers is:");
        System.out.println();

        for(int b = 0; b < evenNumbers.length; b++)
            System.out.print(evenNumbers[b] + "\t");
    }
    public static int[] getOddNumbers(int oddCount, int randomNumbers[])
    {
        int oddNumbers[] = new int[oddCount];
        int counter = 0;

        for(int x = 0; x < randomNumbers.length; x++)
            if(randomNumbers[x] % 2 > 0)
            {
                oddNumbers[counter] = randomNumbers[x];
                counter++;
            }

        return oddNumbers;
    }
    public static int[] getEvenNumbers(int evenCount, int randomNumbers[])
    {
        int evenNumbers[] = new int[evenCount];
        int counter = 0;

        for(int x = 0; x < evenNumbers.length; x++)
            if(randomNumbers[x] % 2 < 1)
            {
                oddNumbers[counter] = randomNumbers[x];
                counter++;
            }

        return evenNumbers;
    }
}

我是 Java 新手,一直在尝试创建一个程序来生成 100 个数字并对奇偶数进行排序。无论我做什么,该程序甚至都会给出 .class 预期错误。任何帮助,将不胜感激。

4

3 回答 3

1

在这一[]行中不需要,而不是

int randomNumbers[] = new int[100];
// and 
int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);

int[] randomNumbers = new int[100];
// and
int[] oddNumbers = getOddNumbers(oddCount, randomNumbers);

[]是类型的一部分,而不是名称。

于 2013-07-16T20:18:11.920 回答
0

正如 Peter Lawrey 所说,您不需要函数调用上的括号。还有一些其他的编译错误。首先,您的函数中似乎存在复制/粘贴错误getEvenNumbers。在 for 循环内oddNumbers应该是evenNumbers.

您还需要初始化整数oddCountevenCount(可能0)。

于 2013-07-16T20:23:11.953 回答
0

将数组作为一个整体引用时,不要将[]与变量名一起使用。改变

int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);
int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers[]);

int oddNumbers[] = getOddNumbers(oddCount, randomNumbers);
int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers);
于 2013-07-16T20:18:25.090 回答