java流程程序控制


java 提供了一些流程控制语句,来控制语句和执行语句。大致分为三类:顺序结构,分支结构,循环结构。

一、顺序结构(默认)

程序中没有其他的结构,按照代码的先后顺序,依次执行程序中的大多数代码。流程如下:
开始 =》语句A =》语句B =》语句C=》结束

package dreamcode;
public class snow1 {
    public static void main(String[] args) {
        System.out.println('A');  // A
        System.out.println('B');  // B
        System.out.println('C');  // C
    }
}

顺序结构就是按照代码从上到下的顺序依次执行。

二、分支结构

1.if分支

根据判定的结果(真或假)决定执行某个分支的代码。
if分支有三种格式:
格式一:

if (条件表达式){
	语句体;
}

eg:

package dreamcode;

public class xuanze {
    public static void main(String[] args) {
        int hearBeat =70;
        if(hearBeat>60 || hearBeat<120){
            System.out.println("你的心跳数据是:"+hearBeat+",指标属于正常,"); // 你的心跳数据是:70,指标属于正常,
        }
        System.out.println("测试结束"); //测试结束
    }
}

格式二:

if(条件表达式){
	语句体1;
}else{
	语句体2}

eg:

package dreamcode;

public class xuanze {
    public static void main(String[] args) {
        int money = 5999;
        if(money>=1314){
            System.out.println("您当前发送成功~~~"); // 您当前发送成功~~~
        }else {
            System.out.println("您自己都没钱,就别发了~~~");
        }
    }
}

格式三:

if(条件表达式1){
	语句体1;
}else if(条件表达式2){
	语句体2}else if(条件表达式3){
	语句体3;
}
...
else{
	语句体n+1;
}

eg:

package dreamcode;

public class xuanze {
    public static void main(String[] args) {
        int score = 99;
        if(score >= 0 && score < 60){
            System.out.println("很遗憾你的成绩不及格!");
        }else if(score >= 60 && score < 90){
            System.out.println("恭喜你的成绩及格了!");
        }else if(score >= 90 && score < 100){
            System.out.println("恭喜你获得很优秀的成绩!"); // 恭喜你获得很优秀的成绩!
        }else {
            System.out.println("你的成绩有误");
        }
    }
}

2.switch分支

匹配条件去执行分支,适合做值匹配的分支选择,结构清晰,格式良好。
执行流程:

switch(表达式){
	case1:
		执行代码...;
		break;
	case2:
		执行代码...;
		break;
		...
	case 值n-1:
		执行代码...;
		break;
	default:
		执行代码n;
}
  • 先执行表达式的值,拿着这个值去与case后的值进行匹配。
  • 匹配那个case的值为true就执行那个case,遇到break就跳出switch分支。
  • 如果case后的值都不匹配则执行default代码。
    eg:
package xuanze;

public class swjiegou {
    public static void main(String[] args) {
        String weekday = "周二";
        switch (weekday) {
            case "周一":
                System.out.println("埋头苦干,解决bug");
                break;
            case "周二":
                System.out.println("请求大牛程序员帮忙"); // 请求大牛程序员帮忙
                break;
            case "周三":
                System.out.println("今晚啤酒、龙虾、小烧烤");
                break;
            case "周四":
                System.out.println("主动帮助新来的程序员解决bug");
                break;
            case "周五":
                System.out.println("今晚吃鸡");
                break;
            case "周六":
                System.out.println("旅游");
                break;
            case "周日":
                System.out.println("郁郁寡欢,准备上班");
                break;
            default:
                System.out.println("数据有误!");
        }
    }
}

switch分支注意事项:

  • 表达式类型只能是byte、short、int、char ,JDK5开始支持枚举,JDK7开始支持String、不支持double、float、long
  • case给出的值不允许重复,且只能是字面量,不能是变量
  • 不要忘记写break,否则会出现穿透现象

