国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

黑馬程序員_JavaSE學習總結第04天_Java基礎語法3

2019-11-15 00:01:34
字體:
來源:轉載
供稿:網友
黑馬程序員_javaSE學習總結第04天_Java基礎語法3

------- android培訓、java培訓、期待與您交流! ----------

04.01 選擇結構switch語句的格式及其解釋

switch語句的格式:

switch(表達式)

{

case 值1:語句體1;break;

case 值2:語句體2;break;

......

default:語句體n+1;break;

}

格式解釋:

switch表示這是switch語句

表達式的取值:byte,short,int,char,JDK5以后可以是枚舉類型,JDK7以后可以是String類型

case后面跟的是要和表達式進行比較的值

語句體部分可以是一條或多條語句

break表示中斷,結束的意思,可以結束switch語句

default語句表示所有情況都不匹配的時候,就執行該處的內容,和if語句的else相似。

04.02 選擇結構switch語句的基本使用

switch語句執行流程:

首先計算出表達式的值

其次,和case后的值依次比較,一旦有對應的值,就會執行相應的語句,在執行的過程中,遇到break就會結束。

最后,如果所有的case都和表達式的值不匹配,就會執行default語句體部分,然后程序結束。

流程圖:

例:鍵盤錄入1~7,對應輸出星期一到星期日

 1 import java.util.Scanner; 2 class Demo 3 { 4    public static void main(String[] args) 5    { 6       Scanner sc = new Scanner(System.in); 7       System.out.

04.03 選擇結構switch語句的注意事項

1.case后面只能是常量,不能是變量,而且,多個case后面的值不能出現相同的。

2.default語句可以省略,一般不建議。除非判斷的值是固定的。

3.break語句可以省略,一般不建議。因為得到的結果可能不是你想要的。

4.default語句可以出現在switch語句任意位置。

5.switch語句的結束條件遇到break或者執行到程序的末尾。

04.04 選擇結構switch語句練習1看程序寫結果

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int x = 2; 6       int y = 3; 7       switch(x) 8       { 9          default:10             y++;break;11          case 3:12             y++;13          case 4:14             y++;15       }16       System.out.println("y="+y); //y=417       18       int a = 2;19       int b = 3;20       switch(a)21       {22          default:23             b++;24          case 3:25             b++;26          case 4:27             b++;28       }29       System.out.println("b="+b); //b=630    }31 }

04.05 選擇結構switch語句練習2單項選擇題

 1 import java.util.Scanner; 2 class Demo 3 { 4    public static void main(String[] args) 5    { 6       Scanner sc = new Scanner(System.in); 7       System.out.println("int類型占幾個字節?"); 8       System.out.println("A.2個/r/nB.4個/r/nC.6個/r/nD.8個"); 9       System.out.println("請輸入答案:");10     //獲取字符串的第一個字符11       char ch = sc.nextLine().charAt(0);12       switch(ch)13       {14          case 'A':15             System.out.println("選擇錯誤!");break;16          case 'B':17             System.out.println("選擇正確!");break;18          case 'C':19             System.out.println("選擇錯誤!");break;20          case 'D':21             System.out.println("選擇錯誤!");break;22          default:23             System.out.println("沒有該選項");24       }25    }26 }

運行結果:

int類型占幾個字節?A.2個B.4個C.6個D.8個請輸入答案:B選擇正確

04.06 選擇結構switch語句練習3表達式是字符串

 1 import java.util.Scanner; 2 class Demo 3 { 4    public static void main(String[] args) 5    { 6       Scanner sc = new Scanner(System.in); 7       System.out.println("請輸入要翻譯的單詞"); 8       System.out.println("hello/r/nworld/r/nchina/r/ndevelop"); 9       String str = sc.nextLine();10       switch(str)11       {12          case "hello":13             System.out.println("你好");break;14          case "world":15             System.out.println("世界");break;16          case "china":17             System.out.println("中國");break;18          case "develop":19             System.out.println("開發");break;20          default:21             System.out.println("該單詞暫時無法翻譯");22       }23    }24 }

04.07 選擇結構switch和if語句的各自使用場景

if語句使用場景:

針對結果是boolean類型的判斷

針對一個范圍的判斷

針對幾個常量值的判斷

switch語句使用場景:

針對幾個常量值的判斷

04.08 循環結構循環語句概述

循環語句可以在滿足循環條件的情況下,反復執行某一段代碼,這段被重復執行的代碼被稱為循環體語句,當反復執行這個循環體時,需要在合適的時候把循環判斷條件修改為false,從而結束循環,否則循環將一直執行下去,形成死循環。

循環語句的組成:

初始化語句:一條或者多條語句,這些語句完成一些初始化操作。

判斷條件語句:這是一個boolean 表達式,這個表達式能決定是否執行循環體。

循環體語句:這個部分是循環體語句,也就是我們要多次做的事情。

控制條件語句:這個部分在一次循環體結束后,下一次循環判斷條件執行前執行。通過用于控制循環條件中的變量,使得循環在合適的時候結束。

04.09 循環結構for語句的格式和基本使用

for循環語句格式:

for(初始化語句;判斷條件語句;控制條件語句)

{

循環體語句;

}

執行流程:

A:執行初始化語句

B:執行判斷條件語句,看其結果是true還是false。如果是false,循環結束。如果是true,繼續執行。

C:執行循環體語句

D:執行控制條件語句

E:回到B繼續

流程圖:

例:在控制臺輸出10次helloworld

1 for(int i = 0;i < 10; i++)2 {3     System.out.println("helloworld");4 }

04.10 循環結構for語句的注意事項

注意事項

1.判斷條件語句的結果是一個boolean類型

2.循環體語句如果是一條語句,大括號可以省略;如果是多條語句,大括號不能省略。建議永遠不要省略。

3.一般來說:有左大括號就沒有分號,有分號就沒有左大括號

04.11 循環結構for語句的練習1獲取數據

在控制臺輸出數據1-10

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       for(int i = 1;i <= 10; i++) 6       { 7          System.out.println(i); 8       } 9    }10 }

04.12 循環結構for語句的練習2求和思想

求出1-10之間數據之和

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       //定義第一個加數 6       int sum = 0; 7       for(int i = 1;i <= 10; i++) 8       { 9          //sum = sum + i;10          sum += i;11       }12       System.out.println("sum="+sum);13    }14 }

04.13 循環結構for語句的練習3偶數和

求出1-100之間偶數和

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       //定義第一個加數 6       int sum = 0; 7       for(int i = 1;i <= 100; i++) 8       { 9          //判斷該數是否為偶數10          if(i % 2 == 0)11          {12             sum += i;13          }14       }15       System.out.println("sum="+sum);16    }17 }

04.14 循環結構for語句的練習4階乘

求5的階乘,即計算1*2*3*4*5的積

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int result = 1; 6       for(int i = 1;i <= 5; i++) 7       { 8          result *= i; 9       }10       System.out.println("result="+result);11    }12 }

