022- Java arithmetic operators

 Arithmetic operators which:

 

Enter the following code:

public  class Operator01 
{ 

    public  static  void main (String [] args) { 
    
        int A = 10 ;
         int B =. 3 ; 

        int C = A + B;
         int D = A- B;
         int E = A * B;
         int F = A / B;
         int G = A% B; 

        System.out.println (C); 
        System.out.println (D); 
        System.out.println (E); 
        System.out.println (F); // integer division only get integer, decimal if you want to get, you need to use a float. 
        System.out.println (G); 
     
    } 
}

 

At the command line compiler is printed as follows:

Precautions:

  • 整数相除只能得到整数。如果想得到小数,必须把数据变化为浮点数类型。
  • /获取的是除法操作的商,%获取的是除法操作的余数

 

自加自减,++这个叫做自增运算符,--叫做自减运算符

public class Operataor02
{
    int a = 10;
    ++a;//相当于a = a+1
    System.out.println(a);//11

    int b = 6;
    b++;//相当于b = b + 1;
    System.out.println(b);

    int c = 11;
    --c; 相当于c = c-1;
    System.out.println(c);

    int d = 8;
    d--;//相当于d = d -1;
    system.out.println(d);


}

 

Guess you like

Origin www.cnblogs.com/Chamberlain/p/11374671.html