有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从不同数组填充数组时出现JAVA程序错误

当从inputList填充oddList、evenList和negativeList时,程序只使用一个int填充它,而不是使用inputList数组中所有对应的int。输出应该是每个数组中的一个列表,其编号对应于其标题。用户将数字输入到inputList数组中,然后从那里确定它是奇数、偶数还是负数,然后填充相应的数组。 即,evenList由inputList中的偶数整数填充

公共类项目Tenone {

public static void main(String[] args)
{
    int[] inputList = new int[10];
    int[] oddList = null;
    int[] evenList = null;
    int[] negativeList = null;
    int evenCount = 0;
    int oddCount  = 0;
    int negCount  = 0;


    Scanner input = new Scanner(System.in);

    //System.out.println("Enter any ten integers: ");

    for(int list = 0; list < inputList.length; list++)
    {
        System.out.println("Enter any " + (inputList.length - list) +  " integers: ");
        inputList[list] = input.nextInt();

    }
    System.out.println();
    System.out.println("The numbers you entered: ");

    for(int in = 0; in < inputList.length; in++)
    {
        System.out.println(inputList[in]);
    }


    for(int ls = 0; ls< inputList.length; ls++)
    {
        if(inputList[ls] % 2 == 0)
        {
            evenCount = evenCount +1;
        }
        if(inputList[ls] % 2 != 0)
        {
            oddCount = oddCount +1;
        }
        if(inputList[ls] < 0)
        {
            negCount = negCount +1;
        }
    }

    evenList     = new int[evenCount];
    oddList      = new int[oddCount];
    negativeList = new int[negCount];

    for(int l = 0; l < inputList.length; l++)
    {
        if((inputList[l] % 2) == 0)
        {   
            for(int j = 0; j < evenList.length; j++)
            {
                evenList[j] = inputList[l];

            }
        }
        if((inputList[l] % 2) != 0)
        {
            for(int k = 0; k < oddList.length; k++)
            {
                oddList[k] = inputList[l];

            }
        }
        if(inputList[l] < 0)
        {
            for(int h = 0; h < negativeList.length; h++)
            {
                negativeList[h] = inputList[l];

            }
        }
    }

    System.out.println("The ODD List is: ");
    for(int i = 0; i < oddList.length; i++)
    {
        System.out.println(oddList[i]);
    }

    System.out.println("The EVEN List is: ");
    for(int j = 0; j < evenList.length; j++)
    {
        System.out.println(evenList[j]);
    }

    System.out.println("The NEGATIVE List is: ");
    for(int k = 0; k < oddList.length; k++)
    {
        System.out.println(negativeList[k]);
    }
}

}


共 (0) 个答案