zoukankan      html  css  js  c++  java
  • 四、Java中的运算符

    /*
    *一、 运算符
    * 1.算数运算符:+,-,*,/,++,--,%
    * 2.关系运算符:<,>,<=,>=,==,!=
    * 3.布尔运算符:&&,||,&,|,!,^
    * 4.位运算符
    * 5.赋值运算符:=,+=,-=,*=,/=,%=
    * 6.字符串运算符:+
    * 7.条件运算符:?
    * 8.其他运算符:instanceof,new
    *
    */

    二、算数运算符

    +,-,*,/,++,--,%

    public class Day12 {
        
        public static void main(String[] args){
            
            int k=10;
            k++;
            System.out.println("k="+k);//11
            
            //++如果出现在变量的后面
            int m=10;
            int e=m++;//如果++出现在变量后面,先赋值再自加1
            System.out.println("e="+e);//10
            System.out.println("m="+m);//11
            
            
            //++出现在变量的前面
            int f=10;
            ++f;
            System.out.println("f="+f);
            
            int c=10;
            int d=++c;//++出现在变量前面,先自加1再赋值
            System.out.println("d="+d);//11
            System.out.println("c="+c);//11
            
            
            
            int z=10;
            System.out.println(z++);//10println中相当于赋值运算xx=z++
            System.out.println(z);//11
            
            int y=10;
            System.out.println(++y);//11
            System.out.println(y);//11
        }
        
    
    }

    三、关系型运算符

    /*
    * 关系型运算符:运算结果一定是真或假
    * <,>,<=,>=,==,!=
    */

    public class Day13 {
        public static void main(String[] args){
            int a=10;
            int b=10;
            //判断两个基本的数据类型是否相等,必须使用“==”
            System.out.println(a>b);
            System.out.println(a>=b);
            System.out.println(a<b);
            System.out.println(a<=b);
            System.out.println(a==b);
            System.out.println(a!=b);
        }
        
    
    }

    四、布尔运算符

    /*
    * 布尔运算符:&&,||,&,|,!,^
    * & 逻辑与 两边都是true,结果是true
    * | 逻辑或 一边是true,结果就是true
    * ! 逻辑非 取反
    * ^ 逻辑异或 两边只要不一致就是true
    *
    * && 短路与 用法和&一样
    * || 短路或 用法和|一样
    *
    * 布尔运算符两边的算子必须是布尔类型,整个表达式的运算符结果也是一个布尔类型
    * 例如:5>3 & 5>5 true
    */

    public class Day14 {
        public static void main(String[] args){
            System.out.println(5>3 & 5>5);//true
            System.out.println(5<3 | 5>5);//true
            
            System.out.println(!false);//true
            System.out.println(true^false);//true
            System.out.println(true^true);//false
            
            //关于短路与或:后边一个表达式不执行
            //什么时候发生短路与:第一个算子结果是false的时候
            //什么时候发生短路或:第一个算子结果是true的时候
    
    //        int a=10;
    //        int b=10;
    //        System.out.println(a<b & a++>=b);//&左右两个表达式都执行
    //        System.out.println(a);//11
            
            int a=10;
            int b=10;
            System.out.println(a<b & a++>=b);//&&第一个表达式为真,第二个表达式不执行
            System.out.println(a);//10
        }
    
    }

    五、赋值运算符

    /*
    * 关于赋值运算符
    * =
    * +=
    * -=
    * *=
    */=
    * %=
    */

    public class Day15 {
        public static void main(String[] args){
            
            //+=
            int i=10;
            i += 10;//相当于i=i+10
            System.out.println(i);//20
            
            i -=5;
            System.out.println(i);//15
            
            i *=3;
            System.out.println(i);//45
            
            i /=5;
            System.out.println(i);//9
            
            i %=4;
            System.out.println(i);//1
            
            //基本赋值运算符合扩展运算符的区别:
            byte a=10;
            //编译无法通过,运算结果是int类型,前边的变量是byte类型
            //a=a+10;
            //语法:扩展运算符不改变运算结果类型,可能损失精度
            a+=10;
            //编译可以通过,严重损失精度
            a+=1000;
            
            
            
        }
    
    }

    六、字符串连接符

    /*
    * 关于字符串连接运算符
    * +运算符可以:
    * 1.做加法运算(加号两边只要都是数字,加法运算)
    * 2.字符串拼接(+号两边任意一端是字符串,一定是字符串拼接)
    */

    public class Day16 {
        public static void main(String[] args){
            int a=10;
            int b=12;
            
            System.out.println("a+b="+(a+b));//a+b=22
            System.out.println("a+b="+a+b);//a+b=1012
            System.out.println(a+"+"+b+"="+(a+b));
        }
        
    
    }

    七、条件运算符之三目运算

    /*
    * 关于条件运算符:三目运算
    * ?:
    * 语法:boolean表达式?表达式1:表达式2
    * boolean表达式的结果是true,则整个表达式的结果就是表达式1的结果,相反则是表达式2的结果
    */

    public class Day17 {
        public static void main(String[] atrgs){
            
            boolean sex=true;
            char c= sex?'男':'女';
            System.out.println(c);
            
            boolean isSuccess=true;
            //char c=isSuccess?'s':'男';
            System.out.println(isSuccess?'s':'男');
            
        }
    
    }

    八、条件运算符之7中控制语句

    /*
    * 关于控制语句7种:
    * if,if else
    * switch
    * for
    * while
    * do while
    * break
    * continue
    */

    1.if     if……else

    public class Day18 {
        public static void main(String[] args){
            boolean isSucess=true;
            if(isSucess){
                System.out.println("1");
            }else{
                System.out.println("0");
            }
            
            //如果分支语句中只有一条语句,大括号可省略
            //不推荐使用
            if(isSucess)
                System.out.println("1");
            else
                System.out.println("0");
            
            
        }
    }

    /*
    * 2.关于switch语句
    * 语法:
    * switch(int类型){
    *
    * case int类型:{
    * java语句;
    * java语句;
    * break;
    * case int类型:{
    * java语句;
    * java语句;
    * break;
    * default:
    * java语句;
    * }
    * 注意:break;可以没有
    * default也可以没有
    * break;语句如果没有则发生case穿透现象
    * switch后面的括号中可以填写byte,short,char类型,因为可以自动转换
    */

    public class Day19 {
        public static void main(String[] args){
            
    //        long a=1000;
    //        switch (a){//error
    //        case 100:{
    //            System.out.println("1");
    //        }
    //        }
            
            
            long a=1000;
            switch ((int)a){//error
            case 100:{
                System.out.println("1");
            }
            }
            
    //        char c='C';
    //        switch(c){
    //        case 'A':{
    //            System.out.println("2");
    //            break;
    //        }
    //        case 'B':{
    //            System.out.println("3");
    //            break;
    //        }
    //        case 'C':{
    //            System.out.println("4");//4
    //            break;
    //        }
    //        default:{
    //            System.out.println("5");
    //        }
    //        }
            
            //case穿透现象
            char c='A';
            switch(c){
            case 'A':{
                System.out.println("2");
            }
            case 'B':{
                System.out.println("3");
            }
            case 'C':{
                System.out.println("4");
                break;
            }
            default:{
                System.out.println("5");
            }
            }
            //case合并
            char d='A';
            switch(d){
            case 'A':case 'B':case 'C':{
                System.out.println("4");
                break;
            }
            default:{
                System.out.println("5");
            }
            }
            
        }
    
    }

    练习题

    /*
    * 练习题:需求:
    * 1.系统给定的成绩,成绩可以带小数,0-100
    * 2.编写程序根据学的城市需判断学生的等级
    * [90-100]优秀
    * [70-90)良好
    * [60-70)及格
    * [0-60]不及格
    * 只允许用switch
    */

    public class Day20 {
        public static void main(String[] args){
            double score=95.5;
            int grade=((int)score)/10;
            //System.out.println(grade);
            switch(grade){
            case 9:case 10:{
                System.out.println("优秀");
                break;
            }
            case 7:case 8:{
                System.out.println("良好");
                break;
            }
            case 6:{
                System.out.println("及格");
                break;
            }
            default:{
                System.out.println("不及格");
            }
            }
        }
    
    }

    /*
    *  3.for循环
    * 语法:for(表达式1;表达式2;表达式3){
    * java语句;
    * }
    * 表达式1是初始化表达式,最先执行,只执行一次,
    * 表达式2必须是boolean类型的。true
    * for循环开始执行,先执行表达式1,并只执行一次;
    * 进而之判断表达式2的结果,如果true,则执行java语句,
    * 再执行表达式3,然后判断表达式2的结果,直到表达式2的结果为false,则for循环结束
    */

    public class Day21_for {
        public static void main(String[] args){
            //死循环
    //        for(;;){
    //            System.out.println("test");
    //        }
            
            for(int i=0;i<10;i++){
                System.out.println(i);
            }
            
            int k;
            for(k=0;k<10;k+=2){
                System.out.println("k="+k);
            }
            for(int m=10;m>0;m--){
                System.out.println("m="+m);
            }
        }
    
    }

    /*
    * 3.1关于for循环嵌套
    *1. 九九乘法表
    * 1*1=1
    * 2*1=1 2*2=4
    * ……
    * 2.1-100奇数求和
    *
    */

    public class Day22_for2 {
        public static void main(String[] args){
            
            //九九乘法表
            for(int i=1;i<10;i++){
                for(int j=1;j<i+1;j++){
                    int m=i*j;
                    System.out.print((i+"*"+j+"="+m)+" ");
                }
                //一行结束换行
                System.out.println(" ");
            }
            
            //1-100奇数求和
            int sum=0;
            for(int i=1;i<100;i+=2){
                sum+=i;
                
            }
            System.out.println(sum);
            
            
        }
    
    }

    /*
    * 4.关于while循环
    * 语法:
    * while(boolean表达式){
    * java语句
    * }
    */

    public class Day23_while {
        public static void main(String[] args){
            int i=0;
            while(i<10){
                i++;
                System.out.println(i);//1-10
            }
            
            int j=0;
            while(j<10){
                
                System.out.println(j);//1-9
                j++;
            }
        }
    
    }

    /*
    * 5.关于do while循环
    * 语法:
    * do{
    * java语句;
    * }while(boolean表达式);
    * 循环次数为:1-n
    */

    public class Day24_DoWhile {
        public static void main(String[] args){
            
            int i=0;
            do{
                //i++;
                System.out.println(i);
                i++;
            }while(i<10);
        }
    
    }

    /*
    * 6.break;语句和continue语句:
    * 1.可以用在switch语句中,结束分支语句;
    * 2.break;语句可以出现在循环语句中,默认情况下结束离他最近的一个循环
    *
    */

    public class Day25_BreakAndContinue {
        public static void main(String[] args){
            //break:只是结束离他最近的一个循环
            for(int i=0;i<10;i++){
                System.out.println(i);
                
                if(i==5){
                    break;//只是结束离他最近的一个循环
                    //return;//结束的是main方法
                    }
            }
            System.out.println("666");
            
            
            //continue:结束当前本次循环,继续下次循环
            for(int i=0;i<10;i++){
                //System.out.println(i);
                
                if(i==5){
                    continue;//结束当前本次循环,继续下次循环
                    //return;//结束的是main方法
                    }
                System.out.println(i);
            }
            
            //深入break
            for(int i=5;i>0;i--){
                for(int j=0;j<i;j++){
                    if(j==2){
                        break;
                    }
                System.out.print(j);
                }
                System.out.println();
            }
            //break结束外面的循环:通过以下程序得知break可以结束指定的某个循环
            ForName:for(int i=5;i>0;i--){
                for(int j=0;j<i;j++){
                    if(j==2){
                        break ForName;
                    }
                System.out.print(j);
                }
                System.out.println();
            }
            
        }
    
    }
  • 相关阅读:
    SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
    谷歌浏览器扩展程序manifest.json参数详解
    获取天气api
    UVA 10385 Duathlon
    UVA 10668 Expanding Rods
    UVALIVE 3891 The Teacher's Side of Math
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 11210 Chinese Mahjong
    UVA 11384 Help is needed for Dexter
  • 原文地址:https://www.cnblogs.com/chushujin/p/9975331.html
Copyright © 2011-2022 走看看