Java第三天

12.赋值运算符和比较运算符string

public static void main(String[] args){
        //+=、-=、*=、/=、%=
        int a=10;
        //a=a+10
        a+=10;
        System.out.println(a);//20
        short b=10;
        //b=b-10  四则运算变量类型会提高,能够用赋值运算
        b-=10;
        System.out.println(b);//0
        short c=10;
        c*=5;
        System.out.println(c);//50
        short d=10;
        d/=5;
        System.out.println(d);//2
        //比较运算输出的结果类型是boolean;==、!=、>=、<=、>、<
        int a1=9;
        int b1=10;
        boolean result=(a1==b1);
        System.out.println(result);//false
        System.out.println(a1!=b1);//true
        System.out.println(a1>=b1);//false
        System.out.println(a1<b1);//trueit

13.逻辑运算符和if、switch分支判断变量

public static void main(String[] args){
        //逻辑判断符&、|、&&、||、!,byte、short、int、char、string类型
        int year=2014;
        int month=9;
        int day=-1
        //switch分支判断只能用于==判断,注意case和default格式一致
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day=31;
                break;//跳出switch
            case 4:
            case 6:
            case 9:
            case 11:
                day=30;
                break;//跳出switch
            case 2:
                if(year%400==0||(year%4==0&&year%100!=0)){
                day=29;
                }else{
                    day=28;
                }
                break;//跳出switch
            default:
                System.out.println("不存在!");
                break;//跳出switch
        }
        if(day!=-1){
            System.out.println(year+"年"+month+"月有"+day+"天!");
        }
        
    }static

14.三元运算符co

int score=54;
        /*
        if(score>=90){
            System.out.println("A");
        }else if(score>=80){
            System.out.println("B");
        }else if(score>=60){
            System.out.println("C");
        }else{
            System.out.println("you are so bad!");
        }
        */
        //三元运算符用来代替else if,表达式1?表达式2:表达式3;一般表达式1是一个判断表达式,紧接着问号后面的是true结果的输出分号后面是false结果的输出,两种结果均可以以常量、字符或者表达式的方式表示,而且也能够再嵌套另外的三元运算结构
        String result=(score>=90)?"A":((score>=80)?"B":((score>=60)?"C":"you are so bad!"));
        System.out.println(result);字符

相关文章
相关标签/搜索