The third chapter, JAVA flow control statements (if statements, switch statements, for loops, while loops)

Today, listening to the teacher said many people are learning the software is recycled in this chapter are discouraging ~~~ cycle is also a difficulty, focus! So good children's shoes to learn the contents of this chapter of it!

In this chapter before said first wrote about this in detail with a chapter on data types: boolean (Boolean) data types.

boolean data type declared variable having a value of true or false.

Attached below a relational operator table

Note: relational operators is equal to two equal sign (==), instead of an equal sign (-), which refers to the value of bin operator. Result of the comparison is a Boolean value: true (true) or false (false)

true and false are the direct amount, such as 10 numbers. They are treated as the same as reserved words, do not use the program identifier. 

 

Sequence Structure

Structure is progressive sequence code starts to run down from the main function, the rest have nothing to say!

Select structure

There are two main selection structure
if statements and switch statements

What is the difference between them then: IF both can be for a single value judgment, can judge the value of a range; switch can only judge the value of a single data type switch required variables: byte, short, int, char, String, enumeration

 

The if statement

Format if statement is divided into three types: single-branch, branch and multi-branch double

Below are a flowchart thereof:

    

 

//基本格式

if(条件表达式){
      当条件为true时执行的代码;
}else{
      当条件为false时执行的代码;
}


//单if形式

if(条件表达式){
     当条件为true时执行的代码;
}


//if-else-if形式

if(条件1){

}else if(条件2){

}else if(条件3){

}else{

}


//嵌套if-else形式

if(条件1){
      条件1为true时执行的代码;
      if(条件2){
          条件2为true时执行的代码;
      }else{
          条件2为false时执行的代码;
      }
}else{
      条件1为false时执行的代码;
}

switch statement

Java provides a switch statement to effectively deal with the problem of multiple conditions.

switch(变量){
    case 值1:
          执行语句1;
          break;
    case 值2:
          执行语句2;
          break;
    case 值3:
          执行语句3;
          break;
    ........
    deafult:
          执行语句n;
          break;
}

Loop structure

Loop structure is the structure of the programmer will use circulating reproducibility problems encountered in the programming process to resolve the issue
circulation structure includes two aspects, for and while loops

for loop

The for loop format

for (initializing cycle; loop continuation condition 2; 4 step cycle) {
         3 loop body
}

Cycle: 1-2-3-4-2 until the termination condition -3-4-2-3-4-2- 3-4-2 until the cycle

Flowchart is as follows:

while loop

while( 循环继缞条件) { 
	循环体;
} 

有一种写while逻辑比较清楚但有危险(死循环)的写法(需要在合适的时候用break关键字跳出循环)

while(true){
     1.循环初始化
     2.循环的继续条件
     3.循环体
     4.循环的步长
}

do-while循环

do-while是先将do后面的循环体执行之后在进入循环

do{  
	循环体;  
} while ( 循环继续条件)

基础代码示例

输出:********

class Test01{
public static void main(String[] args){ 
    for(int line=1;line<=4;line++){
        for(int i=1;i<=8;i++){
            System.out.print("*");
           } 
            System.out.println();
        }
    }
}
/*
输出:
********
********
********
********

*/
 /*              i   j
            *           1   1
            **          2   1 2
            ***         3   1 2 3
            ****        4   1 2 3 4
            *****       5   1 2 3 4 5
            ******      6   1 2 3 4 5 6
            *****       7   1 2 3 4 5 
            ****        8   1 2 3 4
            ***         9   1 2 3
            **          10  1 2
            *           11  1
            j<=i && j<=12-i
            
        要求 * 循环体
        最多出现两个for关键
        只能有一个for嵌套
        */
//代码:

for(int i=1;i<=11;i++){

            for(int j=1;j<=i&&j<=12-i;j++){

                System.out.print("*");

            }

            System.out.println();

        }
        /*          k=|i-6| |k| 当前行的最大的空格数
                    k    i   j
                 * -5    1   1
                ** -4    2   1 2
               *** -3    3   1 2 3
              **** -2    4   1 2 3 4
             ***** -1    5   1 2 3 4 5
            ******  0    6   1 2 3 4 5 6
             *****  1    7   1 2 3 4 5 
              ****  2    8   1 2 3 4
               ***  3    9   1 2 3
                **  4    10  1 2
                 *  5    11  1
        */
//代码:

 for(int i=1;i<=11;i++){

            for(int k=1;k<=Math.abs(i-6);k++){

                System.out.print(" ");

            }

            for(int j=1;j<=i&&j<=12-i;j++){

                System.out.print("*");

            }

            System.out.println();

        }
        /*          k=|i-6| |k| 当前行的最大的空格数
                            k    i   j
                 *         -5    1        1
                * *        -4    2       1 2
               * * *       -3    3      1 2 3
              * * * *      -2    4     1 2 3 4
             * * * * *     -1    5    1 2 3 4 5
            * * * * * *     0    6   1 2 3 4 5 6
             * * * * *      1    7    1 2 3 4 5 
              * * * *       2    8     1 2 3 4
               * * *        3    9      1 2 3
                * *         4    10      1 2
                 *          5    11       1
        */

