step1 基本语法流程控制
目錄
1、用戶交互Scanner?
2、Scanner進階使用
3、順序結構
4、if選擇結構
(1)if單選則結構
(2)if雙選擇結構
(3)if多選擇結構、if嵌套結構
5、Switch選擇結構
6、While循環詳解
7、DoWhile循環
8、For循環詳解
9、增強for循環
10、break、continue、goto
???????
1、用戶交互Scanner?
·兩種方法的區別主要在于”結束符“不一樣
·next():分隔符:空格
1、一定要讀取到有效字符后才可以結束輸入。
2、對輸入有效字符之前遇到的空白,next()方法會自動將其去掉。
3、只有輸入有效字符后才將其后面輸入的空白作為分隔符或者結束符。
4、next()不能得到帶有空格的字符串。
·nextLine(): 分隔符:回車
1、以Enter為結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字符。
2、可以獲得空白。
?
package liuchengkongzhi.scanner; import java.util.Scanner; // 交互輸入 public class Demo01 {public static void main(String[] args) {// 創建一個掃描對象,用于接收鍵盤數據Scanner scanner = new Scanner(System.in);System.out.println("使用next方式接收:");// 判斷用戶有沒有輸入字符if (scanner.hasNext()){String str = scanner.next();System.out.println("輸出的內容為:"+str);}// 凡是屬于IO流的類如果不關閉會一直占用資源,所以用完之后應該關閉scanner.close();} } package liuchengkongzhi.scanner; import java.util.Scanner; public class Demo02 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("使用nextLine方式接收:");if (scanner.hasNextLine()){String a = scanner.nextLine();System.out.println("輸出的內容為:"+a);}scanner.close();} }
2、Scanner進階使用
package liuchengkongzhi.scanner;
import java.util.Scanner;
public class Demo04 {public static void main(String[] args) {
// 我們可以輸入多個數字,并求其總和和平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入并輸出執行結果Scanner scanner = new Scanner(System.in);// 和double sum = 0;// 計算輸入了多少個數字int m =0;System.out.println("請輸入數據:");// 通過循環判斷是否還有輸入,并在里面對每一次進行求和統計while(scanner.hasNextDouble()){double x = scanner.nextDouble();m++;sum = sum + x;System.out.println("你輸入了第"+m+"個數據,然后當前結果sum="+sum);}System.out.println(m + "個數的和為:" + sum);System.out.println(m + "個數的平均數為:" + (sum/m));scanner.close();}
}
3、順序結構
JAVA的基本結構就是順序結構,除非特別指明,否則就按照順序一句一句執行。順序結構是最簡單的算法結構。語句與語句之間,框與框之間是按從上到下的順序進行的,它是由若干個依次執行的處理步驟組成的,它是任何一個算法都離不開的一種基本算法結構。
4、if選擇結構
(1)if單選則結構
package IF0410; import java.util.Scanner; // if單選擇結構 public class DemoIf01 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("請輸入內容:");String s = scanner.nextLine();// equals 判斷字符串是否相等if(s.equals("Hello")){System.out.println("hi "+s);}System.out.println("End");scanner.close();} }(2)if雙選擇結構
package IF0410; import java.util.Scanner; // if雙選擇結構 public class IFDemo02 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("請輸入成績:");// 如果分數大于等于60就是及格,小于60就是不及格if(scanner.hasNextDouble()) {double score = scanner.nextDouble();if (score >= 60) {System.out.println("及格!");} else {System.out.println("不及格!");}}scanner.close();} }(3)if多選擇結構、if嵌套結構
package IF0410; import java.util.Scanner; // if多選擇結構 public class IFDemo03 {public static void main(String[] args) {// 成績100-90:A 90-80:B 80-70:C 70-60:D 60-不及格Scanner scanner = new Scanner(System.in);System.out.println("請輸入成績:");int score = 0;if (scanner.hasNextInt()) {// 這里時嵌套的if結構,防止沒有輸入score = scanner.nextInt();if (score == 100) {System.out.println("恭喜滿分!");} else if (score < 100 && score >= 90) {System.out.println("A");} else if (score < 90 && score >= 80) {System.out.println("B");} else if (score < 80 && score >= 70) {System.out.println("C");} else if (score < 70 && score >= 60) {System.out.println("D");}else if (score < 60 && score >=0) {System.out.println("不及格!");} else{System.out.println("輸入成績錯誤");}scanner.close();}} }
5、Switch選擇結構
多選擇結構還有一個實現方式就是switch case語句。switch case語句判斷一個變量與一系列值中某個值是否相等,每個值稱為一個分支。
switch語句中的變量類型可以是:
- 1、byte、short、int或者char、String
- 2、同時case標簽必須為字符串常量或字面量。
?
?
public class Demo02 {public static void main(String[] args) {String name = "Merak";switch(name){case "Merak":System.out.println("美女!");break;case "Zhang Xuan":System.out.println("學霸!");break;default:System.out.println("你輸入的不是主人的名字!");}} }?在Java SE7以后switch的變量可以是String,但本質上還是將它先變成字節碼,然后匹配。
java文件在編譯之后會生成class文件,class文件是字節碼文件,人是無法看懂的,但是可以通過IDEA反編譯
在File-->Project Structure -->Compiler? output中可以找到Java編譯后生成的class文件
?右鍵點擊找到所在的package所在的文件位置,并且將編譯后的class文件復制到該路徑下
?然后在IDEA中點開class文件
可以看到,首先將string編譯為哈希碼,并且引入中間變量var3來判斷。
6、While循環詳解
注意控制循環結束條件,不要寫成死循環了
package While0411; // 輸出1-100 public class Demo01 {public static void main(String[] args) {int i = 0;while(i<100){// 控制訓練結束i++;System.out.println(i);}} }// 1加到100 class Demo02{public static void main(String[] args) {int i = 0;int sum = 0;while(i<100){i++;sum+=i;}System.out.println(sum);} }7、DoWhile循環
while:先判斷條件是否成立再執行
do-while:先執行一遍再判斷條件是否成立
對于while語句而言,如果不滿足條件,則不能進入循環。但有時候我們需要即使不滿足條件,
也至少執行一次。do...while循環和while循環相似,不同的是,do...while循環至少會執行一次。
?
8、For循環詳解
for循環執行的次數是在執行前就確定的。語法格式如下
// 初始值;循環條件;迭代 for(int i=1;i<=99;i+=2){ // i首先判斷是否滿足條件,滿足則執行語句,執行完之后再迭代快捷輸入:
練習1:計算0到100之間的奇數和偶數的和
練習2:用while或for循環輸出1-1000之間能被5整除的數,并且每行輸出3個
?注意:
1、System.out.println 輸出完會自動換行? System.out.print不會換行
System.out.println() 等價于?System.out.print("\n")
2、System.out.print(i+"\t"); // 轉義字符一定要用雙引號
package For0411; /* 練習2:用while或for循環輸出1-100之間能被5整除的數,并且每行輸出3個*/ public class Demo02 {public static void main(String[] args) {int j = 0;for (int i = 1; i <= 100; i++) {if(i%5==0){j++;if(j%3 !=0){System.out.print(i+"\t"); // 轉義字符一定要用雙引號}else{System.out.print(i+"\n");}}}} } class Demo04 {public static void main(String[] args) {for (int i = 1; i <= 100; i++) {if(i%5==0 ){if(i%15 ==0){System.out.print(i+"\n");}else{System.out.print(i+"\t");}}}}}class demo05{public static void main(String[] args) {for (int i = 1; i <= 100; i++) {if(i%5==0 && i%15 !=0){System.out.print(i+"\t");}if(i%15==0){System.out.print(i+"\n");}}} }練習3:打印九九乘法表
package For0411; /*練習3:打印九九乘法表 * 嵌套for循環*/ public class Demo03 {public static void main(String[] args) {for (int i = 1; i < 10; i++) {for (int j = 1; j <= i; j++) {System.out.print(j+"x"+i+"="+(i*j)+"\t");}System.out.println();}} }9、增強for循環
主要用于遍歷數組和集合
package For0411; // 增強for循環 public class Demo1 {public static void main(String[] args) {// 定義一個數組int[] numbers = {1,2,3,4,5};// 打印數組中的元素for(int x:numbers){System.out.println(x);}// 等價于System.out.println("---------------");for(int i=0;i<5;i++){// 輸出數組中第i位置的元素System.out.println(numbers[i]);}} }
10、break、continue、goto
(1)break在任何循環語句的主體部分,均可用break控制循環的流程。break用于強行退出循環,不執行循環中剩余的語句。(break語句也在switch語句中使用)
package control; // break public class DemoBreak {public static void main(String[] args) {// 在3那里跳出循環,繼續執行循環后的語句for (int i = 1; i <= 10; i++) {System.out.println(i);if(i==3){break;}}System.out.println("break done!");} }(2)continue語句用在循環語句體中,用于終止某次循環過程,即跳過循環體中尚未執行的語句,接著進行下一次是否執行循環的判定。
package control; // continue public class DemoContinue {public static void main(String[] args) {int i=0;while(i<10){i++;// 當i==3時,結束這一次循環,后面的語句就不進行了繼續執行循環的下一步if(i==3){continue;}System.out.print(i+"\t");}System.out.println();System.out.println("___________________");int j=0;while(j<10){j++;// 當j==3時,結束循環if(j==3){break;}System.out.println(j);}System.out.println("done!");} }
(3)goto
?盡管goto仍是Java的一個保留字,但并未在語言中得到正式使用
Java沒有goto。然而,在break和continue這兩個關鍵字的身上,我們仍然能看出一些goto的影子---帶標簽的break和continue。
“標簽”是指后面跟一個冒號的標識符,例如:label:
對Java來說唯一用到標簽的地方是在循環語句之前。而在循環之前設置標簽的唯一理由是:我們希望在其中嵌套另一個循環,由于break和continue關鍵字通常只中斷當前循環,但若隨同標簽使用,它們就會中斷到存在標簽的地方。
例:找質數
package control; // 找出1-20中的質數 public class DemoLable {public static void main(String[] args) {outer:for (int i = 1; i <= 20; i++) {// 判斷是否為質數for(int j=2;j<=i/2;j++){if(i%j==0){// 能整除,不是質數,跳過,判斷下一個icontinue outer;}}System.out.print(i+" ");}} }11、打印三角形及Debug
package control; // 打印三角形 public class Demo {public static void main(String[] args) {int num=5; // 行數for(int j=1;j<=num;j++){// 先打印最左邊的空白for (int i = num; i >= j; i--) {System.out.print(" ");}// 再打印三角形左半邊for(int i=1;i<=j;i++){System.out.print("*");}// 最后打印三角形右半邊for(int i=1;i<j;i++){System.out.print("*");}System.out.println();}} } package control; // 打印三角形public class Demo {public static void main(String[] args) {System.out.print("開始打印");System.out.println(":");// 重點是print即使是兩句話也會自動連起來,因為他們之間沒有分隔符int num=6; // 行數for(int j=1;j<=num;j++){// 先打印最左邊的空白for (int i = num; i >= j; i--) {System.out.print(" ");}// 再打印三角形左半邊for(int i=1;i<=2*j-1;i++){System.out.print("*");}System.out.println();}} }程序Debug
總結
以上是生活随笔為你收集整理的step1 基本语法流程控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino测试一块5路非自锁开关量输
- 下一篇: 安装CARLA Simulator错误