JAVA基础之控制流程

if

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

if

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

练习-BMI

基于前面的操作符练习-BMI,做基于判断的改进:

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

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

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

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

package caozuofu;
import java.util.Scanner;;

public class Bml
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner h = new Scanner(System.in);
		System.out.println("请输入身高(m):");
		float height = h.nextFloat();
		System.out.println("请输入体重(kg)");
		float weight = h.nextFloat();
		float bml = weight/(height*height);
		System.out.println("当前的BMI是:"+bml);
		if (bml<18.5)
		{
			System.out.println("身体状态是:体重过轻");
		}
		else if (bml<24)
		{
			System.out.println("身体状态是:正常范围");
		}
		else if (bml<27)
		{
			System.out.println("身体状态是:体重过重");
		}
		else if (bml<30)
		{
			System.out.println("身体状态是:轻度肥胖");
		}
		else if (bml<35)
		{
			System.out.println("身体状态是:中度肥胖");
		}
		else 
		{
			System.out.println("身体状态是:中度肥胖");
		}

		

	}

}

练习-闰年

package kongzhiliucheng;
import java.util.Scanner;

public class Ruinian 
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		System.out.println("请输入年份:");
		int n = s.nextInt();
		if (n%4==0 && n%100 != 0||n%400==0)
		{
			System.out.println(n+"是闰年");
		}
		else
		{
			System.out.println(n+"不是闰年");
		}
		

	}

}

switch

switch可以使用byte,short,int,char,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("这个是什么鬼?");
        }
         
    }
}

练习-季节

package kongzhiliucheng;
import java.util.Scanner;

public class Jijie
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner month = new Scanner(System.in);
		System.out.println("请输入月访:");
		int n = month.nextInt();
		switch (n) {
			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("输入有误,重新输入");
				
		}

	}

}

while

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

练习-阶乘

package kongzhiliucheng;
import java.util.Scanner;

public class Jiecheng 
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner jie = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int n = jie.nextInt();
		int sum = 1;
		while(n>0)
		{
			sum=sum*n;
			n--;
		}
		System.out.println("阶乘是:"+sum);
	}

}

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

练习-乞丐 

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

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

package kongzhiliucheng;
import java.util.Scanner;

public class Qigai 
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		int sum =0;
		for(int i=1;i<=10;i++)
		{
			sum += Math.pow(2,i-1);
		}
		System.out.println("乞丐一共收了:"+sum);

	}

}

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的倍数,就忽略掉

package kongzhiliucheng;

public class Huluebeishu 
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		for (int i = 1;i<=100;i++)
		{
			if (i%3==0|i%5==0)
			{
				continue;
			}
			System.out.println(i);
		}
	}

}

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块钱进行投资。

然后你认真的钻研了 《股票和基金 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

解读:
本金是10000
年利率是5%
存了两年
复利收入 10000*( (1+0.05)^2 ) = 11025

package kongzhiliucheng;

public class Baiwangfugong
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		double benjin =12000;
		double  nianlilv =0.2;
		double sum=0;
		int  year;
		for (year =1;year<100000;year++)
		{
			sum = (sum+benjin)*Math.pow(1.2, 1);
			if (sum>1000000.0)
			{
				System.out.println(year);
				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]

package kongzhiliucheng;

public class Zonghelianxi
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		//练习-黄金分割点 
		int a =0;
		int b = 1;
		double c = 1; //将c 看做 一个 现阶段最接近的值。 c-0.618的值表示 距离。 只要 sum 比c-0.618小,就表示, sum 更接近 0.618.
		double sum = 0;
		for(int i = 1 ; i <= 10; i++)
		{
			for(int j = 11; j <= 20 ; j++)
			{
				if((i % 2 == 0) && (j % 2 == 0))
				{
					continue;
				}
				sum = (double)i /j;
				if(Math.abs(sum - 0.618) < Math.abs(c - 0.618))
				{
					c = sum;
					a =i;
					b = j;
				}
			}
		}
		System.out.println(a+"/"+b+"= " + c+"最接近0.618");

	}

}

练习-水仙花数

package kongzhiliucheng;

public class Shuixianhua
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		for(int a=1;a<10;a++)
		{
		    for(int b=0;b<10;b++)
		    {
                        for(int c=0;c<10;c++)
	                {
	                double suma = Math.pow(a,3);    
	                double sumb = Math.pow(b,3);    
	                double sumc = Math.pow(c,3);
	                double BigSum;
	                BigSum=suma+sumb+sumc;
	                double sum=100*a+10*b+c;
	                if(BigSum==sum)
	                {
	                    System.out.println(sum);
	                }
	            }
	        }
	    }

	}

}

练习-小学算术题

练习-小学算术题

package kongzhiliucheng;

public class Xiaoxueti
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		for (int a = 0;a<14;a++)
		{
			for (int b = 0;b<14;b++)
			{
				for (int c = 0;c<14;c++)
				{
					for (int d = 0;d<14;d++)
					{
						if (a+b == 8 & a+c == 14 & b+d ==10 & c-d ==6)
							System.out.println(a+","+b+","+c+","+d);
					}
				}
			}
		}
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值