Java笔记 --- 双分支

一、双分支

基本语法:

    if(条件表达式) {

        执行代码块1;

   }

else {

   执行代码块2; 

}

二、入门练习

编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,则输出"你年龄大于18,要对自己的行为负责,送入监狱",否则输出“小于18岁不追究”。

package demo02;

import java.util.Scanner;

public class Single {
    public static void main(String[] args) {
        int age ;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的年龄");
        age = scanner.nextInt();
        if (age > 18){
            System.out.println("你年龄大于18,要对自己的行为负责,送入监狱");
        } else {
            System.out.println("小于18岁不追究");
        }
    }
}

 三、流程图

四、练习 一 

package demo02;

public class Test07 {
    public static void main(String[] args) {
        int x = 7;
        int y = 4;
        if (x > 5) {
            if (y > 5) {
                System.out.println(x + y);
            }
            System.out.println("老王来了");//输出
        }else {
            System.out.println("x=" + x);
        }
    }
}

 练习二

编写程序,声明2个double型变量并赋值。判断第一个数大于10.0, 且第2个数小于20.0,打印两数之和。

package demo02;

public class Test08 {
    public static void main(String[] args) {
        double num1 = 11.0;
        double num2 = 21.0;
        if (num1 > 10.0 && num2 > 20.0) {
                double num3 = num1 + num2;
                System.out.println("num1+num2=" + num3);
        }
    }
}

 练习三

定义两个变量int,判断者的和,是否能被3又能被5整除打印提示信息

package demo02;

public class Test09 {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 3;
        int num3 = num1 + num2;
        if (num3 % 3 == 0 && num3 % 5 == 0){
            System.out.println("能被3和5整除");
        } else {
            System.out.println("不能被3和5整除");
        }
    }
}

 练习四

判断一个年份是否是每年,闰年的条件是符合下面二者之(1)年份能被4整除,但不能被100整除; (2)能被400整除

package demo02;

public class Test10 {
    public static void main(String[] args) {
        int year = 2000;
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) {
            System.out.println(year + "年是闰年");
        } else {
            System.out.println(year + "不是闰年");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸭鸭老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值