zoukankan      html  css  js  c++  java
  • java笔记2之算术运算符

    1运算符是什么呢

      对常量和变量进行操作的运算符

    2运算符分为哪些

      算术运算符(+,-,*,/),

      赋值运算符

      比较运算符

      逻辑运算符

      位运算符

      三目运算符

    3运算符

      A 算术运算符的注意事项

        (1)整数相除只能是整数,如果想得到小数,必须把数据变化为浮点数类型

        (2)/获取的是除法操作的商,%获取的是除法操作的余数

      代码检测

    class OperatorDemo {
        public static void main(String[] args) {
            //定义变量
            int x = 3;  //把3赋值给int类型的变量x
            int y = 4;
            
            System.out.println(x+y);
            System.out.println(x-y);
            System.out.println(x*y);
            System.out.println(x/y); //整数相除只能得到整数
            
            //我就想得到小数,该肿么办呢?
            //只需要把操作的数据中任意的一个数据变为浮点数
            System.out.println(x*1.0/y);
            
            //%的应用
            System.out.println(x%y); //得到的是余数
        }
    }

      B ++,--运算符的使用:
            单独使用:
                放在操作数的前面和后面效果一样。(这种用法是我们比较常见的)
            参与运算使用:
                放在操作数的前面,先自增或者自减,然后再参与运算。
                放在操作数的后面,先参与运算,再自增或者自减。     
          作用:就是对变量进行自增1或者自减1。

      练习题目

     1 class OperatorTest {
     2     public static void main(String[] args) {
     3         int a = 10;
     4         int b = 10;
     5         int c = 10;
     6 
     7         a = b++; //a=10,b=11,c=10
     8         c = --a; //a=9,b=11,c=9
     9         b = ++a; //a=10,b=10,c=9
    10         a = c--; //a=9,b=10,c=8
    11         
    12         System.out.println("a:"+a);
    13         System.out.println("b:"+b);
    14         System.out.println("c:"+c);
    15         System.out.println("--------------");
    16         
    17         int x = 4;
    18         int y = (x++)+(++x)+(x*10);
    19         //4+6+60
    20         //x=5,6
    21         
    22         System.out.println("x:"+x);
    23         System.out.println("y:"+y);
    24     }
    25 }

    C +的用法:
            A:加法
            B:正号
            C:字符串连接符

     1 class OperatorDemo3 {
     2     public static void main(String[] args) {
     3         //加法
     4         System.out.println(3+4);
     5         
     6         //正号
     7         System.out.println(+4);
     8         
     9         System.out.println('a');
    10         System.out.println('a'+1); //这里是加法
    11         
    12         //字符串连接符
    13         System.out.println("hello"+'a'+1);
    14         System.out.println('a'+1+"hello");
    15     }
    16 }
  • 相关阅读:
    amd 2500 boot设置
    Windows Service开发日志(转载csdn)
    asp网站(asp+access)怎么防注入呢
    重新点亮shell————语法[四]
    重新点亮shell————特殊符号[五]
    Spring AOP及事务配置三种模式详解
    手撸一个IOC容器
    Mybatisplus入门教程
    Spring AOP源码解析
    深入理解Spring IOC容器及扩展
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/6266632.html
Copyright © 2011-2022 走看看