Java初級(三)
操作符
-
算數操作符
public class HelloWorld { public static void main(String[] args) { int i = 1; int j = 1; // +表示求和 int a = i+j; // -表示就差 int b = i-j; // *表示求積 int c = i*j; // /表示求商 int d = i/j; // %表示求餘,也叫取模 int e = i%j; // ++表示自增 --表示自減 //置後表示先取值,再運算;置前表示先運算,再取值 //結果爲1 i++; //結果爲2 ++i; //結果爲1 j--; //結果爲0 --j; //長度不相同的運算單元的運算結果就按照最長的長度計算 int ii = 1; long ll = 1; //ll的長度比ii的長度長,所以ii+ll的運算結果爲long型 long li = ii + ll; //ii+ll的運算結果爲long型,只能強制轉換爲int型 int il = (int)(ii+ll); //報錯:cannot convert from long to int(不能將long轉換成int) int iil = ii+ll; } }
-
關係操作符
public class HelloWorld { public static void main(String[] args) { int a = 5; int b = 6; int c = 5; // > 大於 // >= 大於或等於 // < 小於 // <= 小於或等於 // == 是否相等 // != 是否不等 // 返回 false System.out.println(a > b); // 返回 true System.out.println(a >= c); // 返回 true System.out.println(a <= c); // 返回 false System.out.println(a < c); // 返回 false System.out.println(a == b); // 返回 true System.out.println(a != b); } }
-
邏輯運算符
public class HelloWorld { public static void main(String[] args) { //&(長路與) 無論第一個表達式的值是true或者false,第二個的值,都會被運算 int i = 2; System.out.println( i== 1 & i++ ==2 ); //無論如何i++都會被執行,所以i的值變成了3 System.out.println(i); //&&(短路與) 只要第一個表達式的值是false的,第二個表達式的值,就不需要進行運算了 int j = 2; System.out.println( j== 1 && j++ ==2 ); //因爲j==1返回false,所以右邊的j++就沒有執行了,所以j的值,還是2 System.out.println(j); //|(長路或) 無論第一個表達式的值是true或者false,第二個的值,都會被運算 int i = 2; System.out.println( i== 1 | i++ ==2 ); //無論如何i++都會被執行,所以i的值變成了3 System.out.println(i); //||(短路或) 只要第一個表達式的值是true的,第二個表達式的值,就不需要進行運算了 int j = 2; System.out.println( j== 2 || j++ ==2 ); //因爲j==2返回true,所以右邊的j++就沒有執行了,所以j的值,還是2 System.out.println(j); //!(取反)真變爲假,假變爲真 boolean b = true; //輸出true System.out.println(b); //輸出false System.out.println(!b); //^(異或),同真異假 boolean a = true; boolean b = false; //不同返回真 System.out.println(a^b); //相同返回假 System.out.println(a^!b); } }
-
位操作符
public class HelloWorld { public static void main(String[] args) { //位操作是對於二進制而言的,需要通過Integer.toBinaryString()方法,將十進制整數轉換位二進制字符串。 //255的二進制的表達11111111 String s = Integer.toBinaryString(255); //輸出:11111111 System.out.println(b); //2的二進制爲10 String s1 = Integer.toBinaryString(2); //3的二進制爲11 String s1 = Integer.toBinaryString(3); //|(位或)對每一位進行或運算,輸出:3 System.out.println(2|3); //&(位與)對每一位進行與運算,輸出:2 System.out.println(2&3); //^(異或)對每一位進行異或運算,輸出:1 System.out.println(2^3); //~(取非)對每一位進行取反運算,輸出:-4 System.out.println(~3); //<<(左移)對每一位都向左移動,最右邊一位補0,輸出:6 System.out.println(3<<1); //>>(右移)對每一位都向右移動,輸出:1 System.out.println(3>>1); //帶符號右移 >>,對於正數, 帶符號右移 >> 會把所有的位右移,並在左邊補0,對於負數, 帶符號右移 >> 會把所有的位右移,並在左邊補1,輸出:-2 System.out.println(-3>>1); //無符號右移 >>>,如果是一個負數,那麼對應的二進制的第一位是1,無符號右移>>>會把第一位的1也向右移動,導致移動後,第一位變成0,這樣就會使得負數在無符號右移後,得到一個正數,輸出:2147483646 System.out.println(-3 >>> 1); } }
-
賦值操作符
public class HelloWorld { public static void main(String[] args) { int i = 1;//=表示賦值操作 i+=1;//等同於i=i+1;下面的表達式類同 i-=1; i*=1; i/=1; i%=1; i&=1; i|=1; i^=1; i<<=1; i>>=1; i>>>=1; } }
-
三元操作符
public class HelloWorld { public static void main(String[] args) { //?:(表達式?x1:x2),如果表達式爲真,返回x1,如果表達式爲假,返回x2 int i = 1; int j = 2; int k = i < j ? 99 : 88; // 相當於 if (i < j) { k = 99; } else { k = 88; } //輸出:99 System.out.println(k); } }
Scanner類
-
讀取整數
import java.util.Scanner;//導入這個類,才能使用Scanner類 public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int a = s.nextInt();//讀取整數 } }
-
讀取浮點數
import java.util.Scanner;//導入這個類,才能使用Scanner類 public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); float a = s.nextFloat();//讀取浮點數 double b = s.nextDouble();//讀取浮點數 } }
-
讀取字符串
import java.util.Scanner;//導入這個類,才能使用Scanner類 public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); String a = s.nextLine();//讀取字符串 } }
-
讀取了整數後,接着讀取字符串
import java.util.Scanner;//導入這個類,才能使用Scanner類 //需要注意的是,如果在通過nextInt()讀取了整數後,再接着讀取字符串,讀出來的是回車換行:"\r\n",因爲nextInt僅僅讀取數字信息,而不會讀取回車換行"\r\n". //所以,如果在業務上需要讀取了整數後,接着讀取字符串,那麼就應該連續執行兩次nextLine(),第一次是取走回車換行,第二次纔是讀取真正的字符串 public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int i = s.nextInt();//讀取整數 String rn = s.nextLine();//讀取回車換行"\r\n" String a = s.nextLine();//讀取字符串 } }
循環
-
if,if、else,else if
public class HelloWorld { public static void main(String[] args) { boolean b = true; //如果成立就打印yes if(b){ System.out.println("yes"); } //如果有多個表達式,必須用大括弧包括起來 if(b){ System.out.println("yes1"); System.out.println("yes2"); System.out.println("yes3"); } //否則表達式2 3 無論b是否爲true都會執行 if(b) System.out.println("yes1"); System.out.println("yes2"); System.out.println("yes3"); //如果只有一個表達式可以不用寫括弧,看上去會簡約一些 if(b){ System.out.println("yes1"); } if(b) System.out.println("yes1"); //if後面有一個分號; 而分號也是一個完整的表達式 //如果b爲true,會執行這個分號,然後打印yes //如果b爲false,不會執行這個分號,然後打印yes if (b); System.out.println("yes"); //else 代表不成立的情況 else System.out.println("no"); //else if 是多條件判斷 //如果只使用 if,會執行4次判斷 int i = 2; if (i==1) System.out.println(1); if (i==2) System.out.println(2); if (i==3) System.out.println(3); if (i==4) System.out.println(4); //如果使用else if (i==2)判斷成立, else if (i==3)和else if (i==4)的判斷就不會執行了,節約了運算資源 if (i==1) System.out.println(1); else if (i==2) System.out.println(2); else if (i==3) System.out.println(3); else if (i==4) System.out.println(4); } }
-
switch
public class HelloWorld { public static void main(String[] args) { //如果使用if else int day = 5; if (day==1) System.out.println("星期一"); else if (day==2) System.out.println("星期二"); else if (day==3) System.out.println("星期三"); else if (day==4) System.out.println("星期四"); else if (day==5) System.out.println("星期五"); else if (day==6) System.out.println("星期六"); else if (day==7) System.out.println("星期天"); else System.out.println("這個是什麼鬼?"); //switch可以使用byte,short,int,char,String,enum //注: 每個表達式結束,都應該有一個break; //注: String在Java1.7之前是不支持的, Java從1.7開始支持switch用String的,編譯後是把String轉化爲hash值,其實還是整數 //注: enum是枚舉類型,在枚舉章節有詳細講解 //如果使用switch switch(day){ case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; case 7: System.out.println("星期天"); break; default: System.out.println("這個是什麼鬼?"); } } }
-
while,do while
public class HelloWorld { public static void main(String[] args) { //只要while中的表達式成立,就會不斷地循環執行 while(true){ System.out.println("while"); } //do while與while的區別是,無論是否成立,先執行一次,再進行判斷 //與while的區別是,無論是否成立,先執行一次,再進行判斷 do{ System.out.println("do while"); } while(true); } }
-
for
public class HelloWorld { public static void main(String[] args) { //使用for打印1次 for (int j = 0; j < 1; j++) { System.out.println("for"); } } }
-
continue
public class HelloWorld { public static void main(String[] args) { for (int j = 0; j < 10; j++) { if(0==j%2) continue; //如果是雙數,後面的代碼不執行,直接進行下一次循環 System.out.println(j);//如果是單數,打印j } } }
-
break
public class HelloWorld { public static void main(String[] args) { for (int j = 0; j < 10; j++) { if(0==j%2) break; //如果是雙數,直接結束循環 //直接結束當前for循環 System.out.println(j);////如果是單數,打印j } //藉助boolean變量結束外部循環,需要在內部循環中修改這個變量值,每次內部循環結束後,都要在外部循環中判斷,這個變量的值 boolean breakout = false; //是否終止外部循環的標記 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.println(i + ":" + j); if (0 == j % 2) { breakout = true; //終止外部循環的標記設置爲true break;//終止for (int j = 0; j < 10; j++) } } if (breakout) //判斷是否終止外部循環 break;//終止for (int i = 0; i < 10; i++) } } }
-
結束標籤
public class HelloWorld { public static void main(String[] args) { //在外部循環的前一行,加上標籤,在break的時候使用該標籤,即能達到結束外部循環的效果 exit: //outloop這個標籤是可以自定義的比如quit,out,exit for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.println(i+":"+j); if(0==j%2) break exit; //如果是雙數,結束外部循環 } } } }