零基礎Java入門編程【day3】

運算符

邏輯運算符

&—邏輯與
| —邏輯或
!—邏輯非
&& —短路與
|| —短路或
^ —邏輯異或

01

  • 邏輯運算符用於連接布爾型表達式,在Java中不可以寫成3<x<6,應該寫成x>3 & x<6

  • “&”和“&&”的區別:

    • 單&時,左邊無論真假,右邊都進行運算
    • 雙&時,如果左邊爲真,右邊參與運算,如果左邊爲假,那麼右邊不參與運算
      public class Z01 {
      
      	public static void main(String[] args) {
      		int i=0;
      		int k=1;
      		System.out.println(++i !=0 && ++k == 2);
      		System.out.println(++i !=0 & ++k == 2);
      	}
      
      }
      
  • “|”和“||”的區別同理,||表示:當左邊爲真,右邊不參與運算

  • 在不需要邏輯運算兩邊都參與運算的時候,儘量使用&&和||

  • 異或( ^ )與或( | )的不同之處是:當左右都爲true時,結果爲false。
    理解:異或,追求的是“異”!

    public class Z01 {
    
    	public static void main(String[] args) {
    		int i=0;
    		int k=1;
    		System.out.println(++i !=0 || ++k == 2);
    		System.out.println(++i !=0 | ++k == 2);
    	}
    
    }
    

試一試

public class Z02 {

	public static void main(String[] args) {
		//例1
		int x=1;
		int y=1;
		if(x++==2 & ++y==2) {
			x=7;
		}
		System.out.println("x="+x+",y="+y);	//2 2

		// 例2
//		int x=1,y=1;
//		if(x++==1 | ++y==1) {
//			x=7;
//		}
//		System.out.println("x="+x+",y="+y);	// 7 2
		
		//例3
//		int x=1,y=1;
//		if(x++==2 && ++y==2) {
//			x=7;
//		}
//		System.out.println("x="+x+",y="+y);	//2 1
		
		//例4
//		int x=1,y=1;
//		if(x++==1 || ++y==1) {
//			x=7;
//		}
//		System.out.println("x="+x+",y="+y);	//7 1
	}
}

位運算符

12
13

三目運算符

  • 格式:
    (條件表達式)? 表達式1:表達式2;
    • 爲true,運算後的結果是表達式1;
    • 爲false,運算後的結果是表達式2;

運算符的優先級

  • Java 語言中大部分運算符也是從左向右結合的,只有單目運算符、賦值運算符和三目運算符例外,其中,單目運算符、賦值運算符和三目運算符是從右向左結合的,也就是從右向左運算

  • 算術運算符優先級較高,關係和邏輯運算符優先級較低。多數運算符具有左結合性,單目運算符、三目運算符、賦值運算符具有右結合性。
    在這裏插入圖片描述

  • 使用優先級爲 1 的小括號可以改變其他運算符的優先級。

  • 即如果需要將具有較低優先級的運算符先運算,則可以使用小括號將該運算符和操作符括起來。例如下面的表達式:

(x-y)*z/5
  • 在這個表達式中先進行括號內的減法運算,再將結果與 z 相乘,最後將積除以 5 得出結果。整個表達式的順序按照從左向右執行,比較容易理解。

程序流程控制

順序結構

  • 程序從上到下逐行地執行,中間沒有任何判斷和跳轉。

分支結構

  • 根據條件,選擇性地執行某段代碼。
  • 有if…else和switch兩種分支語句。

循環結構

  • 根據循環條件,重複性的執行某段代碼。
  • 有while、do…while、for三種循環語句。
  • 注:JDK1.5之後提供了foreach循環,方便的遍歷集合、數組元素。
  • Java中定義成員變量時採用合法的前向引用。如:
public class Test{
        int num1 = 12;
        int num2 = num1 + 2;
}
錯誤形式:
public class Test{
       int num2 = num1 + 2int num1 = 12;
}

分支語句

if-else語句

if語句三種格式:

1.  if(true){
	執行代碼塊;
     }

2.  if(條件表達式){
	執行代碼塊;
      }
     else{
	執行代碼塊;
      }
3.  if(條件表達式){
	執行代碼塊;
      }
      else if (條件表達式){
	執行代碼塊;
      }
       ……
       else{
	執行代碼塊;
       }
public class Z04 {

	public static void main(String[] args) {
//		int i=1;
//		if(i==1) {
//			System.out.println(i);
//		}
		
//		int i=2;	//判斷是奇數還是偶數,輸出結果
//		if(i%2==0) {
//			System.out.println("偶數");
//		}else {
//			System.out.println("奇數");
//		}
		
		//如果是數字1,輸出星期一,2輸出星期二,3就輸出星期三
//		int i=1;
//		if(i==1) {
//			System.out.println("星期一");
//		}else if(i==2) {
//			System.out.println("星期二");
//		}else if(i==3){
//			System.out.println("星期三");
//		}
		//如果是數字1,輸出星期一,2輸出星期二,3就輸出星期三,如果不是任何一個,則輸出不知道星期幾
		int i=6;
		if(i==1) {
			System.out.println("星期一");
		}else if(i==2) {
			System.out.println("星期二");
		}else if(i==3){
			System.out.println("星期三");
		}else {
			System.out.println("不知道星期幾");
		}
	}

}

