Java SE (19) Chapter 3 Operators-Arithmetic Operators

Arithmetic Operator

Operator Calculation example result Remarks
+ Positive sign +3 3
plus 3+5 8
- negative b=4;-b;  -4
Less 5-3 2
* Multiply 3*4 12
/ except 10/5 2
% Modulus (ie take the remainder) 13%5 3
++ Self-increasing (front) a=2; b=++a; a=3; b=3 Increment first, then calculate
Self-increasing (after) a=2; b=a++; a=3; b=2 Calculate first, then increment
-- Decrement (front) a=2; b=--a; a=1; b=1 Decrease first, then calculate
Decrement (post) a=2; b=a--; a=1; b=2 Calculate first, then subtract

 

 

 

 

 

 

 

 

 

 

 

 

 

table of Contents

Arithmetic Operator

+ Operator

 Division operator (/)

Modulus operator (%)

    Exercise 1 (/and %)

Increment (++) operator

    Exercise 2 (++)


 

  • + Operator

(1) Display positive number

(2) Represents the addition operation

(3) Concatenate strings

public class TestOpe01{
	public static void main(String[] args){
		//表示正数
		System.out.println(+5);    //5
		//表示相加操作
		System.out.println(5+6);   //11
		System.out.println(5+'6'); //5+54——>59
		//字符串的拼接
		/*
		规则:+左右两侧的任意一侧有字符串,
		那么这个加号就是字符串拼接的作用,
		结果一定是字符串
		*/
		int num=50;
		System.out.println("num="+num);   //num=50
		System.out.println(5+6+"7");      //11+"7"——>"117"——>117
		System.out.println(5+'6'+"7");    //5+54+"7"——>59+"7"——>"597"——>597
		System.out.println("5"+6+"7");    //"56"+"7"——>"567"——>567
		System.out.println("5"+'6'+"7");  //"56"+"7"——>"567"——>567
		System.out.println("5"+'6'+'7');  //"56"+'7'——>"567"——>567
	}
}

 

  •  Division operator (/)

In the division operation, when the divisor and the dividend are both integers , the result obtained is also an integer ;

                                   If a decimal is involved in the division operation , the result will be a decimal .

public class TestOpe02{
	public static void main(String[] args){
		System.out.println(12/3);    //4
        System.out.println(12/3.0);  //4.0
	}
}
  • Modulus operator (%)

When performing modulo operation, the sign of the result depends on the sign of the modulus ( the number to the left of % ). It has nothing to do with the sign of the modulus (the number to the right of %).

For example, the result of (-5)%3 is -2, and the result of 5%(-3) is 2.

public class TestOpe03{
	public static void main(String[] args){
		System.out.println(-5%3);    //-2
		System.out.println(-5.0%3);  //-2.0
		
        System.out.println(5%-3);    //2
		System.out.println(5.0%-3);    //2.0
	}
}
  • Exercise 1 (/and %)

public class TestOpe04{
	public static void main(String[] args){
		//实现功能:任意给出一个四位数,求出每位上的数字并输出
		
		//任意给出一个四位数
		int num=2345;
		//求出每位上的数字
		int num1=num%10;      //2345——>5
		int num2=num/10%10;   //2345——>234——>4
		int num3=num/100%10;  //2345——>23——>3
		int num4=num/1000;    //2345——>2
		//输出每位上的数字
		System.out.println("个位上的数字为:"+num1);  //个位上的数字为:5
		System.out.println("十位上的数字为:"+num2);  //十位上的数字为:4
		System.out.println("百位上的数字为:"+num3);  //百位上的数字为:3
		System.out.println("千位上的数字为:"+num4);  //千位上的数字为:2
	}
}

Improvement: any number entered by the user

       Introduce Scanner

import java.util.Scanner;
public class TestOpe04{
	public static void main(String[] args){
		//实现功能:任意给出一个四位数,求出每位上的数字并输出
		
		//任意给出一个四位数
		Scanner input=new Scanner(System.in);
		System.out.println("请录入一个四位数:");
		int num=input.nextInt();
		//求出每位上的数字
		int num1=num%10;
		int num2=num/10%10;
		int num3=num/100%10;
		int num4=num/1000;
		//输出每位上的数字
		System.out.println("个位上的数字为:"+num1);  
		System.out.println("十位上的数字为:"+num2);  
		System.out.println("百位上的数字为:"+num3);  
		System.out.println("千位上的数字为:"+num4);  
	}
}

 

 

  • Increment (++) operator

When performing an increment ++ or decrement - operation, no matter whether the variable is involved in the operation, as long as the ++ operator is used , the variable itself will eventually be incremented by 1 .

If ++ is at the end, calculate first, then add 1; if ++ is at the front, add 1 first, then calculate.

public class TestOpe05{
	public static void main(String[] args){
		int a=5;
		a++;   
		System.out.println(a);  //6
		
		a=5;
		++a;
		System.out.println(a);   //6
		/*
		总结:无论这个变量是否参与到运算中去,
		只要用++运算符,这个变量本身最终都加1操作。
		*/
		
		a=5;
		int m=a+++7;        //先运算m=a+7 ,再自增a=a+1。
		System.out.println(m);  //12
		System.out.println(a);  //6
		
		a=5;
		int n=++a+7;        //先自增a=a+1 ,再运算n=a+7。
		System.out.println(n);  //13
		System.out.println(a);  //6
	}
}

operation result:

  • Exercise 2 (++)

public class TestOpe06{
	public static void main(String[] args){
		int a=5;
		//注:运算符优先级 ++ > +
		System.out.println(a++ + a++);  
		System.out.println(a++ + ++a);  
		System.out.println(++a + a++);
		System.out.println(++a + ++a);
	}
}

Operation process:

Calculation result:

Guess you like

Origin blog.csdn.net/wqh101121/article/details/112394141