switch的穿透性:
如果代码执行到没有写break的case块,执行完后将直接进入下一个case块执行代码(而且不会进行任何匹配),直到遇到break才跳出分支,这就是switch的穿透性。
switch穿透性结构表达:

switch(表达式){
	case1:
		执行代码1case2:
		执行代码2;
		...
	case 值n-1:
		执行代码n-1;
		break;
	default:
		执行代码 n;
} 

eg:

package xuanze;

import java.time.Month;

public class swct {
    public static void main(String[] args) {
        int manth = 7;
        switch (manth){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.println(manth+"是31天!"); // 7是31天!
                break;
            case 2:
                System.out.println(manth+"闰年为29天,非闰年为是28天!");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println(manth+"是30天!");
                break;
            default:
                System.out.println("数据有误!");
        }
    }
}

三、循环结构

1.for循环

  • 基本格式:
for(初始化语句;循环条件;迭代语句){
	循环体语句(重复执行的代码);
}
  • eg:
package xunhuan;

public class fxunhuan {
    public static void main(String[] args) {
        for(int i=0; i<3; i++){
            System.out.println("Hello World");  // Hello World  Hello World  Hello World 
        }
    }
}
  • 流程图:
    在这里插入图片描述
  • eg1:求1-5之间的数据和,并把求和结构在控制台输出。
package xunhuan;

public class fxunhuan {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1; i<=5; i++){
            sum+=i;
        }System.out.println("1-5的和是:"+sum); // 1-5的和是:15
    }
}
  • eg2:求1-10之间的奇数和,并把求和结构在控制台输出。
    法一:
package xunhuan;

public class fxunhuan {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1; i<=10; i++){
            if(i % 2 == 1){
                sum+=i;
            }
        }
        System.out.println("1-10的奇数和是:"+sum); // 1-10的奇数和是:25
    }
}

法二:

package xunhuan;

public class fxunhuan {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1; i<=10; i+=2){
            sum+=i;
        }
        System.out.println("1-10的奇数和是:"+sum); // 1-10的奇数和是:25
    }
}
  • eg3:水仙花数—>水仙花数是一个三位数;水仙花数的个位、十位、百位的数字立方和等于原数。在控制台输出所有满足要求的三位数。
package xunhuan;

public class fxunhuan {
    public static void main(String[] args) {
        for(int i=100; i<=999; i++){
            if(((i%10)*(i%10)*(i%10)+(i%100/10)*(i%100/10)*(i%100/10)+(i/100)*(i/100)*(i/100)) == i){
                System.out.println(i); // 153  370  371  407
            }
        }
    }
}

2.while循环

  • 基本格式:
 初始化语句;
 while (循环条件){
	循环体语句(被重复执行的代码);
	迭代语句;
}
  • eg:
package xunhuan;

public class wlxh {
    public static void main(String[] args) {
        int i = 0;
        while (i<3){
            System.out.println("Hello Word"); // Hello Word   Hello Word   Hello Word
            i++;
        }
    }
}
  • 流程图:
    在这里插入图片描述
  • eg1:世界最高的山峰是珠穆朗玛峰(8848.86米=8848860毫米),假如我有一张足够大的纸,它的厚度是0.1毫米,请问,折叠多少次,可以折叠成珠穆朗玛峰的高度。
package xunhuan;

public class wleg {
    public static void main(String[] args) {
        double peakHeight = 8848860;
        double paperThickness = 0.1;
        int count = 0;
        while (paperThickness < peakHeight){
            count ++;
            paperThickness += paperThickness;
        }
        System.out.println("折叠次数:"+count);  // 折叠次数:27
    }
}

3.do-while循环

  • 基本格式:
初始化语句;
do {
	循环体语句;
	迭代语句;
}while(循环条件);
  • eg:
package xunhuan;

public class dowl {
    public static void main(String[] args) {
        int i = 0;
        do{
            System.out.println("Hello Word");
            i++;
        }while (i<3); // Hello Word    Hello Word    Hello Word
    }
}
  • 流程图:
    在这里插入图片描述

