不同类中的方法不能应用于给定类型

时间:2016-01-09 21:38:11

标签: java arrays class constructor

主要课程:

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不能应用于给定的类型; odds = s1.getAllOdds(); ^
需要:INT [],INT,INT
发现:没有参数原因:实际和形式参数列表的长度1错误

我一直在谷歌搜索没有运气的解决方案。

1 个答案:

答案 0 :(得分:0)

要调用此方法getAllOdds(),您需要3个参数,请参阅:

 public static int[] getAllOdds(int[] a,int count,int n){...