04.15 循環結構for語句的練習5水仙花

在控制臺輸出所有的水仙花數,水仙花數是一個三位數,各位數字的立方之和等于該數本身

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       for(int i = 100;i < 1000; i++) 7       { 8          //獲取個位數 9          int ge = i % 10;10          //獲取十位數11          int shi = i / 10 % 10;12          //獲取百位數13          int bai = i / 100 % 10;14          if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i)15          {16             System.out.println(i+"滿足條件");17             count++;18          }19       }20       System.out.println("共找到"+count+"個數");21    }22 }

運行結果:

153滿足條件370滿足條件371滿足條件407滿足條件共找到4個數

04.16 循環結構for語句的練習6回文數

在控制臺輸出所有的回文數并統計個數,回文數就是個位等于萬位,十位等于千位,個位+十位+千位+萬位=百位,一共5位數

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       for(int i = 10000;i < 100000; i++) 7       { 8          int ge = i % 10; 9          int shi = i / 10 % 10;10          int bai = i / 100 % 10;11          int qian = i / 1000 % 10;12          int wan = i / 10000 % 10;13          if(ge == wan && shi == qian && ge+shi+qian+wan == bai)14          {15             System.out.println(i+"滿足條件");16             count++;17          }18       }19       System.out.println("共找到"+count+"個數");20    }21 }