4.死循环

  • 死循环就是一直循环的执行下去,如果没有干预不会停止下来。
  • 写法:
		// 第一种方式
        for (;;){
            System.out.println("Hello World");
        }
        // 第二种方式 经典做法
        while (true){
            System.out.println("Hello World");
        }
        // 第三种方式
        do{
            System.out.println("Hello World");
        }while (true);
  • eg1:系统密码是520,请用户不断的输入密码验证,验证不对输出密码错误,验证成功输出欢迎进入系统,并停止程序。
package xunhuan;

import java.util.Scanner;

public class dowl {
    public static void main(String[] args) {
         int okPassword = 520;
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请您输入正确的密码:");
            int password = sc.nextInt();
            if(password == okPassword){
                System.out.println("登录成功了!");
                break;
            }else {
                System.out.println("密码错误!");
            }
        }
    }
}

5.嵌套循环

  • 循环嵌套,循环中包含循环
package xunhuan;

public class qtxh {
    public static void main(String[] args) {
        for (int i = 0; i <3 ; i++) {
            for (int j = 0;j<5;j++){
                System.out.println("我爱你!");
            }
        }
    }
}
  • eg1:在控制台使用*打印出4行5列的矩形。
    在这里插入图片描述
package xunhuan;

public class qtxh {
    public static void main(String[] args) {
        for (int i = 0; i <4; i++) {
            for (int j = 0;j<5;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
  • eg2:用循环嵌套打印乘法表
    -
 package xunhuan;

public class qtxh {
    public static void main(String[] args) {
        for (int i = 1; i <=9; i++) {
            for (int j = 1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"  ");
            }
            System.out.println();
        }
    }
}

6.跳转控制语句

  • break:跳出并结束当前所在循环的执行。
  • continue:用于跳出当前循环的当次执行,进入下一次循环。
    注意事项:
    break:只能用于结束所在的循环,或者结束所在switch分支的执行。
    continue:只能在循环中进行使用。
	package xunhuan;

public class guanjianzi {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("快乐星球"+i); // 快乐星球0 快乐星球1 快乐星球2
            if(i == 2){
                break;
            }
        }
        for (int j = 0; j <= 5 ; j++) {
            if(j==3){
                continue;
            }
            System.out.println("快乐地球"+j); // 快乐地球0 快乐地球1 快乐地球2 快乐地球4 快乐地球5
        }
    }
}

四、案例技术:随机数Random类

Random的使用

  • 作用:用于在程序中获得随机数的技术。
  • 使用步骤:
    在这里插入图片描述
package shuijishu;
import java.util.Random;

public class suijisu {
    public static void main(String[] args) {
        Random r = new Random();
        int number = r.nextInt(10);
        System.out.println("随机数生成了:"+number);
    }
}
  • 注意:
    nextInt(n)功能只能生成:0至n-1之间的随机数,不包含n。

猜数游戏

- 随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜小提示过小,直到猜中结束游戏。

package shuijishu;
import java.util.Random;
import java.util.Scanner;

public class youxi {
    public static void main(String[] args) {
       int min = 1;
       int max = 100;
       Random r = new Random();
       Scanner sc = new Scanner(System.in);
       while (true){
           System.out.println("请您输入"+min+"-"+max+"中猜测的数字:");
           int sjs = min + (int)(Math.random() * (max-min+1));
           int sz = sc.nextInt();
           if(sz < max && sz > min){
               if(sjs==sz){
                   System.out.println("恭喜你猜对了!");
                   break;
               }else if(sz<sjs){
                   System.out.println("很遗憾请从新猜测!");
                   min = sz;
               }else{
                   System.out.println("很遗憾请从新猜测!");
                   max = sz;
               }
           }else {
               System.out.println("请输入合法猜测数!");
           }
       }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值