It's our wits that make us men.

Java学习笔记(5)控制流程

Posted on By Xc L


Java的if条件语句

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

多个表达式与一个表达式

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

if使用过程中可能遇到的坑

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

如果if后面只有一个分号,分号也是完整的表达式

如果b为true,会执行这个分号,然后打印yes 如果b为false,不会执行这个分号,然后打印yes

这样,看上去无论如何都会打印yes

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

else if

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);
	}
}

练习-BMI

使用Scanner收集你的身高体重,并计算出你的BMI值是多少

BMI的计算公式是 体重(kg) / (身高*身高)

比如邱阳波的体重是72kg, 身高是1.69,那么这位同学的BMI就是 72 / (1.69*1.69) = ?

然后通过条件判断BMI的范围,打印出是超重还是正常

public class Bmi {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in );
		System.out.println("请输入身高(m):");
		float a = in.nextFloat();
		System.out.println("请输入体重(kg):");
		float b = in.nextFloat();
		double k = b/(a*a);
		System.out.println("当前的BMI是:"+k);
		if (k<18.5)
            System.out.println("身体状态是:体重过轻");
        else if (k>=18.5&&k<24)
            System.out.println("身体状态是:正常范围");
        else if (k>=24&&k<27)
            System.out.println("身体状态是:正常过重");
        else if (k>=27&&k<30)
            System.out.println("身体状态是:轻度肥胖");
        else if (k>=30&&k<35)
            System.out.println("身体状态是:中度肥胖");
        else if (k>=35)
            System.out.println("身体状态是:重度肥胖");
		
	}
}

练习-闰年

判断某一年是否为闰年 通过Scanner 输入一个年份,然后判断概念是否是闰年

闰年判断标准(满足任何一个)

  1. 如果能够被4整除,但是不能被100整除
  2. 能够被400整除
public class HelloWorld{
	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = s.nextInt();//1992
         
        if((year%4 == 0 && year%100 != 0)) {
            System.out.println(“闰年:” + year);
        }else if(year%400 == 0){
            System.out.println("闰年:” + year);
        }else{
            System.out.println(“非闰年:” + year);
        }
	}
}

Java的switch语句

switch语句相当于if else的另一种表达方法

switch

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

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

enum 是枚举类型
public class HelloWorld{
	public static void main{

		//如果使用 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("输入错误");
		}
	}
}

Java的while语句

while和do-while循环语句

关键字 简介
while 条件为true时,重复执行
do while 条件为true时,重复执行,至少会执行一次

条件为true时 重复执行

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

public class HelloWorld{
	public static void main(String[] args){

		//打印0-4
		int i = 0;
		while(i<5){
			System.out.println(i);
			i++;
		}
	}
}

条件为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)
	}
}

练习-阶乘

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

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

Java的for循环

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

for

比较for和while

public class HelloWorld{
	public static void main(String[] args){

		//使用while打印0-4
		int i = 0;
		while(i<5){
			System.out.println("while循环的输出"+i);
			i++;
		}

		//使用for循环打印0-4
		for(int j = 0;j<5;j++){
			System.out.println("for循环的输出"+j);
		}
	}
}

练习-for循环

攒零花钱,第一天攒1块,第二天攒2块,第三天攒4块,以此类推, 问10天一共攒多少

import java.util.Scanner;

public class HelloWorld{
	public static void main(String[] args){
		int sum = 0;

	for(int i = 0;i<=10;i++){
		sum += Math.pow(2,i);
	}
	System.out.println("总共攒的钱:"+sum);
	}
}

Java的continue语句

continue 继续下一次循环

continue

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

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

练习-忽略倍数

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

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

Java的break语句

结束循环

break

直接结束当前for循环

public class HelloWorld{
	public static void main(String[] args){

		//打印单数
		for(int j = 0;j<10;j++){
			if(0==j%2)
				break;//如果是双数,直接结束循环

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

★练习-百万富翁

假设你月收入3000,除开平时花销,每个月留下1000块进行投资。

然后你认真的钻研了投资,达到每年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

答案:

public class HelloWorld{
	public static void main(String[] args){
		int fundPerMonth;
		int fundPerYear = fundPerMonth*12;
		float rate = 0.20f;
		//F = p*((1+r)^n);
		int sum = 0;
		int target = 1000*1000;
		for(int j = 1;j<100;j++){
			int year = j;
			float compoundInterestRate = 1;
			for(int i = 0;i<year;i++){
				compoundInterestRate = compoundInterestRate*(1+rate);//复合利率
			}
			int compoundInterest = (int)(fundPerYear * compoundInterestRate);//复合收益

			sum += compoundInterest;//总收入是历年复合收益之和
			System.out.println("经过"+year+"年,总收入"+sum);

			if(sum>=target){
				System.out.println("一共需要"+year+"年,累计收入超过"+target);
				break;
			}
		}
	}
}

Java如何结束外部for循环

break是结束当前循环

结束当前循环

break; 只能结束当前循环

public class HelloWorld{
	public static void main(String[] args){
		//打印单数
		for(int i = 0;i<10;i++){
			for(int j = 0;j<10;j++){
				System.out.println(i+":"+j);
				if(0==j%2)
					break;
			}
		}
	}
}

使用boolean变量结束外部循环

借助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;
		}
	}
}

使用标签结束外部循环

在外部循环的前一行,加上标签 在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]

答案:

public class HelloWorld{
	public static void main(String[] args){
		int n = 0,d = 0;
		double min = 1.0;
		double res;
		for(int i = 1;i<20;i++){
			for(int j = 1;j<10;j++)
				continue;
			res = (double)i/j;
			double abs = Math.abs(res - o.618);
			if (abs<min){
				min  = abs;
				n = i;
				d = j;
			}
		}
		res = (double)n/d;
		System.out.println("离黄金分割点(0.618)最近的两个数相除是:"+n+"/"+d+"="+res);
	}
}

练习-水仙花数

水仙花数定义: 1.一定是3位数 2.每一位的立方,加起来恰好是这个数本身,比如153=1^3+5^3+3^3 寻找所有的水仙花数

public class HelloWorld{
	public static void main(String[] args){
		int a,b,c,d;
		for(a=1;a<9&a>0;a++){
			for(b=1;b<9&b>0;b++){
				for(c=1;c<9&c>0;c++){
					d = 100*a+10*b+c;
					if(d==a^3+b^3+c^3){
						System,out.println(d+"是水仙花数");
					}
				}
			}
		}
	}
}