文章目錄
- 鞏固題
- 1、從鍵盤輸入一個整數,判斷它是否是5的倍數
- 2、從鍵盤輸入一個字符,判斷字符類型
- 3、計算折扣后金額
- 4、輸出月份對應的英語單詞
- 5、計算今天是星期幾
- 簡答題
- 拔高題(自愿)
- 6、判斷年、月、日是否合法
- 7、判斷打魚還是曬網
- 8、判斷星座
鞏固題
1、從鍵盤輸入一個整數,判斷它是否是5的倍數
參考答案:
import java.util.Scanner;public class Homework1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("請輸入一個整數:");int num = input.nextInt();input.close();if(num % 5==0){System.out.println(num +"是5的倍數");}else{System.out.println(num +"不是5的倍數");}}
}
2、從鍵盤輸入一個字符,判斷字符類型
從鍵盤輸入一個字符,判斷它是字母(a-z或A-A)、數字(0-9),還是其他字符
import java.util.Scanner;public class Homework2 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("請輸入一個字符:");char c = input.next().charAt(0);input.close();if(c >= '0' && c <= '9'){System.out.println(c + "是數字字符.");}else if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'){System.out.println(c + "是字母字符.");}else{System.out.println(c + "是非數字非字母的其他字符.");}}
}
3、計算折扣后金額
從鍵盤輸入訂單總價格totalPrice(總價格必須>=0),
-
判斷當
totalPrice<0
時,顯示輸入有誤 -
當
totalPrice>=0
時,根據優惠政策計算打折后的總價格。-
判斷當
totalPrice >=500
,discount賦值為0.8 -
判斷當
totalPrice >=400
且<500
時,discount賦值為0.85 -
判斷當
totalPrice >=300
且<400
時,discount賦值為0.9 -
判斷當
totalPrice >=200
且<300
時,discount賦值為0.95 -
判斷當
totalPrice >=0
且<200
時,不打折,即discount賦值為1 -
輸出結果
-
import java.util.Scanner;public class Homework3 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("請輸入訂單總價格:");double totalPrice = input.nextDouble();input.close();if(totalPrice >= 0){double discount;if(totalPrice>=500){discount = 0.8;}else if(totalPrice>=400){discount = 0.85;}else if(totalPrice>=300){discount = 0.9;}else if(totalPrice>=200){discount = 0.95;}else{discount = 1;}System.out.println("訂單總價:" + totalPrice);System.out.println("享受的折扣:" + discount);System.out.println("折扣后總價:" + totalPrice * discount);}else{System.out.println("總價格輸入有誤!");}}
}
4、輸出月份對應的英語單詞
從鍵盤輸入月份值(1-12),輸出對應月份的英語單詞,如果月份值超過1-12,提示輸入錯誤!
import java.util.Scanner;public class Homework4 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("請輸入月份值:");int month = input.nextInt();input.close();switch (month){case 1:System.out.println("January");break;case 2:System.out.println("February");break;case 3:System.out.println("March");break;case 4:System.out.println("April");break;case 5:System.out.println("May");break;case 6:System.out.println("June");break;case 7:System.out.println("July");break;case 8:System.out.println("August");break;case 9:System.out.println("September");break;case 10:System.out.println("October");break;case 11:System.out.println("November");break;case 12:System.out.println("December");break;default:System.out.println("月份值輸入有誤!");}}
}
5、計算今天是星期幾
(1)定義變量week賦值為上一年最后一天的星期值,例如:2021年12月31日的星期值5,
(2)定義變量year、month、day,分別賦值今年(例如:2022年)某一天的年、月、日值。
(3)計算這一天是星期幾。
(4)開發提示
- 需要計算這一天是今年(例如:2022年)的第幾天,即今年已經過了幾天了(總天數)
- 再用(總天數 + 5 )% 7 的結果來判斷是星期幾
(5)每個月總天數:
- 平年的2月份有28天,閏年的2月份有29天。
- 1月、3月、5月、7月、8月、10月、12月有31天,
- 4月、6月、9月、11月有30天。
(6)閏年的判斷標準是:
-
年份year可以被4整除,但不能被100整除
-
或者年份year可以被400整除
public class Homework5 {public static void main(String[] args) {int week = 5;int year = 2022;int month = 3;int day = 8;//判斷這一天是當年的第幾天==>從1月1日開始,累加到xx月xx日這一天//(1)[1,month-1]個月滿月天數//(2)單獨考慮2月份是否是29天(依據是看year是否是閏年)//(3)第month個月的day天//聲明一個變量days,用來存儲總天數int days = 0;//累加[1,month-1]個月滿月天數switch (month) {case 12://累加的1-11月days += 30;//這個30是代表11月份的滿月天數//這里沒有break,繼續往下走case 11://累加的1-10月days += 31;//這個31是代表10月的滿月天數//這里沒有break,繼續往下走case 10:days += 30;//9月case 9:days += 31;//8月case 8:days += 31;//7月case 7:days += 30;//6月case 6:days += 31;//5月case 5:days += 30;//4月case 4:days += 31;//3月case 3:days += 28;//2月//在這里考慮是否可能是29天if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days++;//多加1天}case 2:days += 31;//1月case 1:days += day;//第month月的day天}//計算星期week += days;week %= 7;//輸出結果System.out.print(year + "年" + month + "月" + day + "日是星期");switch (week) {case 0:System.out.println("日");break;case 1:System.out.println("一");break;case 2:System.out.println("二");break;case 3:System.out.println("三");break;case 4:System.out.println("四");break;case 5:System.out.println("五");break;case 6:System.out.println("六");break;}}
}
簡答題
switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?
switch(表達式)支持的類型有byte,short,int,char,Byte,Short,Integer,Character,String和枚舉
拔高題(自愿)
6、判斷年、月、日是否合法
(1)從鍵盤輸入年、月、日,
(2)要求年份必須是正整數,月份范圍是[1,12],日期也必須在本月總天數范圍內,
(3)如果輸入正確,輸出“xxxx年-xx月-xx日”結果,否則提示輸入錯誤。
import java.util.Scanner;public class Homework6 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("請輸入日期:");System.out.print("年:");int year = input.nextInt();System.out.print("月:");int month = input.nextInt();System.out.print("日:");int day = input.nextInt();input.close();if (year > 0) {if (month >= 1 && month <= 12) {//計算month月的總天數int days;if (month == 2) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days = 29;} else {days = 28;}} else if (month == 4 || month == 6 || month == 9 || month == 11) {days = 30;} else {days = 31;}if(day >= 1 && day <= days) {System.out.println(year + "-" + month + "-" + day);}else{System.out.println("日期輸入不合法");}} else {System.out.println("月份輸入不合法");}} else {System.out.println("年份輸入不合法");}}
}
7、判斷打魚還是曬網
(1)從鍵盤輸入年、月、日,
(2)假設從這一年的1月1日開始執行三天打魚兩天曬網,那么你輸入的這一天是在打魚還是曬網。
(3)開發提示:
- 先計算這一天是這一年的第幾天,即總天數
- 再用總天數 % 5(三天打魚兩天曬網的周期),根據結果來判斷是打魚還是曬網
(4)每個月總天數:
- 平年的2月份有28天,閏年的2月份有29天。
- 1月、3月、5月、7月、8月、10月、12月有31天,
- 4月、6月、9月、11月有30天。
(5)閏年的判斷標準是:
- 年份year可以被4整除,但不能被100整除
- 或者年份year可以被400整除
參考答案:
import java.util.Scanner;public class Homework7 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("請輸入日期:");System.out.print("年:");int year = input.nextInt();System.out.print("月:");int month = input.nextInt();System.out.print("日:");int day = input.nextInt();input.close();//輸入日期值合法性驗證boolean flag = false;if (year > 0) {if (month >= 1 && month <= 12) {//計算month月的總天數int days;if (month == 2) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days = 29;} else {days = 28;}} else if (month == 4 || month == 6 || month == 9 || month == 11) {days = 30;} else {days = 31;}if(day >= 1 && day <= days) {flag = true;}else{System.out.println("日期輸入不合法");}} else {System.out.println("月份輸入不合法");}} else {System.out.println("年份輸入不合法");}if(flag){//判斷這一天是當年的第幾天==>從1月1日開始,累加到xx月xx日這一天//(1)[1,month-1]個月滿月天數//(2)單獨考慮2月份是否是29天(依據是看year是否是閏年)//(3)第month個月的day天//聲明一個變量days,用來存儲總天數int days = 0;//累加[1,month-1]個月滿月天數switch (month) {case 12://累加的1-11月days += 30;//這個30是代表11月份的滿月天數//這里沒有break,繼續往下走case 11://累加的1-10月days += 31;//這個31是代表10月的滿月天數//這里沒有break,繼續往下走case 10:days += 30;//9月case 9:days += 31;//8月case 8:days += 31;//7月case 7:days += 30;//6月case 6:days += 31;//5月case 5:days += 30;//4月case 4:days += 31;//3月case 3:days += 28;//2月//在這里考慮是否可能是29天if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days++;//多加1天}case 2:days += 31;//1月case 1:days += day;//第month月的day天}System.out.print(year + "-" + month + "-" + day + "這一天是");System.out.println((days % 5 == 1 || days % 5 == 2 || days % 5 == 3 ? "打魚" : "曬網"));}}
}
8、判斷星座
(1)聲明變量month和day,用來存儲你出生的月份和日期,
(2)判斷這個日期屬于什么星座,各個星座的日期范圍如下:
參考答案:
public class Homework8 {public static void main(String[] args) {int month = 12;int day = 2;switch(month) {case 1:if(day<= 19) {System.out.println("摩羯座");}else {System.out.println("水瓶座");}break;}//以下判斷是基于月份和日期在合法范圍內的if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day <= 18 && day >= 1 )) {System.out.println("生日" + month + "月" + day + "日是水瓶座");} else if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day <= 20)) {System.out.println("生日" + month + "月" + day + "日是雙魚座");} else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {System.out.println("生日" + month + "月" + day + "日是白羊座");} else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {System.out.println("生日" + month + "月" + day + "日是金牛座");} else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {System.out.println("生日" + month + "月" + day + "日是雙子座");} else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {System.out.println("生日" + month + "月" + day + "日是巨蟹座");} else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {System.out.println("生日" + month + "月" + day + "日是獅子座");} else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {System.out.println("生日" + month + "月" + day + "日是處女座");} else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {System.out.println("生日" + month + "月" + day + "日是天平座");} else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {System.out.println("生日" + month + "月" + day + "日是天蝎座");} else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {System.out.println("生日" + month + "月" + day + "日是射手座");} else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {System.out.println("生日" + month + "月" + day + "日是摩羯座");}}
}