Java学习(2)—— 运算符

一.   算数运算符

        算数运算符有:加(+)、减(-)、乘(*)、除(/)、取余(%)、++、--

1. 除(/)

public class Test{
    public static void main(String[] args) {
        int num1 = 12;
        int num2 = 5;
        int result = num1 / num2;
        System.out.println(result);
    }
}

   输出:2

public class HelloJava{
    public static void main(String[] args) {
        double num1 = 12;
        double num2 = 5;
        double result = num1 / num2;
        System.out.println(result);
    }
}

   输出:2.4

public class Test{
    public static void main(String[] args) {
        int num1 = 12;
        int num2 = 5;
        int result = num1 / num2; //2
        double result1 = num1 / num2; //2.0
        double result2 = num1 / (num2 + 0.0); //2.4
        double result3 = (double)num1 / num2; //2.4
        double result4 = (double)(num1 / num2); //2.0
    }
}

2. 取余(%)

    结果的符号与被模数的符号相同。

public class Test{
    public static void main(String[] args) {
        int num1 = 14;
        int num2 = 5;
        int result1 = num1 % num2; // 4
        System.out.println(result1);

        int num3 = 14;
        int num4 = -5;
        int result2 = num3 % num4; // 4
        System.out.println(result2);

        int num5 = -14;
        int num6 = 5;
        int result3 = num5 % num6; // -4
        System.out.println(result3);

        int num7 = -14;
        int num8 = -5;
        int result4 = num7 % num8; // -4
        System.out.println(result4);
    }
}

3. ++

  (1)前++(先自增1,再运算)

public class Test{
    public static void main(String[] args) {
        int a = 12;
        int b = ++a;
        System.out.println("b="+b+","+"a="+a);
    }
}

       输出:b=13,a=13

(2)后++(先运算,再自增1)

public class Test{
    public static void main(String[] args) {
        int a = 12;
        int b = a++;
        System.out.println("b="+b+","+"a="+a);
    }
}

       输出:b=12,a=13

   自增1不会改变原始变量的数据类型:

public class Test{
    public static void main(String[] args) {
        short a = 12;
        //a = a + 1; 编译错误
        //a = (short)(a + 1); 正确
        a++; //自增1不会改变数据类型
    }
}

4. --

  (1)前--(先自减1,再运算)

  (2)后--(先运算,再自减1)

二.   赋值运算符

        赋值运算符有:=    +=    -=    *=     /=     %=

public class Test{
    public static void main(String[] args) {
        int i = 12;
        i += 1;
        System.out.println(i); //13

        int a = 10;
        a /= 5;
        System.out.println(a); //2
        
        short b = 4;
        //b = b + 2; 编译错误
        b += 2;  //不会改变数据类型
        System.out.println(b); //6

        int c = 2;
        c *= 0.2;
        System.out.println(c); //0
    }
}

三.   比较运算符

        比较运算符有:==        !=            >             <             >=             <=                 

四.   逻辑运算符

        逻辑运算符有:与 &&          或  ||            非  !       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值