萌新Java的学习笔记八-控制语句if-17

一、单选择结构if语句
math类的使用:
Math.random()解释为在[0,1)区间中选取一个数类型为double.利用与整数乘积与强制转型为整数,可以提取随机的整数
例子1:

public class TestIf {
	public static void main(String[] args) {
		double a = Math.random();
		int b = 1+(int)a*5;//骰子1-6整数
		System.out.println(b);
		/*if(a>3)
			System.out.println("大数");
			System.out.print("大数!!");//if后没有大括号不受影响
		*/
		//使用大括号是最常用的而且清晰的表达方式
		if(a>3) {
			System.out.println("大数");
		}
	}

例子:2:

double i = 6*Math.random();
double j = 6*Math random();
double k = 6*Math random();
int count = 1+(int)(i+j+k);
if(count>15){
	System.out.println("今天手气不错");
}
if(count>=10){
	System.out.println("今天手气还可以");
}
if(count<10){
	System.out.println("今天手气一般");
}
	System.out.println("得了"+country+"分");

二、双选择结构if-else

public class TestIf {
	public static void main(String[] args) {
		double a = Math.random();
		int b = 1+(int)a*5;//骰子1-6整数
		System.out.println(b);
		/*if(a>3)
			System.out.println("大数");
			System.out.print("大数!!");//if后没有大括号不受影响
		*/
		//使用大括号是最常用的而且清晰的表达方式
		if(a>3) {
			System.out.println("大数");
		}else {
			System.out.println("小数");
		}
	}

三、多选择结构if-else if-else

double i = 6*Math.random();
double j = 6*Math.random();
double k = 6*Math.random();
int count = 1+(int)(i+j+k);
if(count>15){
	System.out.println("今天手气不错");
}else if(count>=10){
	System.out.println("今天手气还可以");
}else {
	System.out.println("今天手气一般");
}
	System.out.println("得了"+count+"分");

多重if-else的使用

	public static void main(String[] args) {
		double a = Math.random();
		int b = 1 + (int)(6*a);
		/*if(b>3) {
			System.out.println("大数");
			}else {
				System.out.println("小数");
			}
		*/

		for(int c=0;c<=99;c++) {
			double i = 6*Math.random();
			double j = 6*Math.random();
			double k = 6*Math.random();
			int count = 3+(int)(i)+(int)(j)+(int)(k);
			if(c==0) {
				if(count>=10) {
				System.out.print(count+" ");
				}else {
					System.out.print(count+"  ");
				}				
			}else{
			if(c%5==0) {
			System.out.println();				
			}
			if(count>=10) {
				System.out.print(+count+" ");
				}else {
					System.out.print(count+"  ");
				}
			}
		}
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值