• 第四章:控制结构


    目录

    一、顺序结构

    1.定义:

    二、分支结构

    1.单分支:

     2.双分支:

    3.多分支:

    4.嵌套分支:

     5.switch分支:

    6.switch与if的选择:

    三、循环结构

    1.for循环:

    2.while循环:

    3.do..while循环:

    4.多层循环(难点,重点):

    5.跳转控制语句----break:

    6.跳转控制语句---continue:

    7.跳转控制语句--return:

    四、课后作业

    1.100000元过路口,大于50000时每次交5%,小于50000时每次交1000,看看能过多少路口,要求while+break;

    2.判断一个数是否为水仙花数:


    一、顺序结构

    1.定义:

       代码都是由上往下去执行。

    二、分支结构

    1.单分支:

       if(条件表达式){执行代码块;可以有多条执行语句};只有一条语句大括号可以省略,但是建议写哦。

    案例:输入一个年龄大于18输出,成年人要对自己行为负责,送入监狱

    1. import java.util.*; //导入
    2. public class Hello{
    3. public static void main(String[]args){
    4. Scanner scanner = new Scanner(System.in);
    5. if(scanner.nextInt() >= 18){
    6. System.out.println("成年人要对自己行为负责,送入监狱");
    7. }
    8. }
    9. }

     2.双分支:

            if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 } else { 执行代码块;可以有多条执行语句 }

    案例:输入一个年龄大于18输出,成年人要对自己行为负责,送入监狱,小于输出这次放过你哦

    1. import java.util.*; //导入
    2. public class Hello{
    3. public static void main(String[]args){
    4. Scanner scanner = new Scanner(System.in);
    5. if(scanner.nextInt() >= 18){
    6. System.out.println("成年人要对自己行为负责,送入监狱");
    7. }else{
    8. System.out.println("这次放过你哦");
    9. }
    10. }
    11. }

    3.多分支:

           if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 } else if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 }

    案例:键盘输入判断:100:优秀,80-99:良好,60-80:一般,其他输出不及格啊

    1. import java.util.*; //导入
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int score = scanner.nextInt();
    6. if (score == 100) {
    7. System.out.println("very good");
    8. } else if (score >= 80 && score <= 99) {
    9. System.out.println("good");
    10. } else if (score >= 60 && score < 80) {
    11. System.out.println("just so so");
    12. } else {
    13. System.out.println("not good");
    14. }
    15. }
    16. }

    4.嵌套分支:

         if(条件表达式) { if( 条件表达式 ){ 执行语句 }}

    案例:输入成绩大于8.0,性别为其分配组

    1. import java.util.*; //导入
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. double score = scanner.nextInt();
    6. String gender = scanner.next();
    7. if(score>8.0){
    8. if (gender.equals("man")){ //注意这里判断是equals哦,不能用==
    9. System.out.println("man group score:"+score);
    10. }else {
    11. System.out.println("woman group score:"+score);
    12. }
    13. }else {
    14. System.out.println("sorry,you not engage us");
    15. }
    16. }
    17. }

     5.switch分支:

          switch( 表达式 ){ case 常量: 语句块;break;   default 语句块 break; } 注意没有break会一直执行的

    案例1:输入a、b,输出星期一二等等

    1. import java.util.*; //导入
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. char c1 = scanner.next().charAt(0);
    6. switch (c1) {
    7. case 'a':
    8. System.out.println("Moday");
    9. break;
    10. case 'b':
    11. System.out.println("Tuesday,wednesday ,Thursday Friday Saturday Sunday");
    12. break;
    13. default:
    14. System.out.println("please input right character");
    15. }
    16. }
    17. }

    案例2:345春季,678夏季,91011秋,1212春天

    1. import java.util.*; //导入
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. int season = scanner.nextInt();
    6. switch (season) {
    7. case 3:
    8. case 4:
    9. case 5:
    10. System.out.println("spring");
    11. break;
    12. case 6:
    13. case 7:
    14. case 8:
    15. System.out.println("summer");
    16. break;
    17. case 9:
    18. case 10:
    19. case 11:
    20. System.out.println("autumn");
    21. break;
    22. case 12:
    23. case 1:
    24. case 2:
    25. System.out.println("winter");
    26. break;
    27. default:
    28. System.out.println("input error,again");
    29. }
    30. }
    31. }

    6.switch与if的选择:

       具体数字不多,且是byte,short,int,char,enum使用switch

       区间的使用if挺好的 

    三、循环结构

    1.for循环:

             定义:for(int i = 0 ; i < 10 ; i++){  }  i为局部变量     for(;;){}  无限循环

    案例:打印1-100的9的整数,统计个数,并计算他的和

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

    2.while循环:

       定义 while(循环条件){ 循环体;循环迭代 }

    案例:输出1-100可以被3整除的数

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

    3.do..while循环:

       定义:do{代码块,循环体} while(表达式)    最少执行一次

    案例:统计1-200之间能被5整除不能被3整除的个数

    1. public class Hello {
    2. public static void main(String[] args) {
    3. int i = 1,count = 0;
    4. do {
    5. if (i % 5 == 0 && i % 3 != 0) {
    6. count++;
    7. System.out.println("i="+i);
    8. }
    9. i++;
    10. } while (i <= 200);
    11. System.out.println(count);
    12. }
    13. }

    4.多层循环(难点,重点):

    定义:for(){ for(){ for(){ } } }  套三层很多了

    案例1:打印九九乘法表

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

    案例2:打印空心金字塔

    1. public class Hello {
    2. public static void main(String[] args) { // *
    3. for (int i = 1; i <= 5; i++) { // **
    4. //打印空格 // * *
    5. for (int k = 1; k <= 5 - i; k++) { // * *
    6. System.out.print(" "); // *********
    7. }
    8. //打印*
    9. for (int j = 1; j <= 2 * i - 1; j++) {
    10. if (j == 1 || j == 2 * i - 1 || i == 5) {
    11. System.out.print("*");
    12. } else {
    13. System.out.print(" ");
    14. }
    15. }
    16. System.out.println();
    17. }
    18. }
    19. }

    5.跳转控制语句----break:

    定义:可以直接跳出循环结束循环,字符串比较使用equals

    *标签的使用,break可以使用标签控制跳出那个循环,尽量不要用在实际的开发中。

    1. public class Hello {
    2. public static void main(String[] args) {
    3. lable1:
    4. for (int i = 0; i < 4; i++) {
    5. lable2:
    6. for (int j = 0; j < 10; j++) {
    7. if(j == 2) break lable1;
    8. System.out.println("i"+j);
    9. }
    10. }
    11. }
    12. }

    案例:输入账号123456密码abc123,3次机会

    1. import java.util.*;
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. boolean flag = false;
    6. String username, password;
    7. for (int i = 0; i < 3; i++) {
    8. System.out.println("please input username");
    9. username = sc.next();
    10. System.out.println("please input password");
    11. password = sc.next();
    12. if (username.equals("123456") && password.equals("abc123")) {
    13. System.out.println("successs");
    14. flag = true;
    15. break;
    16. } else {
    17. System.out.println("faith,have " + (2 - i) + "chance");
    18. }
    19. }
    20. if (!flag) System.out.println("no chance");
    21. }
    22. }

    6.跳转控制语句---continue:

    定义:结束本次循环,进入下次循环。

    *标签的使用,continue可以使用标签控制结束那次循环进入下次循环,尽量不要用在实际的开发中。

    1. public class Hello {
    2. public static void main(String[] args) {
    3. lable1:
    4. for (int i = 0; i < 4; i++) {
    5. lable2:
    6. for (int j = 0; j < 10; j++) {
    7. if (j == 2) continue lable1;
    8. System.out.println("j " + j);
    9. }
    10. }
    11. }
    12. }

    7.跳转控制语句--return:

    定义:用在方法跳出方法,写main里面结束程序,与break一样

    四、课后作业

    1.100000元过路口,大于50000时每次交5%,小于50000时每次交1000,看看能过多少路口,要求while+break;

    1. public class Hello {
    2. public static void main(String[] args) {
    3. double money = 100000, count = 0;
    4. while (money > 1000) {
    5. if (money > 5000) {
    6. money *= 0.95;
    7. } else {
    8. money -= 1000;
    9. }
    10. if (money > 1000){
    11. count++;
    12. }
    13. }
    14. System.out.println(count);
    15. }
    16. }

    2.判断一个数是否为水仙花数

    1. import java.util.*;
    2. public class Hello {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. int floor = sc.nextInt();
    6. int h = floor / 100; //百位
    7. int t = floor / 10 % 10; // 十位
    8. int s = floor % 10; //个位
    9. if (floor == (h*h*h+t*t*t+s*s*s)){
    10. System.out.println("is floor");
    11. }else{
    12. System.out.println("not is floor");
    13. }
    14. }
    15. }
  • 相关阅读:
    单元测试入门篇
    python 获取上个月时间
    你的NET程序需要保护吗?Agile.net 6.6.X 注入式Crack
    monaco-editor 的 Language Services
    Temu如何提高销量,Temu能做无货源吗?——站斧浏览器
    SpringBoot整合Easy-ES操作演示文档
    springMvc55-自定义异常
    Tableau同比卡片制作
    wpa-supplicant移植
    ScreenCapture:通过DirectX 库进行屏幕捕获
  • 原文地址:https://blog.csdn.net/m0_61927991/article/details/126821360