好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

一篇文章带你了解JAVA结构化编程详情

1.什么是结构化编程

编程中只使用三大结构

不能使用goto语句

1972年美国科学家,发表论文证明所有的程序流程,只需要三大结构完成。

2.为什么要使用结构化编程?

把代码分成一个一个的代码块

极大的提高了代码的可 读性

3.三大结构

A. 循环结构

单线程(一个执行序列) 代码总是执行上一行,再执行下一行 在顺序结构中,要提防不小心产生的空语句

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//空语句实例:

public class BanchTest {

     public static void main(String[] args) {

         int i;

         for (i= 0 ;i< 5 ;i++); //这里的分号产生了空语句

         {  

         System.out.print(i);

         }

     }

}

//运行结果为:

//5

//正确例子:

public class BanchTest {

     public static void main(String[] args) {

         int i;

         for (i= 0 ;i< 5 ;i++) //注意这里没分号

         {

             System.out.print(i);

         }

     }

}

//运行结果

//01234

一行代码: 在JAVA中分号是行的分隔符。 在J S语言中分号、回车都是行的分隔符 在VB语言中,回车是分隔符

注释:只要回车是分隔符、都需要右续行符

B. 分支结构

单分支结构(if)

?

1

2

3

4

5

6

7

8

9

10

11

12

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int age = 19 ;

         if (age>= 18 ) { //注意这里的判断题的大括号不要省略,除非执行体和判别式在同一行

             System.out.println( "成年" );

         }

     }

}

双分支结构(if...else..)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int age = 1 ;

         if (age>= 18 ) { //符合判别表达式执行体

             System.out.println( "成年" );

         } else { //不复合判别表达式执行体

             System.out.println( "未成年" );

         }

     }

}

多分支结构

a.多选一的结构(多个分支结构、只有一个被执行)

使用else if 比单分支满足条件的做法,有更高的效率,所有的分支,同时只会执行一个

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int age = 37 ;

         if (age< 6 ) {

             System.out.println( "幼儿" );

         }

         else if (age< 18 ) {

             System.out.println( "少年" );

         }

         else if (age< 60 ) {

             System.out.println( "成年" );

         }

         else {

             System.out.println( "老年人" );

         }

     }

}

b.多选多的结构 (多个分支结构、有多个被同时执行)(switch)

格式:switch(key) 其中key支持的类型有:

1. int类型

2.可以自动升级为int的类型(byte、short、char、Integer)

3. enum(枚举类型)

4.在JDK8之后还支持string类型

在没有break时会全部被执行

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int key= 1 ;

         switch (key) {

         case 1 :

             //这个条件下执行的部分

             System.out.println( "成绩优秀" );

         case 2 :

             System.out.println( "成绩良好" );

         case 3 :

             System.out.println( "成绩一般" );

         default :

             //default等同于else,在switch中用default

             System.out.println( "成绩不良" );

         }

     }

/*运行结果为:
成绩优秀
成绩良好
成绩一般
成绩不良*/

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int key= 2 ;

         switch (key) {

         case 1 :

             //这个条件下执行的部分

             System.out.println( "成绩优秀" );

         case 2 :

             System.out.println( "成绩良好" );

         case 3 :

             System.out.println( "成绩一般" );

         default :

             //default等同于else,在switch中用default

             System.out.println( "成绩不良" );

         }

     }

}

/*key值不同值下的运行结果:
成绩良好
成绩一般
成绩不良
*/

在break之后,选中哪个就执行哪个(有多选一的效果)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int key= 1 ;

         switch (key) {

         case 1 :

             //这个条件下执行的部分

             System.out.println( "成绩优秀" );

             break ; //关闭

         case 2 :

             System.out.println( "成绩良好" );

             break ;

         case 3 :

             System.out.println( "成绩一般" );

             break ;

         default :

             //default等同于else,在switch中用default

             System.out.println( "成绩不良" );

         }

     }

}

跑穿(没有加break时,可以进入多个分支),只要以上条件都满足时,我都执行

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

         int key= 2 ;

         switch (key) {

         case 1 :

             //这个条件下执行的部分

             System.out.println( "成绩优秀" );

         case 2 :

             System.out.println( "成绩良好" );

         case 3 :

             System.out.println( "成绩一般" );

         default :

             //default等同于else,在switch中用default

             System.out.println( "成绩不良" );

             System.out.println( "继续加油" ); //满足上面的四个条件,不管key的值是多少,此代码都执行。

         }

     }

}