04.17 循環結構for語句的練習7統計思想

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       for(int i = 100;i < 1000; i++) 7       { 8          int ge = i % 10;   9          int shi = i / 10 % 10;10          int bai = i / 100 % 10;11          if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i)12          {13             count++;14          }15       }16       System.out.println("共找到"+count+"個數");17    }18 }

04.18 循環結構for語句的練習8同時取余問題

統計1~1000之間同時滿足 對3整除余2 對5整除余3 對7整除余2 的所有數據并輸出至控制臺

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       for(int i = 1;i <= 1000; i++) 7       { 8          if(i % 3 == 2 && i % 5 == 3 && i % 7 == 2) 9          {10             System.out.println(i+"滿足條件");11             count++;12          }13       }14       System.out.println("共找到"+count+"個數");15    }16 }

04.19 循環結構while語句的格式和基本使用

while循環語句格式:

while(判斷條件語句)

{

循環體語句;

}

擴展格式:

初始化語句;

while(判斷條件語句)

{

循環體語句;

控制條件語句;

}

執行流程:先執行判斷條件語句,如果是false,循環結束。如果是true,繼續執行循環體語句。直到判斷條件語句是false。

流程圖:

例:

1 int i = 0;2 while(i < 10)3 {4    System.out.println("hello");5    i++;6 }

04.20 循環結構while語句的練習1求和思想

求1~100之和

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int sum = 0; 6       int i = 1; 7       while(i <= 100) 8       { 9          sum += i;10          i++;11       }12       System.out.println("sum = "+sum);13    }14 }

04.21 循環結構while語句的練習2統計思想

統計水仙花數

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       int i = 100; 7       while(i <= 1000) 8       { 9          int ge = i % 10;10          int shi = i / 10 % 10;11          int bai = i / 100 % 10;12          if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i)13          {14             System.out.println(i);15             count++;16          }17          i++;18       }19       System.out.println("count = "+count);20    }21 }

04.22 循環結構while語句和for語句的區別

使用區別:

控制條件語句所控制的那個變量,在for循環結束后,就不能再被訪問到了,而while循環結束還可以繼續使用,如果你想繼續使用,就用while,否則推薦使用for。原因是for循環結束,該變量就從內存中消失,能夠提高內存的使用效率。

場景區別:

for循環適合針對一個范圍判斷進行操作

while循環適合判斷次數不明確操作

04.23 循環結構while語句的練習珠穆朗瑪峰

現在有一張足夠大的紙張,厚度為:0.01m。請問,折疊多少次,就可以保證厚度不低于8848m?

由于次數不確定,使用while循環

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       int count = 0; 6       float num = 0.01f; 7       while(num < 8848) 8       { 9          count++;10          num *= 2;11          System.out.println("第"+count+"次"+num+"m");12       }13       System.out.println("共計"+count+"次");14    }15 }

04.24 循環結構do...while語句的格式和基本使用

do...while循環語句格式:

基本格式:

do

{

  循環體語句;

}

while

(判斷條件語句);

擴展格式:

初始化語句;

do

{

  循環體語句;

  控制條件語句;

}

while(判斷條件語句);

流程圖:

例:

1 int i = 0;2 do3 {4    System.out.println("hello");5    i++;6 }7 while (i < 5);

04.25 循環結構三種循環語句的區別

三種循環語句的區別:

1.do...while循環至少會執行一次循環體

2.for循環和while循環必須先判斷條件是否成立,只有在條件成立的時候才會去執行循環體

注意事項:寫程序優先考慮for循環,再考慮while循環,最后考慮do...while循環。

04.26 循環結構循環注意事項之死循環

兩種簡單的死循環

1.while(true){}

2.for(;;){}

04.27 循環結構循環嵌套輸出4行5列的星星

輸出一個4行5列的星星(*)圖案,如下所示:

* * * * *

* * * * *

* * * * *