//代码:

for(int i=1;i<=11;i++){

            for(int k=1;k<=Math.abs(i-6);k++){

                System.out.print(" ");

            }

            for(int j=1;j<=i&&j<=12-i;j++){

                System.out.print("* ");

            }

            System.out.println();

        }
        /*          k=|i-6| |k| 当前行的最大的空格数
                            k    i   j
                 *         -5    1        1
                * *        -4    2       1 2
               *   *       -3    3      1 2 3
              *     *      -2    4     1 2 3 4
             *       *     -1    5    1 2 3 4 5
            *         *     0    6   1 2 3 4 5 6
             *       *      1    7    1 2 3 4 5 
              *     *       2    8     1 2 3 4
               *   *        3    9      1 2 3
                * *         4    10      1 2
                 *          5    11       1
                j==1 j==i j+i==12
        */

//代码:

for(int i=1;i<=11;i++){

            for(int k=1;k<=Math.abs(i-6);k++){

                System.out.print(" ");

            }

            for(int j=1;j<=i&&j<=12-i;j++){

                if(j==1||j==i||j+i==12){

                    System.out.print("* ");

                }else{

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

例题展示

import java.util.*;
    class Demo03_14{
        public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num = scanner.nextInt();
        for(int i=1;i<=num;i++){
        for(int j=1;j<=num-i;j++){
        System.out.print("  ");
    }
        for(int k=-(i-1);k<=i-1;k++){
        System.out.print(Math.abs(k)+1+" ");
    }
        System.out.println();
    }
  }
}

每行数字的规律 :

以第三行为例,3 2 1 2 1  一般先减后增的数字排列,我们的第一反应应该就是 数学中的绝对值函数

那么对应函数的   值域为 [ 1,3 ]   y  即   3   2  1  2  1     -------对应第 3 行

                      定义域为 [ - 2, 2 ]   x  即  -2  -1  0  1  2   ,解题思想就是 需要 建立 行数 i 与   x 之间的关系 把输出数字 用 x 表示

出来 , 可以发现 x   最小是   负的 行数  - 1  ,最大是  行数  - 1  ,那么输出数字就是  x 的绝对值 + 1 ;

空格的规律:空格数 依次递减  以第三行为例, 空格数 为  输出总行数   -  该行序列数 

 

class Demo03_15{
    public static void main(String[] args) {
        //Pattern1:
        for(int i = 1;i<=6;i++){
        for(int j = 1;j<=i;j++)
        System.out.print(j);
        System.out.println();
        
}
         System.out.println();
//Pattern2:
        for(int i = 6;i>=1;i--){
        for(int j = 1;j<=i;j++)
        System.out.print(j);
        System.out.println();
}
        System.out.println();
//Pattern3 :
        for(int i = 1;i<=6;i++){
        for(int j = 1; j<=(6-i);j++)
        System.out.print(" ");
        for(int j = i;j>=1;j--)
        System.out.print(j);
        System.out.println();
}
        System.out.println();
//Pattern4:
        for(int i = 1;i<=6;i++){
        for(int j = 1; j<=(i-1);j++)
        System.out.print(" ");
        for(int j = 1;j<=(7-i);j++)
        System.out.print(j);
        System.out.println();
    }
  }
}

pattern1:

每行都是由1递增到该行序列数,所以建立它与行数之间的关系即  x<= i  注意输出的是  数字 + 一个空格

pattern2

以第一行为例 1 2 3 4 5 6 可以发现它与行数 i 的 关系就是输出数字  x = 7- i  第一行 输出 1~6,第二行输出 1~5,

以此类推

pattern3:

与前两个图案不同的是  这个图案 加入了 空格

空格规律:第一行 5 个 空格 ,第二行 4 个 ,第三行 3 个 ,依次递减,那么建立 他与 行数 i 的关系 就是 k=6- i 

数字规律:以第三行为例  3 2 1  即 由行序数依次递减为 1 ;那么建立他与行数 之间的关系就是 输出数字数 等于行序数 

,输出数字最小 为1  且 依次递减 即 int x=i;x>=1;x--

 


public class Demo03_16{

    public static void main(String[] args){

        for(int i =1;i<=8;i++){      //输出行数

            for(int j =1;j<=8-i;j++){  //输出空格数

                System.out.print("    "); //每次输出4个空格

            }

            for(int x=-(i)+1;x<=i-1;x++ ){  //输出数字

                System.out.printf("%4d",(int)Math.pow(2,Math.abs(x)));

            }

        System.out.println();

        }

    }

}

累死了! 自我觉得总结的还算全面吧 这里说一下博客里的东西有一部分有参考其他博主 因为有些东西我理解但我不太会表达!

发布了6 篇原创文章 · 获赞 0 · 访问量 130

Guess you like

Origin blog.csdn.net/q1220668269/article/details/104255473