`
黑马程序员-muma
  • 浏览: 3917 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

黑马程序员___位运算、if、switch

 
阅读更多

------- android培训java培训、期待与您交流! --------

 

黑马程序员___位运算、ifswitch

左移:3<<2 就是3乘以22次幂

 

         m<<n 就是m乘以2n次幂

 

右移:m>>n 就是m除以2n次幂

 

无符号右移:>>>无论你的最高位是0还是1都拿0补空位。

 

         右移:>>最高位补什么由原有的数据最高位值而定,如果最高位是0,右移后用0补空位;如果最高位是1,右移后用1补空位。

 

eg: class Demo

 

{

 

       public static void main(String[] args)

 

       {

 

              int i,a;

 

              i = -6>>>2;

 

              System.out.println("i="+i);

 

              a = -6>>2;

 

              System.out.println("a="+a);

 

       }

 

} //结果:i=1073741822

 

a=-2

 

Day 0214

 

^:一个数异或同一个数2次,结果还是那个数。//用于加密

 

Day 0215

 

位运算练习 1、最有效的方式算出2乘以8等于几?  2<<3

 

2、对两个整数变量的值进行互换(不需要第三方变量)

 

eg: class Demo // 技巧型方式开发一般不用

 

{

 

       public static void main(String[] args)

 

       {

 

              int n = 2, m = 3;

 

              System.out.println("n="+n+",m="+m);

 

              n = n ^ m;

 

              m = m ^ n;

 

              n = n ^ m;

 

              System.out.println("n="+n+",m="+m);

 

       }

 

}结果:n=2,m=3

 

n=3,m=2

 

Day 0216

 

&>>换算练习

 

三元运算符:(条件表达式)?表达式1:表达式2;

 

eg

 

class Demo

 

{

 

       public static void main(String[] args)

 

       {   //三元运算符

 

int x = 3,y;

 

              y = (x>1)?100:200;

 

              System.out.println("y="+y);

 

 

 

              // &>>换算练习

 

              int mun = 60,n1,n2;

 

              //获取60的最低4位,通过&15

 

              n1 = mun & 15;

 

              System.out.println(n1>9?(char)(n1-10+'A'):n1);

 

              //60右移4位,获取下一个4

 

              int temp = 60 >> 4;

 

              //通过&15获取temp的最低4

 

              n2 = temp & 15;

 

              System.out.println(n2>9?(char)(n2-10+'A'):n2);

 

       }

 

}

 

Day 0217

 

程序流程控制

 

如果if控制的语句只有一条大括号可以不写

 

If else 结构简写格式:变量=(条件表达式)?表达式1:表达式2

 

三元运算符:

 

       好处:可以简化if else 代码。

 

       弊端:因为是一个运算符,所以运算符必须有一个结果。

 

eg

 

class Demo

 

{

 

       public static void main(String[] args)

 

       {

 

             

 

              //如果if控制的语句只有一条大括号可以不写

 

              //以下程序是一个整体(if语句的第三种形式)满足适合一个就结束

 

              int i = 3;

 

              if (i>1)

 

              {

 

                     System.out.println("a");

 

              }

 

              else if (i>2)

 

              {

 

                     System.out.println('b');

 

              }

 

              else if (i>3)

 

              {

 

                     System.out.println('c');

 

              }

 

              else

 

                     System.out.println("d");

 

 

 

              System.out.println("over");

 

              结果:a

 

              */

 

 

 

              //以下是3if语句

 

              int i = 3;

 

              if (i>1)

 

              {

 

                     System.out.println("a");

 

              }

 

              if (i>2)

 

              {

 

                     System.out.println('b');

 

              }

 

              if (i>3)

 

              {

 

                     System.out.println('c');

 

              }

 

              else

 

                     System.out.println("d");

 

 

 

              System.out.println(5%-5);//毕老师讲的那个结果看%的左边是在非整除的条件下。

 

       }

 

}  结果:a

 

b

 

d

 

0

 

Day 0218

 

If 练习

 

//需求:根据用于指定月份,打印该月份所属的季节。

 

class Demo

 

{

 

       public static void main(String[] args)

 

       {

 

              int x = 4;

 

              if(x==3||x==4||x==5)

 

                     System.out.println(x+"春节");

 

              else if(x==6||x==7||x==8)

 

                     System.out.println(x+"夏季");

 

              else if(x==9||x==10||x==11)

 

                     System.out.println(x+"秋季");

 

              else if(x==12||x==1||x==2)

 

                     System.out.println(x+"冬季");

 

/*以下是个人认为比较好的方式

 

              if(x>12&&x<1)

 

                     System.out.println(x+"月份不存在");

 

              else if(x>=3&&x<=5)

 

                     System.out.println(x+"春季");

 

              else if(x>=6&&x<=8)

 

                     System.out.println(x+"夏季");

 

              else if(x>=9&&x<=11)

 

                     System.out.println(x+"秋季");

 

              else

 

                     System.out.println(x+"冬季");

 

//以下是switch语句

 

int x = 4;

 

              switch (x)

 

              {

 

              case 3:

 

              case 4:

 

              case 5:

 

                     System.out.println(x+"春季");

 

                     break;

 

              case 6:

 

              case 7:

 

              case 8:

 

                     System.out.println(x+"春季");

 

                     break;

 

              case 9:

 

              case 10:

 

              case 11:

 

                     System.out.println(x+"春季");

 

                     break;

 

              case 12:

 

              case 1:

 

              case 2:

 

                     System.out.println(x+"春季");

 

                     break;

 

              default:

 

                     System.out.println("不符合月份");

 

                     //break;

 

              }

 

       }

 

}

 

*/

 

Day 0219

 

选择结构

 

Switch语句

 

格式:

 

       switch(表达式)//就接受byte short int char

 

       {

 

       case 取值1

 

              执行语句;

 

              Break;

 

       case 取值2

 

              执行语句;

 

              Break;

 

              、、、、、、

 

       case 取值n

 

              执行语句;

 

              Break;

 

       default :

 

              执行语句;

 

              break;

 

}

 

执行循序固定,default 的位置可以随便只有表达式都不符合所有的case才会执行default

 

Switch有两种结束情况1、执行到break 2、是执行到switch的大花括号。

 

eg

 

class Demo

 

{

 

       public static void main(String[] args)

 

       {

 

              int x = 3;

 

              switch (x)

 

              {

 

              default:

 

                     System.out.println('e');

 

                     //break;

 

              case 2:

 

                     System.out.println('a');

 

                     //break;

 

              case 4:

 

                     System.out.println('b');

 

                     break;

 

              case 5:

 

                     System.out.println('c');

 

                     break;

 

              case 6:

 

                     System.out.println('d');

 

                     break;

 

              }

 

       }

 

} 结果:e

 

a

 

b

 

ifswitch语句很像。

 

 如果判断的具体数值不多,而是符合byte short int char 这四种类型。虽然两个语句都可以使用,建议使用switch语句,因为效率高。

 

其他情况:对于区间判断,对结果为boolean类型判断,使用ifif的使用范围更广。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics