比较Array的元素无法正常工作

时间:2014-12-06 19:58:27

标签: java arrays

我被卡在评估"大于先前数字"的if语句上。部分。 这是我的代码:

class ifBiggerPrint 
{
    public static void main (String []args) 
    {
        double numbers[] = {2,4,6,9,5,4,5,3,1,5,6,2,1}; 
        System.out.print("The numbers in the array which are bigger than the preceding number are: \n");
        for (int i = 0; i < numbers.length; i++)
        {
            // if statement checks that number is greater than
            // previous number in array?? Cant figure...
            if (numbers [i] > numbers [i=+1])
            {
                System.out.println( numbers[i] + "  ");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将其更改为

for (int i = 1; i < numbers.length; i++)
// Start from the 1st element and compare 1st element with 0th element.
{
     if (numbers [i] > numbers [i-1])  
     {
        System.out.println( numbers[i] + "  ");
     }
}