Java小白入门第四弹 运算的区别和联系

主要介绍了java的基本运算,这里主要是通过具体的代码操作来说明。
算术运算:

package com.csdn;

public class Demo04 {
	//算数运算 + - * / %
	static void testMath() {
		int a = (int)(Math.random() * 100);
		int b = (int)(Math.random() * 100);
		
		System.out.println("a=" + a);
		System.out.println("b=" + b);
		
		int result;
		result = a + b;
		System.out.println("a+b=" + result);
		result = a - b;
		System.out.println("a-b=" + result);
		result = a * b;
		System.out.println("a*b=" + result);
		result = a/b;//结果取整
		System.out.println("a/b=" + result);
		result = a % b;
		System.out.println("a%b=" + result);
		//被0除
		int x = 0, y = 20;
		//int r = y/x;
		//System.out.println(r);
		
		//结果是浮点型
		int i = 10, j = 3;
		System.out.println(i/(float) j);
		
		// 字符串 + 运算
		String name = "tom";
		String email = "tom@gmail.com";
		int age = 20;
		String msg = name + ',' + email + ',' + age;
		System.out.println(msg);
	}
		public static void main(String[] args) {
			testMath();
	}
}

输出结果
在这里插入图片描述
关系运算:

package com.csdn;

public class Demo04 {
	//关系运算 > >= < <= == !=
	static void testRelation() {
		//基本类型的比较
		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
		int b = (int)(Math.random() * 100);
		
		System.out.println("a=" + a);
		System.out.println("b=" + b);
		
		boolean result;
		result = a > b;
		System.out.println("a>b:" + result);
		
		result = a >= b;
		System.out.println("a>=b:"+ result);
		
		result = a < b;
		System.out.println("a<b:" + result);
		
		result = a <= b;
		System.out.println("a<=b:" + result);
		
		result = a == b;
		System.out.println("a==b:" + result);
		
		result = a != b;
		System.out.println("a!=b:" + result);
}
	
//	//算数运算 + - * / %
//	static void testMath() {
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		int result;
//		result = a + b;
//		System.out.println("a+b=" + result);
//		result = a - b;
//		System.out.println("a-b=" + result);
//		result = a * b;
//		System.out.println("a*b=" + result);
//		result = a/b;//结果取整
//		System.out.println("a/b=" + result);
//		result = a % b;
//		System.out.println("a%b=" + result);
//		//被0除
//		int x = 0, y = 20;
//		//int r = y/x;
//		//System.out.println(r);
//		
//		//结果是浮点型
//		int i = 10, j = 3;
//		System.out.println(i/(float) j);
//		
//		// 字符串 + 运算
//		String name = "tom";
//		String email = "tom@gmail.com";
//		int age = 20;
//		String msg = name + ',' + email + ',' + age;
//		System.out.println(msg);
//	}
		public static void main(String[] args) {
			//testMath();
			testRelation();
	}
}

输出结果:
在这里插入图片描述
逻辑运算:

package com.csdn;

public class Demo04 {
	//逻辑运算  & | ! && ||
	static void testLogic() {
		final int  HEIGHT = 185;
		final int  WEIGHT = 75;
		final int  MONEY = 10000000;
		
		int h = 165;
		int w = 100;
		int m = 20000000;
		boolean result;
		
//      & | ! and or not
		result = h >= HEIGHT & w <= WEIGHT & m >= MONEY;
		System.out.println(result);
		
		result = h >= HEIGHT | w <= WEIGHT | m >= MONEY;
		System.out.println(result);
		
		boolean flag = false;
		System.out.println(!flag);
		
		//短语与比较一和&和两个&&(如果前面的表达式可以确定结果,后面的表达式将不再计算)的区别
		int a = 1, b = 2,c = 3;
		if (a>b &&  c++ > 10) {
			System.out.println("run");
		}
		System.out.println(c);
		//短路或
		if (a < b || c++ > 10) {
			System.out.println("run...");
		}
		System.out.println(c);
	}
	//关系运算 > >= < <= == !=
//	static void testRelation() {
//		//基本类型的比较
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		boolean result;
//		result = a > b;
//		System.out.println("a>b:" + result);
//		
//		result = a >= b;
//		System.out.println("a>=b:"+ result);
//		
//		result = a < b;
//		System.out.println("a<b:" + result);
//		
//		result = a <= b;
//		System.out.println("a<=b:" + result);
//		
//		result = a == b;
//		System.out.println("a==b:" + result);
//		
//		result = a != b;
//		System.out.println("a!=b:" + result);
//}
	
//	//算数运算 + - * / %
//	static void testMath() {
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		int result;
//		result = a + b;
//		System.out.println("a+b=" + result);
//		result = a - b;
//		System.out.println("a-b=" + result);
//		result = a * b;
//		System.out.println("a*b=" + result);
//		result = a/b;//结果取整
//		System.out.println("a/b=" + result);
//		result = a % b;
//		System.out.println("a%b=" + result);
//		//被0除
//		int x = 0, y = 20;
//		//int r = y/x;
//		//System.out.println(r);
//		
//		//结果是浮点型
//		int i = 10, j = 3;
//		System.out.println(i/(float) j);
//		
//		// 字符串 + 运算
//		String name = "tom";
//		String email = "tom@gmail.com";
//		int age = 20;
//		String msg = name + ',' + email + ',' + age;
//		System.out.println(msg);
//	}
		public static void main(String[] args) {
			//testMath();
			//testRelation();
			testLogic();
	}
}

