第四课:逻辑控制

1.分支语句

(1)if语句

练习

1.判断一个数字是奇数还是偶数

public static void main(String[] args) {
        int a = 10;
        if (a % 2 == 1){
            System.out.println("a是奇数");
        }
        else{
            System.out.println("a是偶数");
        }
            
}

 2.判断一个年份是否为闰年

普通闰年:公历年份为4的倍数但不是100的倍数的年份

世纪闰年:公历年份是整百数的,必须是400的倍数

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int year = scanner.nextInt();

        if (year % 100 == 0){
            if (year % 400 == 0){
                System.out.println("世纪闰年");
            }
            else{
                System.out.println("不是闰年");
            }
        }
        else{
            if (year % 4 == 0){
                System.out.println("普通闰年");
            }
            else {
                System.out.println("不是闰年");
            }
        }
    }

Java的输入需要一个工具,要导入

import java.util.Scanner;//导包

这个可以不用手写,输入Scan,点击Scanner系统自动导包

 代码风格

// 风格1-----> 推荐
int x = 10;
if (x == 10) {
// 语句1
} else {
// 语句2
}

 (2)switch语句

switch(表达式){
case 常量值1:{
    语句1;
    break;
}
case 常量值2:{
    语句2;
    break;
}

...
default:{
    内容都不满足时执行语句;
    break;
}
}

不能做switch参数的类型:long,double,float,boolean

2.循环结构

while(循环条件){
循环语句;
}

条件恒为true那么就是死循环

int a = 1;//循环的初始条件
while (a <= 10){  //循环的判断条件
    System.out.println(a);
    a++;//循环的步进
}

练习:

1.1~100的和

//1~100的和
    public static void main(String[] args) {
        int a = 1;
        int sum = 0;
        while (a <= 100){
            sum += a;
            a++;
        }
        System.out.println(sum);
        //1~100奇数的和
        a = 1;
        int sumOdd = 0;
        while (a <= 100){
            sumOdd += a;
            a += 2;
        }
        System.out.println(sumOdd);
        //1~100偶数的和
        int sumEve = 0;
        a = 2;
        while (a <= 100){
            sumEve += a;
            a += 2;
        }
        System.out.println(sumEve);
    }
2.计算 1! + 2! + 3! + 4! + 5!
    public static void main(String[] args) {
        int num = 1;
        int sum = 0;
// 外层循环负责求阶乘的和
        while (num <= 5) {
            int ret = 1;
            int tmp = 1;
// 里层循环负责完成求阶乘的细节.
            while (tmp <= num) {
                ret *= tmp;
                tmp++;
            }
            sum += ret;
            num++;
        }
        System.out.println("sum = " + sum);
    }

 3.找100~200之间第一个3的倍数

    public static void main(String[] args) {
        int num = 100;
        while (num <= 200) {
            if (num % 3 == 0) {
                System.out.println(num);
                break;
            }
            num++;
        }
    }

调试:直接在需要的语句旁边点一下打断点就行

4.找到1~100之间既能被3整除,也能被5整除的数字

        int  i = 1;
        while(i <= 100){
            if(i % 3 == 0 && i % 5 == 0){
                System.out.println(i);
            }
            i++;
            continue;
        }

 for循环,在idea里面打fori直接帮你列好

 do while 循环

3.输入输出 

⚠关于输出方法

 ⚠关于输入方法

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();//如果是next在遇到空格时就只输出空格前面的内容而已
        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你的工资:");
        float salary = sc.nextFloat();
        System.out.println("你的信息如下:");
        System.out.println("姓名: "+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);
        sc.close(); // 注意, 要记得调用关闭方法
    }

如果年龄和姓名交换位置

在输入时,系统不给你输入姓名的机会

因为在输入年龄后按的回车被当作字符串输入到姓名里面了,所以姓名被自动跳过

        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();
        sc.nextLine();//加这一句就行
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();

 循环读取n个数字

 while(sc.hasNextInt()){
            int tmp = sc.nextInt();
        }

终止输入在输入框内按CTRL+D

4.猜数字游戏

系统自动生成一个随机整数 (1-100), 然后由用户输入一个猜测的数字 . 如果输入的数字比该随机数小 , 提示 " 低 了", 如果输入的数字比该随机数大 , 提示 " 高了 " , 如果输入的数字和随机数相等 , 则提示 " 猜对了 " .
import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Random random = new Random();// 每次根据当前的时间生成随机数
        //如果在括号里加数字,那就根据这个数字来生成随机数
        int randNum = random.nextInt(100);//[0,100)
        //System.out.println("生成的随机数字:"+ randNum); 可以看系统给你生成的数字
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入要猜的数字:");
            int num = scanner.nextInt();
            if (num > randNum){
                System.out.println("猜大了");
            }else if(num < randNum){
                System.out.println("猜小了");
            }else{
                System.out.println("猜对了");
                break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值