gpt4 book ai didi

java - 将数组传递给函数时.class 出现预期错误

转载 作者:行者123 更新时间:2023-12-01 14:20:06 25 4
gpt4 key购买 nike

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 预期错误。任何帮助将不胜感激。

最佳答案

此行中不需要 [],而是

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

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

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

关于java - 将数组传递给函数时.class 出现预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com