9.29判断变量x是奇数还是偶数

方法一:

 1 package qqq;
 2 
 3 public class Jiou {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int x=233;
 8         switch(x%2)//取x除以2的余数
 9         {
10         case 0://余数为0则x为偶数
11             System.out.println("x是偶数");
12         break;
13         default://否则为奇数
14             System.out.println("x是奇数");
15         }//switch分支判断语句
16     }
17 }

 

方法二:

 1 package qqq;
 2 
 3 public class Jiou {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub    
 7         int x=45;
 8         System.out.println(x%2==0?"偶数":"奇数");
 9     }
10 }

方法三:

 1 package qqq;
 2 
 3 public class Jiou {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub    
 7         int x=2;
 8         if(x%2==0)
 9         {
10             System.out.println("x是偶数");
11         }
12         else
13         {
14             System.out.println("x是奇数");
15         }
16     }
17 }

 

posted @ 2015-09-29 16:48  dirgo  阅读(1087)  评论(0编辑  收藏  举报