1. 程式人生 > >Java基礎知識複習(一)

Java基礎知識複習(一)

一、算術操作符

1.練習-求和:

            要求: 使用Scanner從控制檯獲取兩個數字,然後計算這兩個數字的和

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		
		Scanner s = new Scanner(System.in);
		System.out.print("輸入第一個整數:");
		int a = s.nextInt();//讀取整數
		System.out.print("輸入第二個浮點數:");
		float b = s.nextFloat();//讀取浮點數
		float sum = a+b;//結果為float
		System.out.println("結果為:"+sum);
		/**
		 * 需要注意的是,如果在通過nextInt()讀取了整數後,再接著讀取字串,讀出來的是回車換行:"\r\n",
		 * 因為nextInt僅僅讀取數字資訊,而不會讀取回車換行"\r\n".
		*所以,如果在業務上需要讀取了整數後,接著讀取字串,那麼就應該連續執行兩次nextLine(),
		*第一次是取走回車換行,第二次才是讀取真正的字串
		**/
		
		System.out.print("輸入字串:");
		String kong = s.nextLine();
		String string = s.nextLine();
		System.out.print("讀取字串:"+string);
		
	}

}

2.任意運算單元的長度超過int,小於int

如果有任何運算單元的長度超過int,那麼運算結果就按照最長的長度計算 
比如 
int a = 5; 
long b = 6; 
a+b -> 結果型別是long

如果任何運算單元的長度都不超過int,那麼運算結果就按照int來計算 
byte a = 1; 
byte b= 2; 
a+b -> int 型別


public class Test2 {

	public static void main(String[] args) {
		int a = 5;
        long b = 6;
        int c = (int) (a+b); //a+b的運算結果是long型,所以要進行強制轉換,否則會報錯
        long d = a+b;
        
        byte e= 1;
        byte f= 2;
        byte g = (byte) (e+f); //雖然a b都是byte型別,但是運算結果是int型別,需要進行強制轉換,否則會報錯
        int h = e+f;
        
         
	}

}

二、邏輯操作符

1.長路與 和 短路與


public class Test3 {

	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);    

	}

}

2.練習-邏輯操作符


public class Test4 {

	public static void main(String[] args) {
		int i = 1;
		boolean b = !(i++ == 3) ^ (i++ ==2) && (i++==3);
		System.out.println(b);
		System.out.println(i);

	}

}

三、三元操作符

1.練習-判斷是否是工作日

 通過Scanner輸入一個1-7之間的整數,使用三元操作符判斷是工作日還是週末?

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		System.out.print("今天星期幾:");
		Scanner s = new Scanner(System.in);
		String b = null;
		int a = s.nextInt();
		if(a>=1 && a<=7) {
			 b = a < 5 ? "工作日" : "週末";
		}else {
			System.err.println("請輸入正確的數字");
		}
		System.out.println("今天是"+b);
	}

}

四、控制流程

1.if 使用過程中可能遇到的坑

在第6行,if後面有一個分號; 而分號也是一個完整的表示式
如果b為true,會執行這個分號,然後列印yes
如果b為false,不會執行這個分號,然後列印yes
這樣,看上去無論如何都會列印yes


public class Test6 {
	public static void main(String[] args) {
        boolean b = false;
        
        if (b);
            System.out.println("yes");
	}
}

2.continue


public class Test7 {
   //列印單數
	public static void main(String[] args) {
		for(int i = 0; i < 20; i++) {
			if(i % 2 ==0) continue;//如果是雙數,後面的程式碼不執行,直接進行下一次迴圈
			System.out.println(i);
		}
	}

}

3.break


public class Test7 {
   //列印雙數
	public static void main(String[] args) {
		for(int i = 0; i < 20; i++) {
			if(i % 2 == 1) break;//如果是單數,直接結束迴圈
			System.out.println(i);
		}
	}

}

4.使用標籤結束外部迴圈


public class Test7 {
	public static void main(String[] args) {
		 //列印單數    
        outloop: //outloop這個標示是可以自定義的比如outloop1,ol2,out5
        for (int i = 0; i < 10; i++) {            
            for (int j = 0; j < 10; j++) {
                System.out.println(i+":"+j);
                if(j%2 == 0) 
                    break outloop; //如果是雙數,結束外部迴圈
            }
             	
        }
	}

}