天天看點

java基礎筆記系列_Day03一、三元運算符二、資料輸入三、順序結構四、if結構五、switch結構

一、三元運算符

  1. 格式

    關系表達式 ? 表達式1 : 表達式2

    先判斷關系表達式,結果為true傳回表達式1,否則傳回表達式2.

    1:表達1和表達式2可以不是表達式,可以是字元,字元串等,但是資料類型需要相同。

    2:接收類型、表達式1、表達式2資料類型需要一緻。例如:字元串需要String接收。

案例:三個和尚

public class Test03{
    	public static void main(String[] args){
    		//定義三個身高變量
    		int height1 = 150;
    		int height2 = 210;
    		int height3 = 165;
    		//判斷三個中最小值
    		int tempheight = height1 < height2 ? height1 : height2;
    		int minweight = tempheight < height3 ? tempheight : height3;
    		//輸出最小值
    		System.out.println("最矮身高是"+minweight+"厘米");
    	}
    }
           

二、資料輸入

格式

1.導包,放在類名的上邊。 import java.util.Scanner

2.建立對象 Scanner sc = new Scanner(System.in)

3.接受資料 int i = sc.nextInt()

//導包
    import java.util.Scanner;
    public class Test04{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("輸入資料:");
    		//接收資料
    		int i = sc.nextInt();
    		System.out.print("輸入的資料是:" + i);
    	}
    }
           

更新版三個和尚:

//導包
    import java.util.Scanner;
    //程式入口
    public class Test05{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);	
    		//輸入三個和尚的身高
    		System.out.print("第一個和尚的身高是:");
    		int height1 = sc.nextInt();
    		System.out.print("第二個和尚的身高是:");
    		int height2 = sc.nextInt();
    		System.out.print("第三個和尚的身高是:");
    		int height3 = sc.nextInt();		
    		//比較三個和尚的身高并取最小值
    		int tempheight = height1 < height2 ? height1 : height2;
    		int minheight = height3 < tempheight ? height3 : tempheight;
    		System.out.print("三個和尚的最小身高是:"+minheight+"cm");
    	}
    }
           

三、順序結構

大多數代碼都按照順序結構執行

四、if結構

1.用法一:if(判斷){語句體}

if後判斷關系為true,則執行語句體,否則不執行語句體繼續順序執行。

示例:

public class Test06{
    	public static void main(String[] args){
    		int a = 10;
    		int b = 11;
    		if (a == b)
    		{
    			System.out.println("a和b相等");
    		}
    		System.out.println("結束");
    	}
    }
           

2.用法二:if(判斷){語句體1}else{語句體2}

if後判斷關系為true,則執行語句體1,否則執行語句體2,然後繼續順序執行。

案例:判斷奇偶

//導包
    import java.util.Scanner;
    //程式入口
    public class Test07{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("輸入一個整數:");
    		//鍵盤錄入接收資料
    		int i = sc.nextInt();
    		//判斷奇偶
    		if (i%2==0){
    			System.out.print(i+"是偶數");
    		}else{
    			System.out.print(i+"是奇數");
    		}
    	}
    }
           

此程式隻能判斷int類型取值範圍内的整數。

案例:潤/平年判斷

閏年條件:

1.能被4整除,不能被100整除。

2.能被400整除

//導包
    import java.util.Scanner;
    public class Test08{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("輸入年份:");
    		//鍵盤接收資料
    		long year = sc.nextLong();
    		//判斷
    		if((year%4==0&&year%100!=0)||(year%400==0)){
    			System.out.print(year+"是閏年");
    		}else{
    			System.out.print(year+"是平年");
    		}
    	}
    }
           

3.用法三:if(判斷){語句體1} else if(判斷){語句體2} else if(判斷){語句體3} else if(判斷){語句體3}…else{}

順序執行語句,直到判斷為true或者運作最後一個語句體,結束if語句

案例:判斷周一到周日

//導包
    import java.util.Scanner;
    public class Test09{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("請輸入一個數(0~7)");
    		int week = sc.nextInt();
    		//判斷
    		if (week < 0 || week > 7){
    			System.out.println("輸入錯誤,請重新輸入。");
    		}else if (week == 1){
    			System.out.println("周一");
    		}else if (week == 2){
    			System.out.println("周二");
    		}else if (week == 3){
    			System.out.println("周三");
    		}else if (week == 4){
    			System.out.println("周四");
    		}else if (week == 5){
    			System.out.println("周五");
    		}else if (week == 6){
    			System.out.println("周六");
    		}else {
    			System.out.println("周日");
    		}
    	}
    }
           

資料測試時需要測試三種資料:正确資料,邊界資料,錯誤資料

案例:考試獎勵

//導包
    import java.util.Scanner;
    public class Test10{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		System.out.print("輸入小明的分數:");
    		int score = sc.nextInt();
    		//判斷獎勵
    		if (score<0 || score>100){
    			System.out.print("輸入錯誤,請重新輸入");
    		}else if (score<=100 && score>=95){
    			System.out.print("獎勵一輛自行車");
    		}else if (score<=94 && score>=90){
    			System.out.print("獎勵遊樂場玩一次");
    		}else if (score<=89 && score>=85){
    			System.out.print("獎勵變形金剛");
    		}else if (score<=84){
    			System.out.print("無");
    		}
    	}
    }
           

五、switch結構

1.表達式:byte short int char 枚舉 String(不能出現long類型)

2.格式

switch(表達式){
			case 值1:
			break;//跳出目前switch語句
			case 值2:
			break;//如果缺少了break,則發生case穿透,順序執行下一個case
			case 值3:
			break;
			default:
			break;//default放在最後的時候可以省略這個break
}
           

3.與if差別:

if可以是判斷(大于、小于、不等于),switch隻能是等于。

4.switch為什麼不允許打斷點?

待定

5.case穿透案例:春夏秋冬

case穿透:沒有break時,繼續執行語句。

//導包
    import java.util.Scanner;
    public class Test11{
    	public static void main(String[] args){
    		//建立對象
    		Scanner sc = new Scanner(System.in);
    		//輸入月份
    		System.out.print("請輸入月份");
    		int month = sc.nextInt();
    		switch(month){
    			case 1:
    			case 2:
    			case 12:
    			System.out.print(month + "月是冬季");
    			break;
    			case 3:
    			case 4:
    			case 5:
    			System.out.print(month + "月是春季");
    			break;
    			case 6:
    			case 7:
    			case 8:
    			System.out.print(month + "月是夏季");
    			break;
    			case 9:
    			case 10:
    			case 11:
    			System.out.print(month + "月是秋季");
    			break;
    			default:
    			System.out.print("您輸入的月份有誤,請重新輸入");
    		}
    	}
    }
           

繼續閱讀