Java学习第五期——控制流程

一、if

if(表达式1){

表达式2;

}

if使用中会遇到的坑

在第6行,if后面有一个分号; 分号也是一个完整的表达式
如果b为true,会执行这个分号,然后打印yes
如果b为false,不会执行这个分号,然后打印yes
这样,看上去无论如何都会打印yes

public class HelloWorld {
    public static void main(String[] args) {
 
        boolean b = false;
 
        if (b);   //此处的分号也会被执行
            System.out.println("yes");
 
    }
}

else if

else if 在使用中可以在前置IF 实现的情况下不执行,其余if的需要再执行。

练习

import  java.util.Scanner;
public class controlTrend {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入你的体重(kg):");
        float weight = s.nextFloat();
        System.out.println("请输入你的身高(m):");
        float heigh = s.nextFloat();

        double BMI = weight / (heigh * heigh);
        System.out.println("当前的BMI为:"+BMI);
        if (BMI < 18.5)
            System.out.println("体重过轻!");
        else if (BMI >= 18.5 & BMI <24)
            System.out.println("正常范围!");
        else if (BMI >= 24 & BMI <27 )
            System.out.println("体重过重!");
        else if (BMI >= 27 & BMI <30)
            System.out.println("轻度肥胖!");
        else if (BMI >= 30 & BMI <35)
            System.out.println("中度肥胖!");
        else
            System.out.println("重度肥胖!");
    }

}

import java.util.Scnner;
public class Test {
    public static void main(String[] args) {
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入任意年份:");
            int year = sc.nextInt();
            if(year % 4 == 0 && year % 400 == 0 || year % 100 !=0){
                System.out.println(year+"年是闰年");
            }else{
                System.out.println(year+"年不是闰年");
            }
            System.out.println("*******************************");
        }
    }
}

 二、switch

相当于 if else ,每条语句都要使用break跳出。

switch可以使用byte,short,int,char,String,enum

注: 每个表达式结束,都应该有一个break;
注: String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数
注: enum是枚举类型,在枚举章节有详细讲解

练习:判断季节

import  java.util.Scanner;
public class controlTrend {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        System.out.println("请输入月份:");
        int month = s.nextInt();

        switch (month){
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
                break;

        }
    }

}

多个数字为相同输出时,可以使用上述的方式进行。

三、while 和 do while

while

条件为true时 重复执行

do while

条件为true时 重复执行,至少会执行一次

1、while 

只要while中的表达式成立,就会不断地循环执行

2、do while

与while的区别是,无论是否成立,先执行一次,再进行判断

练习: 阶乘

import  java.util.Scanner;
public class controlTrend {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        System.out.println("请输入一个整数:");
      int i = s.nextInt();
      int jiecheng = 1;
      while(i>0){
          jiecheng = jiecheng * i;
          i--;
      }
        System.out.println("阶乘是:"+jiecheng);

        }
    }


如果把这个变量定义在while块里,就会报错,因为其作用于值存在while块中。输出时无法调用。

 四、 for循环

for循环,和while一样,只是表达方式不一样

练习:天朝乞丐要钱

天朝有一个乞丐姓洪,去天桥要钱
第一天要了1块钱
第二天要了2块钱
第三天要了4块钱
第四天要了8块钱
以此类推

问题: 洪乞丐干10天,收入是多少?

import  java.util.Scanner;

public class controlTrend {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入乞丐要钱的天数:");
        int dayNumber = s.nextInt()-1;
        double money = 0;
        double sum = 0;
        for (;dayNumber>=0;dayNumber--){
            money = Math.pow(2,dayNumber);
            sum += money;
        }
        System.out.println("最终的乞讨的钱数为:"+sum);
        }
    }


注意:Math.pow(a,b);

表示第一个参数a 的b次方。

 五、continue

continue

遇到了,就开始下一次循环,即使后面有代码。

练习:忽略倍数

打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉。

import  java.util.Scanner;

public class controlTrend {
    public static void main(String[] args) {
       for (int i= 1; i <= 100; i++){
           if ( 0== i%3 || 0==i%5)
               continue;
           System.out.println(i);
       }
        }
    }


六、结束当前循环

break

 结束循环直接结束当前for循环

七、结束外部循环

借助boolean变量结束外部循环
需要在内部循环中修改这个变量值
每次内部循环结束后,都要在外部循环中判断,这个变量的值

public class HelloWorld {
    public static void main(String[] args) {
        boolean breakout = false; //是否终止外部循环的标记
        for (int i = 0; i < 10; i++) {
 
            for (int j = 0; j < 10; j++) {
                System.out.println(i + ":" + j);
                if (0 == j % 2) {
                    breakout = true; //终止外部循环的标记设置为true
                    break;
                }
            }
            if (breakout) //判断是否终止外部循环
                break;
        }
 
    }
}

需要有内部 循环中修改breakout的值,当内部循环跳出后,再判断外部循环,再跳出。
其实就是进行了一个布尔变量的设置,和判定。

2、使用标签结束外部循环

在外部循环的前一行,加上标签
在break的时候使用该标签
即能达到结束外部循环的效果

public class HelloWorld {
    public static void main(String[] args) {
          
        //打印单数    
        outloop: //outloop这个标示是可以自定义的比如outloop1,ol2,out5
        for (int i = 0; i < 10; i++) {
             
            for (int j = 0; j < 10; j++) {
                System.out.println(i+":"+j);
                if(0==j%2) 
                    break outloop; //如果是双数,结束外部循环
            }
             
        }
         
    }
}

此处的outloop就是设置的标签,当与break 结合使用时,不再是跳出当前,而是跳出外循环。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值