运算符: 1.算术运算符2.真题

运算符

1.1.算术运算符

符号 说明
+ 加法
- 减法
* 乘法
/ 除法(如果符号前后有一个小数结果就是正常小数)
% 模,取余数部分
package arithmetic;
public class Demo01Arithmetic{
    public static void main(String[] args) {
        int i=10;
        int j=3;
        int add=j+i;
        System.out.println(add);
        System.out.println(j+i);

        int sub=i-j;
        System.out.println(sub);

        int mul=i*j;
        System.out.println(mul);

        int div=i/j;
        System.out.println(div);

        int mod=i%j;
        System.out.println(mod);
    }
}
+:
	1.运算
    2.字符串拼接
package arithmetic;

public class Demo02Arithmetic {
    public static void main(String[] args) {
        int i = 10;
        int j = 3;
        System.out.println(i + j+"");//13
        System.out.println(i + j+""+1);//131
        System.out.println(i +""+j);//103

        System.out.println("i和j相加之和为:"+(i+j));
    }
}

1.2.自增自减运算符(也算算术运算符的一种)

1.格式:
    变量++ -> 后自加
    ++变量 -> 前自加
    变量-- -> 后自减
    --变量 -> 前自减
    
    自增自减变量只变化1
    
2.使用:
    a.单独使用: ++ -- 单独唯一句,没有和其他语句参和使用
    i++;

	符号在前在后都是先运算

	b.混合使用:++ -- 和其他语句混合使用了(比如:输出语句,赋值语句)
        符号在前:先运算,在使用运算后的值
        符号在后:先使用原值,使用完毕之后自身再运算

package arithmetic;

public class Demo03Arithmetic {
    public static void main(String[] args) {
        int i =10;
        //i++;
        ++i;
        System.out.println("i = "+ i);

        System.out.println("========================");

        int j = 100;
        int result01 = ++j;
        System.out.println("result01 = "+ result01);
        System.out.println(j);

        System.out.println("========================");

        int l = 100;
        int result02 = l++;
        System.out.println("result02 = "+ result02);
        System.out.println(l);

        System.out.println("========================");
        int x=10;
        int y =20;
        /*
        10+19=29
        29+12=41
        */
        int result03 = x++ + --y + ++y;
        System.out.println("result03 = "+ result03);

        System.out.println("========================");
//真题
        int c=10;
        c=c++;
        System.out.println("c = "+ c);
    }
}

image

posted @ 2026-01-22 09:05  何小德  阅读(1)  评论(0)    收藏  举报