做二手元器件那個(gè)網(wǎng)站查價(jià)格關(guān)鍵詞排名批量查詢軟件
1 編碼規(guī)范——衛(wèi)語(yǔ)句
??表達(dá)異常分支時(shí),少用if-else方式。
??比如成績(jī)判斷中對(duì)于非法輸入的處理:
/*>=90 <=100 優(yōu)秀>=80 <90 良好>=70 <80 一般>=60 <70 及格<60 不及格*/@Testpublic void test2() {//int score = 78;//通過(guò)Scanner可以實(shí)現(xiàn)從控制臺(tái)輸入信息Scanner scanner = new Scanner(System.in);System.out.println("請(qǐng)輸入成績(jī):");int score = scanner.nextInt();if(score < 0 || score > 100) {System.out.println("非法輸入");} else if (score >= 90 && score <= 100) {System.out.println("優(yōu)秀");} else if (score >= 80 && score < 90) {System.out.println("良好");} else if (score >= 70 && score < 80) {System.out.println("一般");} else if (score >= 60 && score < 70) {System.out.println("及格");} else {System.out.println("不及格");}}
??修改后:
@Testpublic void test2() {//通過(guò)Scanner可以實(shí)現(xiàn)從控制臺(tái)輸入信息Scanner scanner = new Scanner(System.in);System.out.println("請(qǐng)輸入成績(jī):");int score = scanner.nextInt();//衛(wèi)語(yǔ)句1if (score < 0 || score > 0) {//異常和正常 要分開(kāi)System.out.println("Invalid input!");//后面的代碼不再執(zhí)行return;}//衛(wèi)語(yǔ)句2...//合法輸入if (score >= 90 && score <= 100) {System.out.println("優(yōu)秀");} else if (score >= 80 && score < 90) {System.out.println("良好");} else if (score >= 70 && score < 80) {System.out.println("一般");} else if (score >= 60 && score < 70) {System.out.println("及格");} else {System.out.println("不及格");}}
2 循環(huán)控制語(yǔ)句(接Day2)
?2.2 continue、break
???還是跟C語(yǔ)法相差無(wú)幾,放個(gè)demo了事
???continue:跳出本次循環(huán),繼續(xù)下一次循環(huán)
???break:跳出離他最近的那層循環(huán)
@Test//結(jié)果: 1 2 4 5
public void test44() {for (int i = 1; i <= 5; i++) {if (i == 3) {continue;}System.out.println(i);}
}@Test//結(jié)果: 1 2
public void test45() {for (int i = 1; i <= 5; i++) {if (i == 3) {break;}System.out.println(i);}
}@Test//
public void test46() {//i,j,kfor (int i = 1; i <= 5; i++) {System.out.println("i: " + i);for (int j = 1; j <= 5; j++) {if (j == 3) {break;}System.out.println("j: " + j);}}
}
?2.3 雙重for循環(huán)
???這個(gè)也很簡(jiǎn)單,
???雙重for循環(huán):
???外層循環(huán)控制行數(shù),數(shù)一下有幾行就能確定外層循環(huán)。
???內(nèi)層循環(huán)控制列數(shù),這一行打印多少個(gè),到底要打印多少個(gè)要找出和當(dāng)前行之間的一個(gè)關(guān)系。
???還有一些打印金字塔, 各種三角形的題,統(tǒng)一放到下一篇作業(yè)博客吧.
?2.4 Switch
???這部分當(dāng)初學(xué)的時(shí)候沒(méi)感覺(jué)有啥特別的,但是同種情況的case可以合并這個(gè)操作是真的有點(diǎn)沒(méi)記起來(lái),溫故知新了。
import com.sdust.day2.*;@Test//月份 天數(shù)public void test2() {Scanner scanner = new Scanner(System.in);Homework day2 = new Homework();System.out.println("please input month: ");int month = scanner.nextInt();if (month < 1 || month > 12) {System.out.println("invalid month");return;}switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:System.out.println("31days");break;case 4:case 6:case 9:case 11:System.out.println("30days");break;case 2:System.out.println("please input year: ");int year = scanner.nextInt();if (day2.judgeLeapYear(year)) {System.out.println("29days");} else {System.out.println("28days");}default:System.out.println("default");break;}}
昨天寫(xiě)了幾個(gè)作業(yè)題,包括一個(gè)閏年判斷的題,于是在今天寫(xiě)這部分代碼對(duì)閏年進(jìn)行特判的時(shí)候心血來(lái)潮想直接調(diào)用昨天的代碼。先是要導(dǎo)包,仿照Scanner輸入的形式寫(xiě)了一下,發(fā)現(xiàn)還真行,嗯,基礎(chǔ)的Java也就那么回事嘛~