例1

public class Z05 {

	public static void main(String args[]){
		int age = 75;	
		if (age< 0) {
			System.out.println("不可能!");
		} else if (age>250) {
			System.out.println("是個妖怪!");
		} else {
			System.out.println("人家芳齡 " + age +" ,馬馬乎乎啦!");
		}
	}
}

例2

//判斷小明的期末成績。
//當成績爲100分時,獎勵一輛BMW;
//當成績爲(80,99]時,獎勵一個臺iphone5s;
//當成績爲[60,80]時,獎勵一本參考書;
//其它時,什麼獎勵也沒有。

public class Z06 {

	public static void main(String args[]){
		int score = 75;	
		if(score==100) {
			System.out.println("獎勵一輛BMW");
		}else if(score>80 && score <=99) {
			System.out.println("獎勵一個臺iphone5s");
		}else if(score >=60 && score <=80) {
			System.out.println("獎勵一本參考書");
		}else {
			System.out.println("什麼獎勵也沒有");
		}
	}
}

switch語句

	switch(變量){
	case 常量1:
		語句1;
		break;
	case 常量2:
		語句2;
		break;
	… …
	case 常量N:
		語句N;
		break;
	default:
		語句;
		break;
	 } 

例1

public class Z07 {

	public static void main(String[] args) {
		int i=2;
		switch(i) {
		case 1:
			System.out.println("星期一");
			break;
		case 2:
			System.out.println("星期二");
			break;		
		}
	}
}

例2

public class Z08 {

	public static void main(String[] args) {
		int i=8;
		switch(i) {
		case 1:
			System.out.println("星期一");
			break;
		case 2:
			System.out.println("星期二");
			break;	
		default:	//等同於else
			System.out.println("不知道星期幾");
			break;
		}
	}
}

例3

public class Z09 {
	public static void main(String args[]) {
		int i = 1;
		switch (i) {
		case 0:
			System.out.println("zero");
			break;
		case 1:
			System.out.println("one");
			break;
		default:
			System.out.println("default");
			break;
		}
	}
}

例4

public class Z09 {

	public static void main(String[] args) {
		String str="a";
		switch(str) {
		case "a":
			System.out.println("a");
			break;
		case "bcd":
			System.out.println("bcd");
			break;
		default:
			System.out.println("unknow");
			break;
		}
	}
  • switch(表達式)中表達式的返回值必須是下述幾種類型之一:byte,short,char,int,枚舉,String`;
  • case子句中的值必須是常量,且所有case子句中的值應是不同的
  • default子句是可任選的,當沒有匹配的case時,執行default
  • break語句用來在執行完一個case分支後使程序跳出switch語句塊;如果沒有break,程序會順序執行到switch結尾。break必須寫!!!

試一試

  1. 使用 switch 把小寫類型的 char型轉爲大寫。只轉換 a, b, c, d, e. 其它的輸出 “other”。
public class Z09 {

	public static void main(String[] args) {
		char c='a';
		switch(c) {
		case 'a':
			System.out.println("A");
			break;
		case 'b':
			System.out.println("B");
			break;
		case 'c':
			System.out.println("C");
			break;
		case 'd':
			System.out.println("D");
			break;
		case 'e':
			System.out.println("E");
			break;
		default:
			System.out.println("other");
			break;
		}
	}
}
  1. 對學生成績大於60分的,輸出“合格”。低於60分的,輸出“不合格”。
public class Z09 {

	public static void main(String[] args) {
		int score = 75;	
		switch(score/60) {
		case 0:
			System.out.println("不合格");
			break;
		case 1:
			System.out.println("合格");
			break;
		}
	}
}

  1. 根據用於指定月份,打印該月份所屬的季節。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季。
public class Z09 {

	public static void main(String[] args) {
		int month = 5;	
		switch(month) {
		case 1:
		case 2:
		case 12:
			System.out.println("冬季");
			break;
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋季");
			break;
		}
	}
}
  1. 使用switch語句改寫下列if語句:
 int a = 3;
 int x = 100;
 if(a==1)
	x+=5;
 else if(a==2)
	x+=10;
 else if(a==3)
	x+=16;
 else		
	x+=34;

AC

int a=3;
int x=100;
switch(a) {
case 1:
	x+=5;
	break;
case 2:
	x+=10;
	break;
case 3:
	x+=16;
	break;
default:
	x+=34;
	break;
}

switch和if語句的對比

  • 如果判斷的具體數值不多,而且符合byte、 short 、int、 char這四種類型。雖然兩個語句都可以使用,建議使用swtich語句。因爲效率稍高。

  • 其他情況:對區間判斷,對結果爲boolean類型判斷使用if,if的使用範圍更廣。

今日課件

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