条件判断结构:
-
if单分支结构:
语法结构:
if(布尔表达式){
语句块
}
if 语句对布尔表达式进行一次判定,若判定为真,则执行{}中的语句块,否则跳过该语句块。其执行流程图如下:
代码测试if语句(骰子游戏):
/*
* 测试if语句,一个简单的掷骰子游戏
* */
public class TestIf01 {
public static void main(String[] arg) {
double d=Math.random();//Math.random生成一个【0-1】的随机数包含0不包含1
int a=1+(int)(Math.random()*6);//生成了[1,6]之间的随机整数
int b=1+(int)(Math.random()*6);//生成了[1,6]之间的随机整数
int c=1+(int)(Math.random()*6);//生成了[1,6]之间的随机整数
int count=a+b+c;
if(count>15) {
System.out.println("手气不