注释:设计跑穿就是为了支持多对多的关系。

C.重复结构

do while(until) 先执行循环,再判别。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

     int i = 0 ;

     int count= 5 ;

             do {

                 System.out.println(i);

                 i++; //循环变量自加

             }

             while (i<count);

     }

}

while 先判别,再执行循环

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

     int i = 0 ;

     int count= 5 ;

             while (i<count) {

                 System.out.println(i);

                 i++; //循环变量自加

             }

     }

}

注释:while循环要比do..while循环好多了,因为while循环的可读性要比后者好太多。

for (continue )跳出本次循环

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

     int count= 5 ;

     for ( int i = 0 ; i < count; i++) {

         if (i== 3 ) continue ; //跳出本次循环,跳到右边距离最近的}的左边

         System.out.print(i);

     }    

     }

}

/*运行结果

0124

*/

(break)跳出本层循环

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

     int count= 5 ;

     for ( int i = 0 ; i < count; i++) {

         if (i== 3 ) break ; //跳出本次循环,跳到右边距离最近的}的右边

         System.out.print(i);

     }    

     }

}

/*运行结果

012

*/

(return) 跳出函数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         test1();

     }

     private static void test1() {

     int count= 5 ;

     for ( int i = 0 ; i < count; i++) {

         if (i== 3 ) return ; //跳出本函数

         System.out.print(i);

     }    

     }

}

/*运行结果

012

*/

forEach(遍历集合) for循环和while循环的区别: for循环。定长循环,依靠固定循环次数作为结束条件。 while循环。标志性循环,依靠标志来结束循环(文件是否读到最后一行,用户的输入是否结束)。

4.函数(在JAVA中叫方法)

面向对象的语言叫方法

面向过程的语言叫函数

在JAVA中没有函数这么一说,只有方法一说。实际上方法就是函数,函数就是方法,只是在不同的语言不同的称呼而已。有class声明的就是类。类就是对一类事物的声明,在JAVA中函数就是方法,就是后面带有()的。类只是声明,对象才是真实存在的可以干活的东西,所有的方法(也就是函数)都需要用对象去调用(不包括静态的static)

什么是函数(函数,子程序,过程)

函数是一段可以复用的子程序。

函数(function):有返回值(在C语言中void表示没有返回值)

过程(procedure):无返回值

为什么要使用函数

代码的复用,提高了效率

什么时候写一个函数 复用

在写某个代码时,发现这个代码似曾相识(写过)

分割逻辑

不应该超过50行

一个方法最好只能有一个功能(单一功能原则)

功能简单更加利于复用

函数的定义 函数定义的区域:类体以内,方法以外。 一模一样(同名同参)的函数只能调用一次 方法定义有7个组成要素:

?

1

2

3

public static void main (String[] args) throws Exception {

}

1.权限修饰符 public

2.static/final/abatract

3.返回类型 void

4.方法(函数)名称 main

5.形式参数列表 (String[] args)

6.异常列表 throws Exception

7.{ } 方法体

注释:3 4 5 是必须的

?

1

2

3

//两个例子

void test1 (){}

abstaract void test1(); //抽象方法

函数如何返回返回值 只要函数声明一个不是void返回类型,所有分支必然需要一个返回值。 返回值通过return语句进行返回的,而且数据类型必须和方法声明的返回类型匹配。 函数 的调用 如何调用一个方法

1.可以多次调用

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         new BanchTest().test1();

}

         void test1 (){

             test2();

             test2();

             test2();

         }

         void test2() {

             System.out.println( "JAVA使我快乐" );

         }

     }

2.至少在现在的时候方法(函数)的实参(调用的时候叫实参)和形参(定义的时候叫形参)必须一致

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.kfm.leiyawen.Test0831;

public class BanchTest {

     public static void main(String[] args) {

         new BanchTest().test1();

}

         void test1 (){

             test2( 1 ); //括号里面的是实参

             test2( 2 );

             test2( 3 );

         }

         void test2( int order) { //函数定义时叫形参

             System.out.println( "JAVA使我快乐" +order);

         }

     }

运行结果
/*
JAVA使我快乐1
JAVA使我快乐2
JAVA使我快乐3
*/

函数可以直接或间接的调用自己

递归

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/m0_46331585/article/details/120050000

查看更多关于一篇文章带你了解JAVA结构化编程详情的详细内容...

  阅读:8次