* * * * *

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       //外循環控制行數 6       for(int i = 0;i < 4;i++) 7       { 8          //內循環控制列數 9          for(int j = 0;j < 5;j++)10          {11             //打印一顆*,不帶換行12             System.out.print("* ");13          }14          //換行15          System.out.println();16       }17    }18 }

04.28 循環結構循環嵌套輸出正三角形

輸出以下圖形

*

* *

* * *

* * * *

* * * * *

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       for(int i = 0;i < 5;i++) 6       { 7          for(int j = 0;j <= i;j++) 8          { 9             System.out.print("* ");10          }11          System.out.println();12       }13    }14 }

04.29 循環結構九九乘法表

輸出九九乘法表

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       for(int i = 1;i <= 9;i++) 6       { 7          for(int j = 1;j <= i;j++) 8          { 9             System.out.print(j+"*"+i+"="+(i*j)+"/t");10          }11          System.out.println();12       }13    }14 }

'/x':x表示任意,轉義字符

'/t':TAB鍵,制表符

'/r':回車

'/n':換行

04.30 控制跳轉語句break語句

Java就提供了break,continue和return來實現控制語句的跳轉和中斷

break:中斷

continue:繼續

return:返回

break的使用場景:

1.在選擇結構switch語句中 2.在循環語句中

離開使用場景的存在是沒有意義的

break的作用:

1.跳出單層循環

2.跳出多層循環(帶標簽的跳出,格式: 標簽名:循環語句,標簽名要符合Java的命名規則)

例1:跳出單層循環

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       for(int i = 1;i <= 10;i++) 6       { 7          if(i == 3) 8          { 9             break;10          }11          System.out.println("hello");12       }13    }14 }

運行結果:

hellohello

例2:跳出多層循環

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       AA:for(int i = 0;i < 3;i++) 6       { 7          BB:for(int j = 0;j < 3;j++) 8          { 9             if(j == 2)10             {11                break AA;//跳出外循環12             }13             System.out.print("* ");14          }15          System.out.println();16       }17    }18 }

運行結果:

* *

04.31 控制跳轉語句continue語句

continue的使用場景:在循環語句中,離開使用場景的存在是沒有意義的

continue的作用:結束本次循環,繼續下一次循環,也可以帶標簽使用

例:

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       for(int i = 0;i < 6;i++) 6       { 7          if(i == 4) 8          { 9             continue;10          }11          System.out.print(i+" ");12       }13    }14 }

運行結果:

0 1 2 3 5

04.32 控制跳轉語句return語句

return關鍵字不是為了跳轉出循環體,更常用的功能是結束一個方法,也就是退出一個方法。跳轉到上層調用的方法。

04.33 while語句和break的結合使用小芳存錢

小芳的媽媽每天給她2.5元錢,她都會存起來,但是,每當這一天是存錢的第5天或者5的倍數的話,她都會花去6元錢,請問,經過多少天,小芳才可以存到100元錢?

 1 class Demo 2 { 3    public static void main(String[] args) 4    { 5       //每天存的錢 6       double dayMoney = 2.5; 7        8       //存錢的初始化值是0 9       double daySum = 0;10  11       //從第1天開始存12       int dayCount = 1;13  14       while(true)15       {16          daySum += dayMoney;17          if(daySum >= 100)18          {19             System.out.println("共花了"+dayCount+"天");20             break;21          }22          if(dayCount % 5 == 0)23          {24             //花去6元錢25             daySum -= 6;26          }27          dayCount++;28       }29    }30 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桓仁| 新巴尔虎右旗| 郴州市| 保定市| 盐山县| 青阳县| 新化县| 英吉沙县| 米脂县| 鄂温| 崇文区| 科技| 酒泉市| 闻喜县| 哈巴河县| 三明市| 乡宁县| 民和| 财经| 博爱县| 合肥市| 玛沁县| 义马市| 五寨县| 奉节县| 龙泉市| 威信县| 正蓝旗| 云和县| 禹州市| 惠州市| 衡阳县| 绥江县| 江川县| 广河县| 施秉县| 嘉荫县| 美姑县| 同江市| 沂南县| 内江市|