如何使java输出数字递减顺序

时间:2016-11-08 02:41:21

标签: java

到目前为止,这是我的代码,我需要将它从66输出到11.现在它从11到66。

public class ListNumbers {

    public static void main(String[] args) {

            //define limit
            int limit = 66;

            System.out.println("Printing Even numbers between 1 and " +      limit);

            for(int x=11; x <= limit; x++){
             if(x != 44 && x != 22){
                            System.out.print(x + " ");
                    }
          }
    }
}

2 个答案:

答案 0 :(得分:5)

你只需要从限制开始,到达11时结束

ViewGroup

答案 1 :(得分:1)

您以上限(66)开始循环并向下递减到下限(1)。然后检查它们是even还是odd,但只输出偶数。

    int lowerLimit = 1;
    int upperLimit = 66;
    System.out.println("Printing Even numbers between " + lowerLimit + " and " + upperLimit);

    for (int x = upperLimit; x >= lowerLimit; x--)
    {
        if ((x & 1) == 0)
        {
            // even
            System.out.print(x + " ");
        }
        else
        {
            // odd
        }
    }