if -else 语句

 if -else 标准语句。 

public class Demo03IfElse {
             public static void main(String[] args) {
                   int num = 666;

                   if (num % 2 == 0) { // 如果除以2能够余数为0,说明是偶数
                       System.out.println("偶数");
                    } else {
                       System.out.println("奇数");
                   }
            }
}

 

 

 

if- else 扩展语句

---------------------------------------------------------------------------------

 

// x和y的关系满足如下:
// 如果x >= 3,那么y = 2x + 1;
// 如果-1 < x < 3,那么y = 2x;
// 如果x <= -1,那么y = 2x – 1;


public class Demo04IfElseExt {
              public static void main(String[] args) {
                    int x = -10;
                    int y;
                    if (x >= 3) {
                        y = 2 * x + 1;
                     } else if (-1 < x && x < 3) {
                        y = 2 * x;
                     } else {
                         y = 2 * x - 1;
                     }
                   System.out.println("结果是:" + y);
              }
}

 

posted @ 2020-07-18 10:31  ZHU_FENG  阅读(256)  评论(0)    收藏  举报