Java之二元运算

二元运算+ - * / %

public class Demo4{
    public static void main(String[] args){
        //加法运算
        System.out.println(10 + 20); //30
        //字符串拼接
        System.out.println("10"+"20"); //1020

        int i1 = 100;
        int i2 = 200;
        int i3 = i1 + i2;
        System.out.println("i3=" + i3);//300
int num1 = 9;
        int num2 = 2;


        //在除法运算过程中,如果运算符两侧的类型都为int,那么它们最终做的是取整操作,不是四舍五入
        int num3 = num1 / num2;
        double num4 = num1 / num2;
        System.out.println("num3=" + num3); //4
        System.out.println("num4=" + num4); //4.0
double num5 = num1 / (2 * 1.0);
        System.out.println("num5=" + num5); //4.5

        //计算一下:35占87的百分之多少
        System.out.println(35 / 87.0 * 100 + "%"); //40.229885057471265%

        //做取模运算,最终的正负号和被模数的正负一致
        System.out.println(5 % 2);      //1
        System.out.println(5 % -2);     //1
        System.out.println(-5 % 2);     //-1
        System.out.println(-5 % -2);    //-1
    }
}

你可能感兴趣的:(java)