控制语句之选择结构

3.1 选择结构
3.1.1 if单选择结构
流程图:
在这里插入图片描述
1、math中random()的使用
double 变量名 = Math.random();//返回一个[0,1)区间的随机数
2、编写程序习惯
if可以不写大括号执行后面的第一条语句,在编写if时加上大括号,加强程序的可读性。


/**
 * 测试if单选择结构
 * @author 14323
 *
 */
public class TestIf {
	public static void main(String[] args){
		double d = Math.random();
		int e = (int)(6*d)+1;
		System.out.println(e);
		if(e<=3) {
			System.out.println("小");
		}
	
		System.out.println("#################");
		//通过掷三骰子看看今天的手气如何
		int i = (int)(6*Math.random()+1);
		System.out.println("第一个骰子的点数是:"+i);
		int j = (int)(6*Math.random()+1);
		System.out.println("第二个骰子的点数是:"+j);
		int k = (int)(6*Math.random()+1);
		System.out.println("第三个骰子的点数是:"+k);
		int count = i+j+k;
		//如果三个骰子之和大于15,则手气还不错
		if(count>15) {
			System.out.println("今天手气不错");
		}
		//如果三个骰子之和在10到15之间,则手气一般
		if(count<=15&&count>=10) {
			System.out.println("今天手气一般");
		}
		//如果三个骰子之和小于10,则手气不好
		if(count<10) {
			System.out.println("今天手气不好");
		}
		System.out.println("得了"+count+"分");
	}
}

3.1.2 if-else双选择结构
结构流程图:
在这里插入图片描述
1、数学函数:数学平方函数: Math.pow(变量名,幂)
圆周率: Math.PI
2、if-else应用实例

import java.math.*;

/**
 * 测试if—else双选择结构
 * @author 14323
 *
 */

public class TestIfElse {
	public static void main(String[] args){
		int h = (int)(6*Math.random());
		System.out.println(h);
		if(h<=3){
			System.out.println("小");
		}else {
			System.out.println("大");
		}
		
		System.out.println("##############");
		
		//随机产生一个[0,4)区间的半径,根据半径求圆的面积和周长
		double r = 4*Math.random();
		
		//求圆的面积,Math.pow(r,2),求半径r的平方
		double area = Math.PI*Math.pow(r,2);
		//求圆的周长
		double circle = 2*Math.PI*r;
		System.out.println("半径为:"+r);
		System.out.println("面积为:"+area);
		System.out.println("周长为:"+circle);
		//进行比较
		if(area>=circle){
			System.out.println("面积大于周长");
		}else{
			System.out.println("面积小于周长");
		}	
	}
}

3.1.3 if-else if-else多选择结构
1、结构流程:
在这里插入图片描述
2、应用实例

/**
 * 测试多选择结构
 * @author 14323
 *
 */
public class TestIfElseIfElse {
	public static void main(String[] args){
		int age = (int)(100*Math.random());//岁数范围是[0,99]
		System.out.println("年龄是"+age);
		if(age<15){
			System.out.println("属于儿童,喜欢玩!");
		}else if(age<25){
			System.out.println("属于青年,要学习!");
		}else if(age<45){
			System.out.println("属于中年,要工作!");
		}else if(age<65){
			System.out.println("属于中老年,要补钙!");
		}else if(age<85){
			System.out.println("属于老年,多运动!");
		}else{
			System.out.println("属于老寿星,古来稀!");
		}
	}
}

3.1.4 switch多选择结构
1、一般用来做多值判断,流程结构如图所示
在这里插入图片描述
2、break语句:在switch中碰到break够就跳出switch选择结构

/**
 * 测试switch语句
 * @author 14323
 *
 */

public class TestSwitch {
	public static void main(String[] args) {
		char c = 'a';
		int rand = (int)(26*Math.random());
		char c2 = (char)(c+rand);
		System.out.println(c2+":");
		switch(c2) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			System.out.println("元音");
			break;//从表达式对应值处开始执行,只要不碰到break,则会执行到末尾
		case 'y':
		case 'w':
			System.out.println("半元音");
			break;
		default:
			System.out.println("辅音");
		}
	}
}

3、在jdk1.7之后,允许使用case操作字符串。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值