一、知識總體摘要
1:鍵盤獲取數據重要點:
- 導包:import java.util.Scanner
- Scanner scanner=new Scanner(System.in);
- 只有String類型接受的時候方法爲:scanner.next(),其他的皆爲next(Type):scanner.nextInt(),scanner.nextBoolean()
- 且在接受的方法中沒有char類型接受的方法只能通過String類型轉換爲char型數據
2:循環的重要記憶點
- 1.switch(表達式)-case:switch中的表達式類型只能是byte、short、char、int、枚舉類型、Sting六種類型中的任一種
- 2.for(①;②;③){④} for循環中的執行順序是:①-》②-》④-》③-》②-》④
- 3.while(){}while循環是先判斷再執行,do-while循環是先執行再判斷
3:continue和break
- 1.continue是結束本次離的最近的循環執行下次循環
- 2.break是結束離的最近的循環體
- 注:在continue和break語句後面不能再寫語句
二、代碼實現實例展現
public class LoopTest {
public static void main(String[] args) {
//接受數據實例
Scanner scanner=new Scanner(System.in);
//接受一個String類型數據
System.out.print("請輸入一個字符串:");
String string=scanner.next();
//接受一個int類型數據
System.out.print("請輸入一個數字:");
int num=scanner.nextInt();
//輸出剛纔接受的數據
System.out.println("接受的字符串是:"+string+" 接受的數字是:"+num);
//while和do-while循環實例
int num1=10;
while(num1>10){
System.out.println("進入了while循環體");
}
do {
System.out.println("進入了do-while循環體");
}while (num1>10);
//for循環體中的語句執行順尋的實例
for(int i=0;i<1;i++,System.out.print("執行此語句")){
System.out.println("進入for循環,i的值爲:"+i);
}
/*
* break特殊實例小技巧,在很多算法中需要用到break而break有一個最近原則不會直接結束整個循環,所以
* 此時引進一個標籤,可以直接在break後加上這個標籤名直接結束特定的循環體
* */
//滿足跳出循環條件時,沒有使用標籤完成循環的例子通過循環的次數比較
int count1=0;
for(int i=0;i<4;i++){
for(int j=1;j<5;j++){
if(i==j)
break;
}count1++;
}
System.out.println("循環結束後的count1值爲:"+count1);
int count2=0;
//滿足跳出循環條件時,使用標籤完成循環的例子通過循環的次數來比較
loop: for(int i=0;i<4;i++){
for(int j=1;j<5;j++) {
if (i == j)
break loop;
}count2++;
}
System.out.println("循環結束後的count2值爲:"+count2);
}
}
輸出結果爲:
請輸入一個字符串:Test
請輸入一個數字:123
接受的字符串是:Test 接受的數字是:123
進入了do-while循環體
進入for循環,i的值爲:0
執行此語句循環結束後的count1值爲:4
循環結束後的count2值爲:1
在這些知識的學習中最重要的我覺得需要注意的點就是上述這些,無論是循環中使用的一些算法優化技巧還是鍵盤輸入和for循環的理解,作爲一個非小白重新學習這些還是很快就能看懂學會。重要的是將一些曾經學習其他語言中沒有接觸過的知識點彙總,做一定的理解記憶。