Java基础知识5:算术运算符

public class KV{
    public static void main(String args[]){
        System.out.println(1.1+1.01);
        //需要JavaSE的知识解答为什么小数参与计算时结果可能不精确
        System.out.println(10/3);
        System.out.println(10.0/3);
    }
}

整数/整数 只会得到整数 

取模取余%的应用场景

 1.判断A是否可以被B整除

2.判断A是否为偶数: A%2

3.斗地主发牌:%3=0   %3=1   %3=2

数值拆分

个位:/%10

十位:/10%10

百位:/100%10

import java.util.Scanner;
public class KV{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int number = sc.nextInt();
        int ge = number%10;
        int shi = number/10%10;
        int bai = number/100%10;
        System.out.println(ge);
        System.out.println(shi);
        System.out.println(bai);
        
    }
}

编译时必须要用-encoding utf-8,否则无法运行中文字符串

隐式转换(程序自动进行):

取值范围小的数值转成取值范围大的数值

byte short char进行运算时会先提升为int再进行运算

public class KV{
    public static void main(String args[]){
        int a = 10;
        double b = 2.2;
        double c = a + b;
        System.out.println(c);
    }
}
public class KV{
    public static void main(String args[]){
        byte a = 2;
        byte b = 2;
        int c = a + b;
        System.out.println(c);
    }
}

public class KV{
    public static void main(String args[]){
        int a = 2;
        long b = 200L;
        double c = 20.0;
        double result = a+b+c;
        System.out.println(result);
    }
}

强制转换:

把取值范围大的数值转换成取值范围小的数值

public class KV{
    public static void main(String args[]){
        byte a = 2;
        byte b = 3;
        byte result = (byte)(a+b);
        System.out.println(result);
    }
}

 +:从左到右逐个执行,有字符串就进行拼接操作

public class KV{
    public static void main(String args[]){
        System.out.println(1+99+"年Steam");
    }
}

import java.util.Scanner;
public class KV{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int number = sc.nextInt();
        int ge = number%10;
        int shi = number/10%10;
        int bai = number/100%10;
        System.out.println("个位是"+ge+" 十位是"+shi+" 百位是"+bai);
    }
}

字符的+操作

当字符+字符 或者 字符+数字 时,会把字符通过ASCII码表查询到对应的数字再进行转换

public class KV{
    public static void main(String args[]){
        System.out.println(1+'a');
        System.out.println('a'+"abc");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DoorBreaker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值