03Java运算表达式,格式化

本文详细介绍了Java中的各种运算符,包括算术运算、赋值运算、比较运算、逻辑运算以及位运算,通过示例代码展示了它们的使用方法和效果,如算术运算中的加减乘除和自增自减,赋值运算中的复合赋值,比较运算中的大于、小于、等于,以及逻辑运算和位运算的规则和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运算符:是一种特殊符号,用以表示数据的运算、赋值和比较。

表达式:使用运算符将运算数据连接起来的符合Java语法规则的式子。

一、运算表达式

1.算术运算符

+  -  *  /    整除求整商    %   求余数    ++ --  运算

/*
 * Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
 *
 */
package itcast;

/**
 * <p>Project: JavaStudy - Opt1
 * <p>Powered by jiangbo On 2023-01-31
 * 14:45:09
 * <p>Created by IntelliJ IDEA
 *
 * @author jiangbo [3044483124@qq.com]
 * @version 1.0
 * @since 8/17
 */
public class Opt1 {
    public static void main(String[] args) {
        int a = 10;
        int b = 2;
        System.out.printf("%d + %d = %d%n", a, b, a + b);
        System.out.printf("%d - %d = %d%n", a, b, a - b);
        System.out.printf("%d × %d = %d%n", a, b, a * b);
//求整商
        System.out.printf("%d ÷ %d = %d%n", a, b, a / b);
//求余数
        System.out.printf("%d %% %d = %d%n", a, b, a % b);
        int c = 5;
//变量自身值加1
        ++c;
        System.out.println(c);
//d = 6,c = 7

        int d = c++;
//int d = ++c;
        System.out.println(c);
        System.out.println(d);
        d--;
        System.out.println(d);
        System.out.println(--d);
    }
}


2.赋值运算符

= += -= *= /= %=   (java 程序中 = 赋值 == 判断是不是同一个对象)

/*
 * Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
 *
 */
package itcast;

/**
 * <p>Project: JavaStudy - Opt2
 * <p>Powered by jiangbo On 2023-01-31
 * 15:39:44
 * <p>Created by IntelliJ IDEA
 *
 * @author jiangbo [3044483124@qq.com]
 * @version 1.0
 * @since 8/17
 */
public class Opt2 {
    public static void main(String[] args) {
        int a = 10, b = 3;
        int c = 2;
        c += a * b;
        System.out.println(c);
        c = c + a * b;
        System.out.println(c);
//50
        System.out.println(c-=12);
        System.out.println(c*=2);
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值