根據編寫的順序,從上到下,依次執行。數組
代碼演示oop
// 順序結構 public class Demo01Sequence { public static void main(String[] args) { //順序執行,根據編寫的順序,從上到下運行 System.out.println("牀前明月光"); System.out.println("疑是地上霜"); System.out.println("舉頭望明月"); System.out.println("低頭思故鄉"); } }
if語句第一種形式: ifspa
格式:3d
執行流程圖code
代碼演示單if語句的使用blog
public class DemoIf { //判斷2個變量是否相等 public static void main(String[] args) { // 定義兩個變量 int a = 10; int b = 20; //a == b 是關係表達式 if (a == b) { //語句體 System.out.println("a等於b"); } int c = 10; //a == c 是關係表達式 if (a == c) { //語句體 System.out.println("a等於c");//a等於c } System.out.println("其餘代碼");//其餘代碼 } }
執行流程圖開發
代碼演示if.. else語句的使用字符串
// 標準的if-else語句, 判斷給定的數據是奇數仍是偶數 public class Demo03IfElse { public static void main(String[] args) { int num = 666; // 若是除以2可以餘數爲0,說明是偶數 if (num % 2 == 0) { //語句體1 System.out.println("偶數");//偶數 } else { //語句體2 System.out.println("奇數"); } System.out.println("其餘代碼");//其餘代碼 } }
執行流程圖it
代碼演示其基本使用for循環
/* 指定考試成績,判斷學生等級 90-100 優秀 80-89 好70-79 良 60-69 及格 60如下 不及格 */ public class Demo05IfElsePractise { public static void main(String[] args) { int score = 120; if (score >= 90 && score <= 100) { System.out.println("優秀"); } 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 if (score >= 0 && score < 60) { System.out.println("不及格"); } else { // 單獨處理邊界以外的不合理狀況 System.out.println("數據錯誤");//數據錯誤 } } }
// 題目:使用三元運算符和標準的if-else語句分別實現:取兩個數字當中的最大值 public class Demo06Max { public static void main(String[] args) { int a = 105; int b = 20; /* 首先使用三元運算符 int max = a > b ? a : b; */ // 使用今天的if語句 int max; if (a > b) { max = a; } else { max = b; } System.out.println("最大值:" + max);//最大值:105 } }
格式:
執行流程圖
代碼演示,基本使用
public class Demo07Switch { public static void main(String[] args) { //定義變量,判斷是星期幾 int weekday = 1; //switch語句實現選擇 switch (weekday) { 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; case 7: System.out.println("星期日"); break; default: System.out.println("數據不合理"); break; // 最後一個break語句能夠省略,可是強烈推薦不要省略 } } }
switch語句使用的注意事項:
格式:
執行流程圖
代碼演示,基本使用
//循環練習:使用for循環,計算1-100之間的偶數和 public class DemoFor { public static void main(String[] args) { //1.定義一個初始化變量,記錄累加求和,初始值爲0 int sum = 0; //2.利用for循環獲取1‐100之間的數字 for (int i = 1; i <= 100; i++) { //3.判斷獲取的數組是奇數仍是偶數 if (i % 2 == 0) { //4.若是是偶數就累加求和 sum += i; } } //5.循環結束以後,打印累加結果 System.out.println("sum:" + sum);//sum:2550 } }
執行流程圖
代碼演示,基本使用
//循環練習:使用while循環,計算1-100之間的偶數和 public class DemoWhile { public static void main(String[] args) { //定義一個初始化變量,記錄累加求和,初始值爲0 int sum = 0; //定義初始化表達式 int i = 1; //使用while循環讓初始化表達式的值變化 while (i < 101) { if (i % 2 == 0) { //累加求和 sum += i; } //步進表達式改變變量的值 i++; } //打印求和的變量 System.out.println("1‐100的偶數和是:" + sum);//1‐100的偶數和是:2550 } }
執行流程圖
代碼演示,基本使用
/* do-while循環的標準格式: do { 循環體 } while (條件判斷); 擴展格式: 初始化語句 do { 循環體 步進語句 } while (條件判斷); */ //循環練習:使用do..while循環,計算1-100之間的偶數和 public class Demo11DoWhile { public static void main(String[] args) { //定義一個初始化變量,記錄累加求和,初始值爲0 int sum = 0; // 初始化語句 int i = 0; do { //判斷獲取的數組是奇數仍是偶數 if (i % 2 == 0) { //若是是偶數就累加求和 sum += i; } i++; // 4. 步進語句 } while (i <= 100); // 2. 條件判斷 //打印求和的變量 System.out.println("1‐100的偶數和是:" + sum);//1‐100的偶數和是:2550 } }
public class LoopDifference { public static void main(String[] args) { for (int i = 1; i < 0; i++) { System.out.println("Hello"); } // System.out.println(i); 這一行是錯誤寫法!由於變量i定義在for循環小括號內,只有for循環本身才能用。 int i = 1; do { System.out.println("World"); i++; } while (i < 0); // 如今已經超出了do-while循環的範圍,咱們仍然可使用變量i System.out.println(i); // 2 } }
用法有常見的兩種:
關於循環的選擇,有一個小建議:
代碼演示
public class DemoBreak { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { // 若是但願從第4次開始,後續全都不要了,就要打斷循環 if (i == 4) { // 若是當前是第4次 break; // 那麼就打斷整個循環 } System.out.println("Hello" + i); } } }
常見用法
代碼演示
public class DemoContinue { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 4) { // 若是當前是第4層 continue; // 那麼跳過當前次循環,立刻開始下一次(第5層) } System.out.println(i + "層到了。"); } } }
代碼演示
/* 永遠停不下來的循環,叫作死循環。 死循環的標準格式: while (true) { 循環體 } */ public class DemoDeadLoop { public static void main(String[] args) { while (true) { System.out.println("I Love Java!"); } // System.out.println("Hello"); 錯誤 } }
嵌套循環格式:
代碼實現
public class Test { public static void main(String[] args) { //5*8的矩形,打印5行*號,每行8個 // 外循環5次,內循環8次 for (int i = 0; i < 5; i++) { for (int j = 0; j < 8; j++) { //不換行打印星號 System.out.print("*"); }//內循環打印8個星號後,須要一次換行 System.out.println(); } } }