输出结果:
在这里插入图片描述
赋值运算:
(1)常规赋值

package com.csdn;

public class Demo04 {
	//赋值运算 =
	static void testAssign() {
		// 1.简单赋值
		int x = 1; //常量
		int y = x; //变量
		int z = x + y; //表达式
		int s = Math.max(1, 2); //方法返回值等等
				
		//2.复合赋值 自身运算
		int a = 100;
		a += 10; //a = a + 10;
		System.out.println("a=" + a);
		a -= 10; // a = a - 10
		System.out.println("a=" + a);
		a *= 10;
		System.out.println("a=" + a);
		a /= 10;
		System.out.println("a=" + a);
		
	}
//	//逻辑运算  & | ! && ||
//	static void testLogic() {
//		final int  HEIGHT = 185;
//		final int  WEIGHT = 75;
//		final int  MONEY = 10000000;
//		
//		int h = 165;
//		int w = 100;
//		int m = 20000000;
//		boolean result;
//		
      & | ! and or not
//		result = h >= HEIGHT & w <= WEIGHT & m >= MONEY;
//		System.out.println(result);
//		
//		result = h >= HEIGHT | w <= WEIGHT | m >= MONEY;
//		System.out.println(result);
//		
//		boolean flag = false;
//		System.out.println(!flag);
//		
//		//短语与比较一和&和两个&&(如果前面的表达式可以确定结果,后面的表达式将不再计算)的区别
//		int a = 1, b = 2,c = 3;
//		if (a>b &&  c++ > 10) {
//			System.out.println("run");
//		}
//		System.out.println(c);
//		//短路或
//		if (a < b || c++ > 10) {
//			System.out.println("run...");
//		}
//		System.out.println(c);
//	}
//	//关系运算 > >= < <= == !=
//	static void testRelation() {
//		//基本类型的比较
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		boolean result;
//		result = a > b;
//		System.out.println("a>b:" + result);
//		
//		result = a >= b;
//		System.out.println("a>=b:"+ result);
//		
//		result = a < b;
//		System.out.println("a<b:" + result);
//		
//		result = a <= b;
//		System.out.println("a<=b:" + result);
//		
//		result = a == b;
//		System.out.println("a==b:" + result);
//		
//		result = a != b;
//		System.out.println("a!=b:" + result);
//}
//	
//	//算数运算 + - * / %
//	static void testMath() {
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		int result;
//		result = a + b;
//		System.out.println("a+b=" + result);
//		result = a - b;
//		System.out.println("a-b=" + result);
//		result = a * b;
//		System.out.println("a*b=" + result);
//		result = a/b;//结果取整
//		System.out.println("a/b=" + result);
//		result = a % b;
//		System.out.println("a%b=" + result);
//		//被0除
//		int x = 0, y = 20;
//		//int r = y/x;
//		//System.out.println(r);
//		
//		//结果是浮点型
//		int i = 10, j = 3;
//		System.out.println(i/(float) j);
//		
//		// 字符串 + 运算
//		String name = "tom";
//		String email = "tom@gmail.com";
//		int age = 20;
//		String msg = name + ',' + email + ',' + age;
//		System.out.println(msg);
//	}
		public static void main(String[] args) {
			//testMath();
			//testRelation();
			//testLogic();
			testAssign();
	}
}

输出结果:
在这里插入图片描述
(2)自增自减

