移位運算+字符串連接+流程控制

**

移位運算

**
" >> ":右移運算符,將左操作數向右移動,移位個數由右操作數決定
" << ":左移運算符,將左操作數向左移動,移位個數由右操作數決定
" >>> ":無符號左移位運算符

a>>b通用公式: a/(2^b%32)

移位運算符性質
適用數據類型:byte、short、char、int、long,對低於int型的操作數將先自動轉換爲int型再移位
對於int型整數移位a>>b,系統先將b對32取模,得到的結果纔是真正移位的位數
對於long型整數移位時a>>b ,則是先將移位位數b對64取模
例子如:
移位運算
**

字符串連接

**

double x = 9.987;
		double y = 1;
		double t = x+y;
		String s = "Price is:"+x;//3個字符串常量對象“price is”、”9.987”、“price is 9.987”
		String st = "Total Price is:"+t;//3個字符串常量對象分別是 "Total Price is:"、
       “10.987”、"Total Price is:10.987 "
		System.out.println(s);		
		System.out.println(st);
		System.out.println(""+x+y);//5個對象,分別是“”、”9.987”、“9.987”、”1.0”、 “9.9871.0”
		System.out.println(x+y+"");//3個對象 分別是“10.987”、 “”、“10.987”

**

流程控制

**
順序
分支語句
1.If,else

例:

if (i < 50){
System.out.println("* The input number is less than 50! *");}
else if(i==50){			
System.out.println("* The Input number is equal to 50!  *");}
else{			
System.out.println("*The input number is greater than 50*");}

(誰成立就輸出誰)

2.Switdh(裏面不能爲long,這裏只能放byte,short,char,int類型)

例:

Switch(int)://不能是long
  Case 1:    break;
  Case2:     break;
  Default  .....

循環語句
Java中的循環語句:for、dowhile、while

while和do while區別:wihle是隻要符合條件就執行,dowhile是至少執行一次,先執行一次在判斷是否符合條件
Breake 和continue:breake是跳出本層循環,continue是跳出本次循環進行下一次循環

break語句和標籤(label)的結合可以用來指定從多個嵌套循環的某個循環中跳出。
例子:

 outer:
      for(int i = 0;i<10;i++)
      {
        System.out.println("Outer loop:");
        inner:
          while(true)
          {//從鍵盤緩衝區讀取一個字符的ascii碼
            int k = System.in.read();
            System.out.println("Inner Loop:"+k);
            if(k=='b') break inner;//跳出inner所標記的循環
            if(k=='q') break outer;//跳出outer標記的循環
          }
      } 

**

注意:

**
·For:先判斷,後執行
·While:先判斷,後執行(for的簡寫)
·Dowhile:先執行,後判斷(在循環條件一開始爲真時,while和dowhile是一樣的,若循環條件一開始爲假,while一次都不執行,dowhile 會執行一次)
·Break:跳出當前循環
·Continue:循環不結束,只是跳出本次循環
·Break+標籤label:跳出label所標識的那層循環,注意非正常出口

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章