Java运算符

一.  基础运算:加 / 减 / 乘 / 除 / 取余

public class Laugh {
    public static void main(String[] args) {
        //二元运算符
        System.out.println("-----------------二元运算符 正常计算------------------");
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;
        double j = 41.5;
        System.out.println("我是加法运算:"+ (a+b));
        System.out.println("我是减法运算:"+ (c-b));
        System.out.println("我是乘法运算:"+ (a*d));
        System.out.println("我是除法运算:"+ (float) (b/a));//有用强转哦  画蛇添足了,毕竟上面全是整数哦i🤣
        System.out.println("我是取余法运算:"+ (j%a));
    }
}

精度解释:

输出结果:

二.  一元运算:如:i++ / ++i (后面for循环会用哦)

public class Laugh {
    public static void main(String[] args) {
        //一元运算  ++  --
        System.out.println("---------------- 一元运算 ------------------");
        int k = 3;
        //System.out.println(k); // k = 3
        int l = k++; //先执行这行代码 后 再自增
        //System.out.println(k); // k = 4
        int m = ++k; //先执行这行代码 前 再自增
        System.out.println(k);
        System.out.println(l);
        System.out.println(m);
    }
}

输出结果:

三. 特殊运算说明(下方类型本人均以实验)

public class Laugh {
    public static void main(String[] args) {
        System.out.println("-----------------特殊说明------------------");
        int a = 10;
        short e = 100;
        byte f = 8;
        long g = 100023145689798787L;
        long h  = 1231231231231231L;
        System.out.println(e+f);      //这句输出默认为 short 类型;
        System.out.println(f+e);      //这句输出默认为 byte 类型;
        System.out.println(g+e+f+a);  //这句输出默认为 long 类型;
        System.out.println(a+g+e+f);  //这句输出默认为 int 类型;
        System.out.println(h+a+e+f);  //这句输出默认为 long 类型;
    }
}
//总结我就不说了,害怕出错i🤣

输出结果:

四. 关系运算 

public class Laugh {
    public static void main(String[] args) {
        System.out.println("-----------------关系运算符------------------");
        int a = 10;
        int b = 20;
        System.out.println(a==b);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a!=b);
    }
}

输出结果:

五. Math类说明(数学计算,里面有很多随便举几个例子:几个;幂运算也被称为"指数运算")

public class Laugh {
    public static void main(String[] args) {
        System.out.println("----------第一个分开写,便于阅读和逻辑顺序说明----------");
        System.out.println("-----------------最大值--- max  ------------------");
        int max = Math.max(10, 11);
        System.out.println(max);
        System.out.println("-----------------开始简写(缩写)--------------------");
        System.out.println("-----------------最小值--- min -------------------");
        System.out.println(Math.min(1, 8));
        System.out.println("------------------幂运算--- 2^3 ------------------");
        System.out.println((int)Math.pow(2,3));
    }
}

输出结果:

六. 与  或  (取反)

public class Laugh {
    public static void main(String[] args) {
        //与  或  非(取反)
        boolean n = true;   // true   在Java里面代表   ”真“
        boolean o = false;  // false  在Java里面代表   ”假“
        System.out.println("n&&o 是真是假:" + (n&&o));       // ”与“运算:两边为真都为真   一方为假 则结果为   ”假“   补充:短路运算
        System.out.println("n||o 是真是假:" +(n||o));        // ”或“运算:两边有一个为真   则结果为   ”真"
        System.out.println("!(n&&o) 是真是假:" + !(n&&o));   // ”取反“运算:若 ”n&&o“ 的值为 ”真“ 反之输出结果为 ”假“

        System.out.println("-----------------短路运算------------------");
        int p = 100;
        boolean q = (p<101)&&(p++<101);
        System.out.println(q);
        System.out.println(p);
    }
}

输出结果:

posted @ 2021-10-12 11:36  Laugh"  阅读(55)  评论(0编辑  收藏  举报