package com.csdn;

public class Demo04 {
	//自增自减
	//自增 (++):将变量的值加1,分前缀式(如++i)和后缀式(如i++)。前缀式是先加1再使用;后缀式是先使用再加1。
	//自减(--):将变量的值减1,分前缀式(如--i)和后缀式(如i--)。前缀式是先减1再使用;后缀式是先使用再减1。
	static void testApp() {
		int i = 0;
		int j = i++;
		System.out.println("i=" + i);
		System.out.println("j=" + j);
		int k = --i;
		System.out.println("i=" + i);
		System.out.println("j=" + j);
		System.out.println("k=" + k);
	}
	//赋值运算 =
//	static void testAssign() {
//		// 1.简单赋值
//		int x = 1; //常量
//		int y = x; //变量
//		int z = x + y; //表达式
//		int s = Math.max(1, 2); //方法返回值等等
//				
//		//2.复合赋值 自身运算
//		int a = 100;
//		a += 10; //a = a + 10;
//		System.out.println("a=" + a);
//		a -= 10; // a = a - 10
//		System.out.println("a=" + a);
//		a *= 10;
//		System.out.println("a=" + a);
//		a /= 10;
//		System.out.println("a=" + a);
//		
//	}
//	//逻辑运算  & | ! && ||
//	static void testLogic() {
//		final int  HEIGHT = 185;
//		final int  WEIGHT = 75;
//		final int  MONEY = 10000000;
//		
//		int h = 165;
//		int w = 100;
//		int m = 20000000;
//		boolean result;
//		
      & | ! and or not
//		result = h >= HEIGHT & w <= WEIGHT & m >= MONEY;
//		System.out.println(result);
//		
//		result = h >= HEIGHT | w <= WEIGHT | m >= MONEY;
//		System.out.println(result);
//		
//		boolean flag = false;
//		System.out.println(!flag);
//		
//		//短语与比较一和&和两个&&(如果前面的表达式可以确定结果,后面的表达式将不再计算)的区别
//		int a = 1, b = 2,c = 3;
//		if (a>b &&  c++ > 10) {
//			System.out.println("run");
//		}
//		System.out.println(c);
//		//短路或
//		if (a < b || c++ > 10) {
//			System.out.println("run...");
//		}
//		System.out.println(c);
//	}
//	//关系运算 > >= < <= == !=
//	static void testRelation() {
//		//基本类型的比较
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		boolean result;
//		result = a > b;
//		System.out.println("a>b:" + result);
//		
//		result = a >= b;
//		System.out.println("a>=b:"+ result);
//		
//		result = a < b;
//		System.out.println("a<b:" + result);
//		
//		result = a <= b;
//		System.out.println("a<=b:" + result);
//		
//		result = a == b;
//		System.out.println("a==b:" + result);
//		
//		result = a != b;
//		System.out.println("a!=b:" + result);
//}
//	
//	//算数运算 + - * / %
//	static void testMath() {
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		int result;
//		result = a + b;
//		System.out.println("a+b=" + result);
//		result = a - b;
//		System.out.println("a-b=" + result);
//		result = a * b;
//		System.out.println("a*b=" + result);
//		result = a/b;//结果取整
//		System.out.println("a/b=" + result);
//		result = a % b;
//		System.out.println("a%b=" + result);
//		//被0除
//		int x = 0, y = 20;
//		//int r = y/x;
//		//System.out.println(r);
//		
//		//结果是浮点型
//		int i = 10, j = 3;
//		System.out.println(i/(float) j);
//		
//		// 字符串 + 运算
//		String name = "tom";
//		String email = "tom@gmail.com";
//		int age = 20;
//		String msg = name + ',' + email + ',' + age;
//		System.out.println(msg);
//	}
		public static void main(String[] args) {
			//testMath();
			//testRelation();
			//testLogic();
			//testAssign();
			testApp();
	}
}

输出结果:
在这里插入图片描述
条件运算(三元运算):

package com.csdn;

