JAVA漫游指南_基础篇05_控制流程

概述

本文主要介绍JAVA中的一些控制流程语法,如:if、switch、while、for、continue、break等

1. if 语句

1.if语句一般是用来做判断

if(表达式1){
表达式2;
}

如果表达式1的值为true,就执行表达式2
看图?
在这里插入图片描述

public class HelloWorld {
    public static void main(String[] args) {
         
        boolean b = true;
        //如果成立就打印yes
        if(b){
            System.out.println("yes");
        }
         
    }
}

2.多表达式与一个表达式

public class HelloWorld {
    public static void main(String[] args) {
         
        boolean b = false;
        //如果有多个表达式,必须用大括弧包括起来
        if(b){
            System.out.println("yes1");
            System.out.println("yes2");
            System.out.println("yes3");
        }
         
        //否则表达式2 3 无论b是否为true都会执行
         
        if(b)
            System.out.println("yes1");
            System.out.println("yes2");
            System.out.println("yes3");
             
        //如果只有一个表达式可以不用写括弧,看上去会简约一些
        if(b){
            System.out.println("yes1");
        }
         
        if(b)
            System.out.println("yes1");
         
    }
}

3.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");
 
    }
}

4.if else
else 代表不成立的情况
一图看懂?
在这里插入图片描述

public class HelloWorld {
    public static void main(String[] args) {
 
        boolean b = false;
 
        if (b)
            System.out.println("yes");
        else
            System.out.println("no");
 
    }
}

5.else if
如果只使用if,会执行4次判断。如果使用else if,当判断成立,剩下的就不再执行,这样有利于节约运算资源。

public class HelloWorld {
    public static void main(String[] args) {
 
        //如果只使用 if,会执行4次判断
        int i = 2;
        if (i==1)
            System.out.println(1);
        if (i==2)
            System.out.println(2);
        if (i==3)
            System.out.println(3);
        if (i==4)
            System.out.println(4);
         
        //如果使用else if, 一旦在18行,判断成立, 
        //20行和22行的判断就不会执行了,节约了运算资源
        if (i==1)
            System.out.println(1);
        else if (i==2)
            System.out.println(2);
        else if (i==3)
            System.out.println(3);
        else if (i==4)
            System.out.println(4);     
         
    }
}

6.练习-BMI

基于前面的操作符练习-BMI,做基于判断的改进:
使用Scanner收集你的身高体重,并计算出你的BMI值是多少
BMI的计算公式是 体重(kg) / (身高身高)
比如邱阳波的体重是72kg, 身高是1.69,那么这位同学的BMI就是
72 / (1.69
1.69) = ?
然后通过条件判断BMI的范围,打印出是超重还是正常

2.switch

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

类型缺省值长度数的范围
byte08位-127~127
short016位-32,768~32767
int032位-2,147,483,648~2,147,483,647
char\u000016位只能存放一个字符
String
enum

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

public class HelloWorld {
    public static void main(String[] args) {
         
        //如果使用if else
        int day = 5;
        if (day==1)
            System.out.println("星期一");
              
        else if (day==2)
            System.out.println("星期二");
        else if (day==3)
            System.out.println("星期三");
        else if (day==4)
            System.out.println("星期四");
        else if (day==5)
            System.out.println("星期五");
        else if (day==6)
            System.out.println("星期六");
        else if (day==7)
            System.out.println("星期天");
        else
            System.out.println("这个是什么鬼?");
         
        //如果使用switch
        switch(day){
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期天");
                break;
            default:
                System.out.println("这个是什么鬼?");
        }
         
    }
}
  • 练习

通过Scanner输入月份,然后用switch判断季节

import java.util.Scanner;
public class season {

	public static void main(String[] args) {

	System.out.println("请输入月份:");
	Scanner a = new Scanner(System.in);
	
	int month =a.nextInt();
	
	switch(month) {
		case 1:
		case 2:
		case 3:
			System.out.println("这是冬天");
			break;
		
		case 4:
		case 5:
		case 6:
			System.out.println("这是春天");
			break;
		
		case 7:
		case 8:
		case 9:
			System.out.println("这是夏天");
			break;
		
		case 10:
		case 11:
		case 12:
			System.out.println("这是秋天");
			break;
		
		default:
			System.out.println("输入有误,请重新输入:");    	
	    }
	
	}
}

3.while语句

  • while和do-while循环语句
关键字简介
while条件为true时 重复执行
do while条件为true时 重复执行,至少执行一次

1.条件为true时 重复执行

只要while中的表达式成立,就会不断的循环执行
比如打印0到4?
在这里插入图片描述

public class HelloWorld {
    public static void main(String[] args) {
         
        //打印0到4    
        int i = 0;
        while(i<5){
            System.out.println(i);
            i++;
        }
    }
}

