JAVA基础学习笔记 day004_作业02(运算符的运用)(有补充)

import java.util.Scanner;

/*
 * 需求:
 * 输入a = 5, b = 8; 输出  a = 8 , b = 5
 */
public class WorkDemo02 {

	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		
		int a = input.nextInt();
		int b = input.nextInt();
		
                //方法一:
		int sub = a-b;
		
			a -= sub;
			b += sub;

		/*
		 * 方法二:
		 * int temp;
		 * temp = a;
		 * a = b;
		 * b = temp;
		 */		

		System.out.println(a + "," + b);
	}
}

总结:1.输入a和b 2个数,结果对调

        2.通过单目运算符得出结果

        3.运算的优先级:单目运算符>算数运算符>关系运算符>逻辑运算符>   赋值运算符

        4."a++":先使用a原本自身的值,然后a再自增1
           "++a":先a自增1,然后使用a的值

猜你喜欢

转载自blog.csdn.net/qq_42801561/article/details/87975714