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級的錯誤不同
我一直在google搜索,沒有運氣的解決方案。
本聲明:'賠率= s1.getAllOdds();根據你申報'getAllOdds'的方式,即'是不正確的,其期待3個參數。 – nbro
已經有一個解決方案,但你也通過一個對象訪問一個靜態方法,要麼訪問getAllOdds,如「OddsAndEvens.getAllOdds(arr,count,n)」,要麼刪除靜態修改器 – dbrown93