第二章:Java_基本语法_4 程序流程控制

来源:互联网 发布:网络摄像头忘记密码 编辑:程序博客网 时间:2024/05/21 00:16

4.1 顺序结构

程序从上到下逐行地执行,中间没有任何判断和跳转。

4.2 分支结构

根据条件,选择性地执行某段代码。
有 if…else 和 switch…case两种分支语句。

if-else语句
这里写图片描述

例如:
实现:
/*
score>=90 等级为:A
70<=score<90 等级为:B
60<=score<70 等级为C
score<60 等级为:D
/*

import java.util.Scanner;public class TestScore {    public static void main(String[] args) {        Scanner s = new Scanner(System.in);        System.out.println("请输入学生成绩:");        int score = s.nextInt();        char level;        if (score >= 90) {            level = 'A';            System.out.println("等级为:"+level);        }        if (score >= 70 && score < 90) {            level = 'B';            System.out.println("等级为:"+level);        }        if (score >= 60 && score < 70) {            level = 'C';            System.out.println("等级为:"+level);        }        if (score < 60) {            level = 'D';            System.out.println("等级为:"+level);        }    }}
import java.util.Scanner;public class TestScore {    public static void main(String[] args) {        Scanner s = new Scanner(System.in);        System.out.println("请输入学生成绩:");        int score = s.nextInt();        char level;        if (score > 90) {            level = 'A';        } else if (score >= 70) {            level = 'B';        } else if (score >= 60) {            level = 'C';        } else {            level = 'D';        }        System.out.println("等级为:" + level);    }}

switch-case语句
这里写图片描述

  1. 没有写 break; 语句,则在找到对应case语句后,还会继续向下执行。
  2. 其中变量可以是哪些类型? 可以是char,byte,short,int,枚举,String(jdk1.7),double、float等不可以。
  3. case 条件:其中条件只能是值,不能是取值范围。

4.3 循环结构

根据循环条件,重复性的执行某段代码。
有while、do…while、for三种循环语句。
注:JDK1.5提供了 foreach 循环,方便的遍历集合、数组元素。

①初始化条件 ②循环条件 ③迭代条件 ④循环体
for循环

  1. 格式:
    for(①;②;③){
    //④
    }
  2. 执行过程:①-②-④-③-②-④-③-….-④-③-②

while循环

格式:

while(②){


}

do-while循环

格式:
do{


}while(②)

另:
无限循环:
for( ; ; ){}
或者
while(true){
}
说明:一般情况下,在无限循环内部要有程序终止的语句,使用break实现,若没有,那就是死循环。

1)嵌套循环例子,实现如下图:
这里写图片描述

public class TestFor {    public static void main(String[] args) {        //上半部分        for(int i = 0;i < 5; i++){            for(int k = 0; k < 4-i; k++){                System.out.print(" ");            }            for(int j = 0;j < i+1; j++){                System.out.print("* ");            }            System.out.println();        }        //下半部分        for(int i = 0; i < 4; i++){            for(int k =0;k < i+1; k++){                System.out.print(" ");            }            for(int j = 0; j < 4-i; j++){                System.out.print("* ");            }            System.out.println();        }    }}

2)实现九九乘法表
这里写图片描述

public class TestJiuJiu {    public static void main(String[] args) {        for(int i = 1;i <= 9; i++){//一共有九行            for(int j = 1;j <= i; j++){//每行有 i 个等式                System.out.print(i + "*" + j + "=" + i*j + "\t");            }            System.out.println();        }    }}

break和continue关键字

break:使用在switch-case中或者循环中
如果使用在循环中,表示:结束“当前”循环

continue:使用在循环结构中,表示:结束“当次”循环

关于break和continue中标签的使用。

public class TestBreakContinue {    public static void main(String[] args) {        //break和continue中标签的使用        label:for (int i = 1; i < 5; i++) {            for (int j = 1; j < 10; j++) {                if(j % 4 == 0){                    //break;                    //continue;                    continue label;                }                System.out.print(j);            }            System.out.println();        }    }}
0 0
原创粉丝点击