Java学习之三元运算符-if语句

来源:互联网 发布:火狐 查看元素js事件 编辑:程序博客网 时间:2024/05/22 10:36

三元运算符

package basis.day02;/** * 三元运算符(条件表达式)?表达式1:表达式2;(若为真输出1,若为假输出2) * 其中表达式1和表达式2的数据类型要一致。 * 三元运算符一定程度上可以用if-else语句表示(但是反之不行) * @author LENOVO * */public class TestTer {    public static void main(String[] args){        /*         * 输出两个数中较大的数         */        int m = 12;        int n = 65;        int max = (m > n)? m : n;   //条件为真,输出第一个表达式的值;条件为假输出第二个表达式的值        System.out.println(max);        /*         * 输出三个数中较大的数         */        int a = 21;        int b = 45;        int c = 13;        /*        int max = (a > b)? (a > c)? a : c :(b > c)? b : c;        System.out.println(max);        */        int max1 = (a > b)? a : b;        int max2 = (max1 > c)? max1 : c;        System.out.println(max2);    }}

if语句

package basis.day02;import java.util.Scanner;/** * 流程控制:顺序结构(程序顺序执行),分支结构(if-else,switch-case), * 循环结构(while,do……while,for) * @author LENOVO * */public class TestIf_01 {    /*     * if-else语句:在程序执行过程中一定有一个语句执行,并且只有一条语句执行     * if(条件表达式){}else{}   if(条件表达式){}else if(条件表达式){}else{}     * 如果各个条件之间是“互斥”关系,语句是自由的。     * 如果各个条件之间有“包含”关系,范围大的要放在范围小的后边。     */    public static void main(String[] args){        /*         *从键盘读入小明的成绩         */        Scanner sc = new Scanner(System.in);        System.out.println("请输入小明的期末成绩:");        int score = sc.nextInt();        /*         * 依据不同的条件输出不同的结果         */        if(score == 100){            System.out.println("小明的奖励是:BMW车一辆");        }else if(score > 80){            System.out.println("小明的奖励是:iphone6s一台");        }else if(score >= 60){            System.out.println("小明的奖励是:参考书一本");        }else{            System.out.println("继续努力!");        }    }}
if-else对三个数进行排序练习:package basis.day02;import java.util.Scanner;/** * if-else语句练习 * @author LENOVO * */public class TestIf_02 {    public static void main(String[] args){        //从键盘接受数据        Scanner sc = new Scanner(System.in);        System.out.println("请输入三个整数:");        int num1 = sc.nextInt();        int num2 = sc.nextInt();        int num3 = sc.nextInt();        /*         * 判断大小:先找出最大的,再将剩下的进行比较。         */        if(num1 <= num2){            if(num2 <= num3){                System.out.println(num1 + "," + num2 + "," + num3);            }else if (num1 <= num3){                System.out.println(num1 + "," + num3 + "," + num2);            }else{                System.out.println(num3 + "," + num1 + "," + num2);            }        }else{            if(num1 <= num3){                System.out.println(num2 + "," + num1 + "," + num3);            }else if (num2 <= num3){                System.out.println(num2 + "," + num3 + "," + num1);            }else{                System.out.println(num3 + "," + num2 + "," + num1);            }        }    }}

if-else中(多个条件练习)

package basis.day02;import java.util.Scanner;/** * if测试题 * @author LENOVO * */public class TestIf_03 {    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        System.out.println("请输入你的身高:(cm)");        //获取身高        int height = sc.nextInt();        System.out.println("请输入你的财富:(千万)");        //获取财富值        double fortune = sc.nextDouble();        System.out.println("你长得帅吗?(true/false)");        //获取帅值        boolean b = sc.nextBoolean();        /*         * 条件筛选         *///      if(height > 180 || fortune > 1 || b){//          if(height > 180 && fortune > 1 && b){//              System.out.println("我一定要嫁给他!!!");//          }else{//              System.out.println("嫁吧,比上不足,比下有余。");//          }//      }else{//          System.out.println("不嫁");//      }        if(height > 180 && fortune > 1 && b){            System.out.println("我一定要嫁给他!!!");        }else if(height > 180 || fortune > 1 || b){            System.out.println("嫁吧,比上不足,比下有余。");        }else{            System.out.println("不嫁");        }    }}
2 0
原创粉丝点击