public class Demo04 {
	//三元运算 e1 ? e2 : e3  
	static void testTerm() {
		//假设修正法
		int max;
		int x = 100, y = 30;
		if (x > y) {
			max = x;
		}else {
			max = y;
		}
		System.out.println(max);
		
		max = x > y ? x : y;
		System.out.println(max);
		
		int a = 999999, b = 1000, c =200;
		
		if (a > b) {
			max = a;
			if (c > a) {
				max = c;
			}else
				if (c > b) {
					max = c;
				}
			
			System.out.println(max);
			max = a > b ? (c > a ? c : a) : (c > b ? c : b);
			System.out.println(max);
			}
		}	
	//自增自减
	//自增 (++):将变量的值加1,分前缀式(如++i)和后缀式(如i++)。前缀式是先加1再使用;后缀式是先使用再加1。
	//自减(--):将变量的值减1,分前缀式(如--i)和后缀式(如i--)。前缀式是先减1再使用;后缀式是先使用再减1。
//	static void testApp() {
//		int i = 0;
//		int j = i++;
//		System.out.println("i=" + i);
//		System.out.println("j=" + j);
//		int k = --i;
//		System.out.println("i=" + i);
//		System.out.println("j=" + j);
//		System.out.println("k=" + k);
//	}
	//赋值运算 =
//	static void testAssign() {
//		// 1.简单赋值
//		int x = 1; //常量
//		int y = x; //变量
//		int z = x + y; //表达式
//		int s = Math.max(1, 2); //方法返回值等等
//				
//		//2.复合赋值 自身运算
//		int a = 100;
//		a += 10; //a = a + 10;
//		System.out.println("a=" + a);
//		a -= 10; // a = a - 10
//		System.out.println("a=" + a);
//		a *= 10;
//		System.out.println("a=" + a);
//		a /= 10;
//		System.out.println("a=" + a);
//		
//	}
//	//逻辑运算  & | ! && ||
//	static void testLogic() {
//		final int  HEIGHT = 185;
//		final int  WEIGHT = 75;
//		final int  MONEY = 10000000;
//		
//		int h = 165;
//		int w = 100;
//		int m = 20000000;
//		boolean result;
//		
      & | ! and or not
//		result = h >= HEIGHT & w <= WEIGHT & m >= MONEY;
//		System.out.println(result);
//		
//		result = h >= HEIGHT | w <= WEIGHT | m >= MONEY;
//		System.out.println(result);
//		
//		boolean flag = false;
//		System.out.println(!flag);
//		
//		//短语与比较一和&和两个&&(如果前面的表达式可以确定结果,后面的表达式将不再计算)的区别
//		int a = 1, b = 2,c = 3;
//		if (a>b &&  c++ > 10) {
//			System.out.println("run");
//		}
//		System.out.println(c);
//		//短路或
//		if (a < b || c++ > 10) {
//			System.out.println("run...");
//		}
//		System.out.println(c);
//	}
//	//关系运算 > >= < <= == !=
//	static void testRelation() {
//		//基本类型的比较
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		boolean result;
//		result = a > b;
//		System.out.println("a>b:" + result);
//		
//		result = a >= b;
//		System.out.println("a>=b:"+ result);
//		
//		result = a < b;
//		System.out.println("a<b:" + result);
//		
//		result = a <= b;
//		System.out.println("a<=b:" + result);
//		
//		result = a == b;
//		System.out.println("a==b:" + result);
//		
//		result = a != b;
//		System.out.println("a!=b:" + result);
//}
//	
//	//算数运算 + - * / %
//	static void testMath() {
//		int a = (int)(Math.random() * 100);//产生1到100之间的随机数
//		int b = (int)(Math.random() * 100);
//		
//		System.out.println("a=" + a);
//		System.out.println("b=" + b);
//		
//		int result;
//		result = a + b;
//		System.out.println("a+b=" + result);
//		result = a - b;
//		System.out.println("a-b=" + result);
//		result = a * b;
//		System.out.println("a*b=" + result);
//		result = a/b;//结果取整
//		System.out.println("a/b=" + result);
//		result = a % b;
//		System.out.println("a%b=" + result);
//		//被0除
//		int x = 0, y = 20;
//		//int r = y/x;
//		//System.out.println(r);
//		
//		//结果是浮点型
//		int i = 10, j = 3;
//		System.out.println(i/(float) j);
//		
//		// 字符串 + 运算
//		String name = "tom";
//		String email = "tom@gmail.com";
//		int age = 20;
//		String msg = name + ',' + email + ',' + age;
//		System.out.println(msg);
//	}
		public static void main(String[] args) {
			//testMath();
			//testRelation();
			//testLogic();
			//testAssign();
			//testApp();
			testTerm();
	}
}

输出结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

固执的鱼besos

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

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

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

打赏作者

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

抵扣说明:

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

余额充值