JAVA/编程语言

[上]JAVA学习系列模块三第二章73.运算符_三元运算符练习

daimafengzi · 5月23日 · 2024年 · · 本文共781个字 · 预计阅读3分钟1059次已读

[上]JAVA学习系列模块三第二章73.运算符_三元运算符练习

视频


笔记

练习1

需求:小明考完试了,判断小明的分数是否及格,返回结果

public class Demo01Ternary {
    public static void main(String[] args) {
        //定义一个变量,表示小明的分数
        int score = 60;
        String result = score>=60?"及格":"不及格";
        System.out.println("result = " + result);
    }
}

练习2

有两个老人,年龄分别为70 80 求出两个老人的最高年龄

public class Demo02Ternary {
    public static void main(String[] args) {
        int old1 = 70;
        int old2 = 80;
        
        int max = old1>old2?old1:old2;
        System.out.println("max = " + max);
    }
}

练习3

有三个老人,年龄分别为70 80 60 求出三个老人的最高年龄

public class Demo03Ternary {
    public static void main(String[] args) {
        int old1 = 70;
        int old2 = 80;
        int old3 = 60;

        int temp = old1>old2?old1:old2;

        int max = temp>old3?temp:old3;
        System.out.println("max = " + max);
    }
}
0 条回应
Copyright © 2022-2024 LuoWeiHua
| 耗时 0.365 秒 | 查询 58 次 | 内存 4.15 MB |