• Java知识系统回顾整理01基础04操作符06三元运算符


    一、三元运算符

    表达式?值1:值2

    如果表达式为真 返回值1

    如果表达式为假 返回值2

    if语句学习链接:if语句

       

    public class HelloWorld {

        public static void main(String[] args) {

            int i = 5;

            int j = 6;

            int k = i < j ? 99 : 88;

       

            // 相当于

            if (i < j) {

                k = 99;

            } else {

                k = 88;

            }

            System.out.println(k);

        }

    }

       

    二、练习-判断是否是工作日

    题目:

    通过Scanner输入一个1-7之间的整数,使用三元操作符判断是工作日还是周末?

    效果:

       

    官方答案:

    import java.util.Scanner;

        

    public class HelloWorld {

        public static void main(String[] args) {

            Scanner s = new Scanner(System.in);

            System.out.println("今天是周几 ?");

            int day = s.nextInt();

            String status= day>=6?"周末":"工作日";

            System.out.println("今天是 " + status);

        }

    }

       

    个人整理答案:

    public class Operator06 {

        public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);

            System.out.println("今天是周几?");

            System.out.println("请输入整数1-7表示今天周几");

            int dayNumber = scanner.nextInt();

            String status = dayNumber>6 ?"周末":"工作日";

            System.out.println("今天是" + status);

        }

    }

       

  • 相关阅读:
    codeforces 673D D. Bear and Two Paths(构造)
    codeforces 673C C. Bear and Colors(暴力)
    codeforces 673B B. Problems for Round(模拟)
    codeforces 673A A. Bear and Game(水题)
    hdu-3555 Bomb(数位dp)
    西交校赛 I. GZP and CS(数位dp)
    西交校赛 F. GZP and Poker
    删除目录下包含“2018” 关键字文件
    CSV转成Excel格式
    解决字符sqlplus 乱码
  • 原文地址:https://www.cnblogs.com/xlfcjx/p/10770304.html
Copyright © 2020-2023  润新知