day04 Java基礎(chǔ)語法 (下) 縉云網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。 |
筆記Notes要點(diǎn)
|
switch(表達(dá)式){ ??????? case 常量1: ????????????執(zhí)行語句1; ????????????break; ??????? case 常量2: ????????????執(zhí)行語句2; ????????????break; ????????...... ????????default: ????????????執(zhí)行語句n; ????????????break; ????????}
/* 從鍵盤分別輸入年、月、日,判斷這一天是當(dāng)年的第幾天 ???注:判斷一年是否是閏年的標(biāo)準(zhǔn): ???????1)可以被4整除,但不可被100整除 ????或 ???????2)可以被400整除 */ import java.util.Scanner; public class SwitchTest5{ ????public static void main(String[] args){ ????????//1.讀取數(shù)據(jù) ????????Scanner sc = new Scanner(System.in); ????????System.out.println("請輸入年份"); ????????int year = sc.nextInt(); ????????System.out.println("請輸入月份"); ????????int month = sc.nextInt(); ????????System.out.println("請輸入日期"); ????????int day = sc.nextInt(); ????????int sumDay = 0; //用來統(tǒng)計(jì)當(dāng)年的總天數(shù) ????????//2.統(tǒng)計(jì)天數(shù) ????????switch(month){ ????????case 4: ????????????sumDay += 31; ????????case 3: ????????????//判斷當(dāng)年是否是閏年 ????????????if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ ????????????????sumDay += 29; ????????????}else{ ????????????????sumDay += 28; ????????????} ????????case 2: ????????????sumDay??+= 31; ????????case 1: ????????????sumDay += day; ????????} ????????//3.輸出 ????????System.out.println(year + "年" + month + "月" + day + "日是當(dāng)年的第" + sumDay + "天"); ????} } /* ????編寫程序:由鍵盤輸入三個(gè)整數(shù)分別存入變量num1、num2、num3, ????對它們進(jìn)行排序(使用 if-else if-else),并且從小到大輸出。 */ import java.util.Scanner; public class IfTest { ????public static void main(String[] args){ ????????Scanner sc = new Scanner(System.in); ????????System.out.println("請輸入三個(gè)數(shù)字"); ????????int num1,num2,num3; ????????num1 = sc.nextInt(); ????????num2 = sc.nextInt(); ????????num3 = sc.nextInt(); ????????if(num1>num2){ ????????????if(num3>num1){//num3最大 ??????????????? System.out.println("小到大:"+ num2 +" "+ num1 +" "+ num3); ????????????}else if(num3 ??????????????? System.out.print("小到大:"+ num3 + " " + num2 + " " + num1); ????????????}else{ ??????????????? System.out.println("小到大:"+ num2 + " " + num3 + " " + num1); ????????????} ????????}else{//num1 ????????????if(num3>num2){//num3最大 ??????????????? System.out.println("小到大:"+ num1 + " "+ num2 + " "+ num3); ????????????}else if(num3 ??????????????? System.out.println("小到大:"+ num3 + " " + num1 + " " + num2); ????????????}else{ ??????????????? System.out.println("小到大:"+ num1 + " " + num3 + " " + num2); ????????????} ????????} ????} } String字符串比較 String str = new String("帥"); ????????//判斷帥不帥 ????????System.out.println("帥" ==??str); //這樣不能比字符串的內(nèi)容 ????????boolean handsome = "帥".equals(str); //比較的是字符串的內(nèi)容 - 如果內(nèi)容相同返回true否則返回false switch使用小技巧 對學(xué)生成績大于60分的,輸出“合格”。低于60分的,輸出“不合格” switch(score / 60){ ???????? ????????case 0: ????????????System.out.println("不合格"); ????????????break; ????????case 1: ????????????System.out.println("合格"); ????????????break; 編寫程序:輸入月,日,輸出第幾天 /* 4. 編寫程序:從鍵盤上輸入2019年的“month”和“day”,要求通過程序輸出輸入的日期為2019年的第幾天。 */ import java.util.Scanner; public class SwitchTest4{ ????public static void main(String[] args){ ???? ????????//1.讀取數(shù)據(jù) ????????Scanner sc = new Scanner(System.in); ????????System.out.println("請輸入月份"); ????????int month = sc.nextInt(); ????????System.out.println("請輸入日期"); ????????int day = sc.nextInt(); ????????int sumDay = 0; //用來統(tǒng)計(jì)當(dāng)年的總天數(shù) ???? ????????//2.統(tǒng)計(jì)天數(shù) ????????switch(month){ ????????/* ????????case 1: ????????????sumDay = day; ????????????break; ????????case 2: ????????????sumDay = day + 31; ????????????break; ????????case 3: ????????????sumDay = day + 31 + 28; ????????????break; ????????case 4: ????????????sumDay = day + 31 + 28 + 31; ????????????break; ????????*/ ????????case 4: ????????????sumDay += 31; ???????????? ????????case 3: ??????????? sumDay += 28; //賦值和+=完全不一樣。sumDay += 28 : 除了sumDay原來的天數(shù)再額外加上28 ???????????? ????????case 2: ????????????sumDay??+= 31; // sumDay = sumDay + 31 ???????????? ????????case 1: ????????????sumDay += day; ???? ????????} ????????//3.輸出 ????????System.out.println("2019年" + month + "月" + day + "日是當(dāng)年的第" + sumDay + "天"); ????} } 編寫程序:輸入年,月,日,輸出第幾天 /* 從鍵盤分別輸入年、月、日,判斷這一天是當(dāng)年的第幾天 ???注:判斷一年是否是閏年的標(biāo)準(zhǔn): ???????1)可以被4整除,但不可被100整除 ????或 ???????2)可以被400整除 */ import java.util.Scanner; public class SwitchTest4 { ????public static void main(String[] args){ ????????Scanner sc = new Scanner(System.in); ??????? System.out.println("請輸入年份:"); ????????int year = sc.nextInt(); ??????? System.out.println("請輸入月份:"); ????????int month = sc.nextInt(); ??????? System.out.println("請輸入天數(shù):"); ????????int day = sc.nextInt(); ????????int sumDay = 0; ????????switch(month){ ????????????case 12: ????????????????sumDay += 30; ????????????case 11: ????????????????sumDay += 31; ????????????case 10: ????????????????sumDay += 30; ????????????case 9: ????????????????sumDay += 31; ????????????case 8: ????????????????sumDay += 31; ????????????case 7: ????????????????sumDay += 30; ????????????case 6: ????????????????sumDay += 31; ????????????case 5: ????????????????sumDay += 30; ????????????case 4: ????????????????sumDay += 31; ????????????case 3: ????????????????// 28或 29 ????????????????if((year%4==0&&year%100!=0)||year % 400 ==0){//閏年 ????????????????????sumDay += 29; ????????????????}else{ ????????????????????sumDay += 28; ????????????????} ????????????case 2: ????????????????sumDay += 31; ????????????case 1: ????????????????sumDay += day; ????????????????break; ????????} ????????System.out.println(year+"年" + month + "月" + day + "日是當(dāng)年的第" + sumDay + "天"); ????} } 循環(huán)四要素 循環(huán)的四個(gè)部分: ????????1.初始化條件 ????????2.循環(huán)條件 ????????3.循環(huán)體 ????????4.迭代條件 打印九九乘法表 /* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 打印九九乘法表 */ public class c99 { ????public static void main(String[] args){ ????????int i,j; ????????for(i=1;i<=9;i++){//9行 ????????????for(j=1;j<=i;j++){ ????????????????System.out.print(j + "*" +i+" = "+ i*j +" "); ????????????} ????????????System.out.println(); ????????} ????} } 特殊關(guān)鍵字continue、break break用于 switch-case語句與循環(huán)結(jié)構(gòu)中,表示結(jié)束當(dāng)前case語句,結(jié)束循環(huán)語句 continue,用于 循環(huán)結(jié)構(gòu)? 表示結(jié)束當(dāng)此循環(huán)。 在嵌套循環(huán)中break結(jié)束的包含該語句的循環(huán)語句,對外層循環(huán)無作用 在嵌套循環(huán)中continue跳出的是包含他的循環(huán)語句的當(dāng)次循環(huán),不跳出 嵌套循環(huán)中如何使用內(nèi)部循環(huán)的break結(jié)束外層循環(huán) 可以給循環(huán)命名,break + 循環(huán)名字 |
總結(jié)Summary |
|