您好,登錄后才能下訂單哦!
day04 Java基礎語法 (下) |
筆記Notes要點
|
switch(表達式){ ??????? case 常量1: ????????????執行語句1; ????????????break; ??????? case 常量2: ????????????執行語句2; ????????????break; ????????...... ????????default: ????????????執行語句n; ????????????break; ????????}
/* 從鍵盤分別輸入年、月、日,判斷這一天是當年的第幾天 ???注:判斷一年是否是閏年的標準: ???????1)可以被4整除,但不可被100整除 ????或 ???????2)可以被400整除 */ import java.util.Scanner; public class SwitchTest5{ ????public static void main(String[] args){ ????????//1.讀取數據 ????????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; //用來統計當年的總天數 ????????//2.統計天數 ????????switch(month){ ????????case 4: ????????????sumDay += 31; ????????case 3: ????????????//判斷當年是否是閏年 ????????????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 + "日是當年的第" + sumDay + "天"); ????} } /* ????編寫程序:由鍵盤輸入三個整數分別存入變量num1、num2、num3, ????對它們進行排序(使用 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("請輸入三個數字"); ????????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<num2){//num3最小 ??????????????? System.out.print("小到大:"+ num3 + " " + num2 + " " + num1); ????????????}else{ ??????????????? System.out.println("小到大:"+ num2 + " " + num3 + " " + num1); ????????????} ????????}else{//num1<num2 ????????????if(num3>num2){//num3最大 ??????????????? System.out.println("小到大:"+ num1 + " "+ num2 + " "+ num3); ????????????}else if(num3<num1){//num2最大 ??????????????? System.out.println("小到大:"+ num3 + " " + num1 + " " + num2); ????????????}else{ ??????????????? System.out.println("小到大:"+ num1 + " " + num3 + " " + num2); ????????????} ????????} ????} }
String str = new String("帥"); ????????//判斷帥不帥 ????????System.out.println("帥" ==??str); //這樣不能比字符串的內容 ????????boolean handsome = "帥".equals(str); //比較的是字符串的內容 - 如果內容相同返回true否則返回false
對學生成績大于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.讀取數據 ????????Scanner sc = new Scanner(System.in); ????????System.out.println("請輸入月份"); ????????int month = sc.nextInt(); ????????System.out.println("請輸入日期"); ????????int day = sc.nextInt(); ????????int sumDay = 0; //用來統計當年的總天數 ???? ????????//2.統計天數 ????????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原來的天數再額外加上28 ???????????? ????????case 2: ????????????sumDay??+= 31; // sumDay = sumDay + 31 ???????????? ????????case 1: ????????????sumDay += day; ???? ????????} ????????//3.輸出 ????????System.out.println("2019年" + month + "月" + day + "日是當年的第" + sumDay + "天"); ????} }
/* 從鍵盤分別輸入年、月、日,判斷這一天是當年的第幾天 ???注:判斷一年是否是閏年的標準: ???????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("請輸入天數:"); ????????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 + "日是當年的第" + sumDay + "天"); ????} }
循環的四個部分: ????????1.初始化條件 ????????2.循環條件 ????????3.循環體 ????????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(); ????????} ????} }
可以給循環命名,break + 循環名字 |
總結Summary |
|
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。