【发布时间】:2016-01-09 21:38:11
【问题描述】:
主类:
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.println("Enter no. of elements you want in array:");
n = s.nextInt();
while(n!=69)
{
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
int[]odds;
OddsAndEvens s1 = new OddsAndEvens();
odds = s1.getAllOdds();
System.out.print("Odds- ");
System.out.print( Arrays.toString(odds));
System.out.println(" ");
System.out.println("");
System.out.println("Evens- ");
System.out.println(" ");
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
}
}
}
中学班:
public class OddsAndEvens
{
private static int countEm(int[] a, int n,boolean odd,int count, int anticount)
{
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 != 0)
{
count++;
}
anticount++;
}
return 0;
}
public static int[] getAllEvens(int[] a,int anticount,int n)
{
int[]gotevens = new int[anticount];
for(int i = 0 ; i < n ; i++)
{
int toc = 0;
if(a[i] % 2 == 0)
{
int a2 = a[i];
gotevens[toc] = a2;
toc++;
}
}
return gotevens;
}
public static int[] getAllOdds(int[] a,int count,int n)
{
int[]gotodds = new int[count];
for(int i = 0 ; i < n ; i++)
{
int tic = 0;
if(a[i] % 2 != 0)
{
int a1 = a[i];
gotodds[tic] = a1;
tic++;
}
}
return gotodds;
}
}
我不断收到以下错误。
G:\MyProjects\Arraysoddsevens\OddsAndEvensRunner.java:33:错误:类 OddsAndEvens 中的方法 getAllOdds 不能应用于给定类型;赔率 = s1.getAllOdds();
^
必需:int[],int,int
发现:无参数原因:实际参数列表和形式参数列表长度不同 1 错误
我一直在用谷歌搜索没有运气的解决方案。
【问题讨论】:
-
此声明:
odds = s1.getAllOdds();根据您声明getAllOdds的方式不正确,即需要 3 个参数。 -
已经有解决方案了,不过你也是通过对象访问静态方法,要么访问getAllOdds比如“OddsAndEvens.getAllOdds(arr, count, n)”,要么去掉static修饰符跨度>
标签: java arrays class constructor