JAVA程序流程控制

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();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值