2.条件为true是 重复执行,至少会执行一次

do{
} while 循环
与while的区别是,无论是否成立,先执行一次,再进行判断
在这里插入图片描述

public class HelloWorld {
    public static void main(String[] args) {
         
        //打印0到4
        //与while的区别是,无论是否成立,先执行一次,再进行判断
        int i = 0;
        do{
            System.out.println(i);
            i++;           
        } while(i<5);
         
    }
}

如果条件为flase,也会执行一遍
比如将i的值改为5,不满足i<5,此时还是会打印一次

public class HelloWorld {
    public static void main(String[] args) {
         
        //打印0到4
        //与while的区别是,无论是否成立,先执行一次,再进行判断
        int i = 5;
        do{
            System.out.println(i);
            i++;           
        } while(i<5);
         
    }
}

在这里插入图片描述

  • 练习-阶乘

通过Scanner 获取一个整数,然后使用while计算这个整数的阶乘
N的阶乘等于 N* (N-1) * (N-2) * … * 1

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

4.for循环

for循环和while一样,知识表达方式不同
使用for循环打印0-4

import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
       for(int i=0;i<5;i++) {
    	   System.out.println("打印"+i);
       }
    }
}
  • 练习

天朝有一个乞丐姓洪,去天桥要钱
第一天要了1块钱
第二天要了2块钱
第三天要了4块钱
第四天要了8块钱
以此类推
问题: 洪乞丐干10天,收入是多少?

import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
    	int sum = 1;
    	for(int i =1;i<=10;i++) {
    		if (i==1)
    			sum=1;
    		else
    		sum =sum*2+1;
    		System.out.println("第"+i+"天收入是"+sum);
    	}

       }
    	
    
}

在这里插入图片描述

5.continue

继续下一次循环
如果是双数,后面的代码不执行,直接进行下一次循环

public class HelloWorld {
    public static void main(String[] args) {
          
        //打印单数    
        for (int j = 0; j < 10; j++) {
            if(0==j%2) 
                continue; 
                //如果是双数,后面的代码不执行,直接进行下一次循环
             
            System.out.println(j);
        }
    }
}

在这里插入图片描述

  • 练习

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

public class HelloWorld {
    public static void main(String[] args) {
          
        //打印单数    
        for (int j = 1; j <=100; j++) {
            if(0==j%5||0==j%3) 
                continue; 
                //如果是5或3的倍数,后面的代码不执行,直接进行下一次循环

            System.out.println(j);
        }
    }
}

在这里插入图片描述

6.break

直接结束当前的for循环

public class HelloWorld {
    public static void main(String[] args) {
          
        //打印单数    
        for (int j = 1; j < 10; j++) {
            if(0==j%2) 
                break; //如果是双数,直接结束循环
             
            System.out.println(j);
        }
    }
}

在这里插入图片描述

  • 练习

假设你月收入是3000,除开平时花销,每个月留下1000块钱进行投资。
然后你认真的钻研了 《股票和基金 21天从入门到精通》,达到了每年20%的投资回报率。
那么问题来了,以每个月投资1000块钱的节奏,持续投资多少年,总收入达到100万
(复利计算按照每年12000投入计算,不按照每月计息)
复利公式:
F = p* ( (1+r)^n );
F 最终收入
p 本金
r 年利率
n 存了多少年
假设情景一:
p = 10000
r = 0.05
n = 1
解读:
本金是10000
年利率是5%
存了一年 1次
复利收入 10000*( (1+0.05)^1 ) = 10500
假设情景二:
p = 10000
r = 0.05
n = 2
解读:
本金是10000
年利率是5%
存了两年
复利收入 10000*( (1+0.05)^2 ) = 11025

import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
    	System.out.println("请输入每月能投资的本金数:");
        Scanner a1 = new Scanner(System.in);
        double p =a1.nextDouble();
        
        System.out.println("请输入年利率:");
        Scanner a2 = new Scanner(System.in);
        double r =a2.nextDouble();
        
        System.out.println("请输入最大能投资的年限:");
        Scanner a3 = new Scanner(System.in);
        int n =a3.nextInt();
        
        //复利公式
        
         double F=  p * (Math.pow(1+r, n));
         System.out.println("最终收入是"+F);
    } 
}

7.结束外部循环

  • 借助boolen变量结束外部循环
    需要再内部循环中修改这个变量值
    每次内部循环结束后,都要在外部循环中判断,这个变量的值
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;
        }
 
    }
}
  • 使用标签结束外部循环
    在外部循环的前一行,加上标签
    在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; //如果是双数,结束外部循环
            }
             
        }
         
    }
}
  • 练习
    寻找某两个数相除,其结果 离黄金分割点 0.618最近
    分母和分子不能同时为偶数
    分母和分子 取值范围在[1-20]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值