运算符:对常量或者变量进行 *** 作的符号
表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式。
不同运算符连接的表达式体现的是不同类型的表达式。
举例说明:
int a = 10;
int b = 20;
int c = a + b;
+:是运算符,并且是算术运算符
a + b: 是表达式,由于+是算术运算符,所以这个表达式叫算术表达式
符号 | 作用 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
% | 取余 |
注意:
/和%的区别:两个数据做除法,/取结果是商,%取结果是余数 |
---|
整数 *** 作只能得到整数,要想得到小数,必须有浮点数参与运算 |
/*
字符的"+" *** 作
*/
public class OperatorDemo02 {
public static void main(String[] args) {
//定义两个变量
int i = 10;
char c = 'A'; //字符'A'的值为65
char b = 'a'; //字符'a'的值为97
char d = '0'; //字符'0'的值为48
//System.out.println(i + c);
//System.out.println(i + b);
//System.out.println(i + d);
char e = '1'; //字符'1'的值为49
//System.out.println(i + e);
//接收值
//char ch = i + c; 报错
//char类型会被自动提升为int类型
//int ch = i + c;
//System.out.println(ch);
//int k = 10 + 13.14; 报错
//整数默认int类型,浮点数默认double类型,两个类型计算,需要用double类型接收
double k = 10 + 13.14;
System.out.println(k);
}
}
1.2字符串的"+" *** 作
当"+“ *** 作中出现字符串时,这个”+"是字符串连接符,而不是算术运算。
范例:“a”+11
结果为:a11
当"+“ *** 作时,如果出现了字符串,就是连接运算符,否则就是算术运算。当连续进行”+" *** 作时,从左道友逐个执行。
范例:“a” + 1 +2
结果为:a12
范例:1+2+“a”
结果为:3a
/*
字符串的"+" *** 作
*/
public class OperatorDemo03 {
public static void main(String[] args) {
System.out.println("a" + "b");
System.out.println("a" + 1);
System.out.println(1 + "a");
System.out.println("a" + 1 + 2 );
System.out.println(1 + 2 + "a");
}
}
2.赋值运算符
2.1赋值运算符
/*
赋值运算符
*/
public class OperatorDemo04 {
public static void main(String[] args) {
//把10赋值给int类型的变量i
int i = 10;
System.out.println("i:" + i);
// +=把左边和右边的数据做加法 *** 作,结果赋值给左边
i += 20;
System.out.println("i:" + i);
//i += 20 等价于 i = i + 20;在int类型中两个可等价
//但在short或者byte或者char类型里不可等价
//因为+=赋值运算符隐含了强制类型转换
//建议在赋值运算中用第一种写法,例 i += 20;
short s = 10;
//s +=20
//s = s + 20;报错
// s = (short)(s + 20);不报错
System.out.println("s:" + s);
}
}
3.自增自减运算符
3.1自增自减运算符
/*
自增自减运算符
*/
public class OperatorDemo05 {
public static void main(String[] args) {
//定义变量
int i = 14;
System.out.println("i:" + i);
//自增自减运算符单独使用时,i++和++i效果一样。习惯放后面
i++;
//i++就是给i加了个1
System.out.println("i:" + i);
short w = 7;
++w;
System.out.println("w:" + w);
int j = 10;
System.out.println("j:" + j);
j--;
//j--就是给j减了个1
System.out.println("j:" + j);
short v = 4;
--v;
System.out.println("v:" + v);
//参与 *** 作使用
int n = 66;
//int m = n++; 此时n为67,m为66
int m = ++n; //此时n为67,m为67
System.out.println("n:"+ n);
System.out.println("m:" + m);
}
}
4.关系运算符
4.1关系运算符
/*
关系运算符
*/
public class OperatorDemo06 {
public static void main(String[] args) {
//定义变量
int i = 10;
int j = 20;
int k = 10;
//==
System.out.println(i == j);
System.out.println(i == k);
System.out.println("---------");
//!=
System.out.println(i != j);
System.out.println(i != k);
System.out.println("---------");
//>
System.out.println(i > j);
System.out.println(i > k);
System.out.println("---------");
//>=
System.out.println(i >= j);
System.out.println(i >= k);
System.out.println("---------");
//不小心把==写成=
//把j的值赋值给i,然后输出i的值
System.out.println(i = j);
}
}
5.逻辑运算符
5.1逻辑运算符概述
在数学中,一个数据x,大于3,小于6,我们可以这样来进行表示:3 在java中,需要把上面的式子先进行拆解,再进行合并表达。 需求:动物园里面有两只老虎,已知两只老虎的体重分别为180kg、200kg,请用程序实现判断两只老虎的体重是否相同。 需求:一座寺庙里住着三个和尚,已知他们的身高分别为150cm、210cm、165cm,请用程序实现获取这三个和尚的最高身高。 需求:一座寺庙里住着三个和尚,他们的身高必须经过测量得出,请用程序实现获取这三个和尚的最高身高。 欢迎分享,转载请注明来源:内存溢出
5.2逻辑运算符
拆解为: x>3和x<6
合并后:x>3&&x<6
&&其实就是一个逻辑运算符。
我们可以这样说,逻辑运算符,是用来连接关系表达式的运算符。
当然,逻辑运算符也可以直接连接布尔类型的常量或者变量。
5.3短路逻辑运算符
/*
逻辑运算符
*/
public class OperatorDemo07 {
public static void main(String[] args) {
//定义变量
int i = 10;
int j = 20;
int k = 30;
//& (逻辑与) 有false则false
System.out.println((i > j) & (i > k)); //false & false
System.out.println((i < j) & (i > k)); //ture & false
System.out.println((i > j) & (i < k)); //false & ture
System.out.println((i < j) & (i < k)); //ture & ture
System.out.println("--------------------");
//| (逻辑或) 有true则true
System.out.println((i > j) | (i > k)); //false | false
System.out.println((i < j) | (i > k)); //ture | false
System.out.println((i > j) | (i < k)); //false | ture
System.out.println((i < j) | (i < k)); //ture | ture
System.out.println("--------------------");
//^ (逻辑异或) 相同为false,不同为true
System.out.println((i > j) ^ (i > k)); //false ^ false
System.out.println((i < j) ^ (i > k)); //ture ^ false
System.out.println((i > j) ^ (i < k)); //false ^ ture
System.out.println((i < j) ^ (i < k)); //ture ^ ture
System.out.println("--------------------");
//! (逻辑非)
System.out.println(i > j); //false
System.out.println(!(i > j)); //!false
System.out.println(!!(i > j)); //!!false
System.out.println(!!!(i > j)); //!!!false
}
}
/*
逻辑运算符
*/
public class OperatorDemo07 {
public static void main(String[] args) {
//定义变量
int i = 10;
int j = 20;
int k = 30;
/**
//& (逻辑与) 有false则false
System.out.println((i > j) & (i > k)); //false & false
System.out.println((i < j) & (i > k)); //ture & false
System.out.println((i > j) & (i < k)); //false & ture
System.out.println((i < j) & (i < k)); //ture & ture
System.out.println("--------------------");
//| (逻辑或) 有true则true
System.out.println((i > j) | (i > k)); //false | false
System.out.println((i < j) | (i > k)); //ture | false
System.out.println((i > j) | (i < k)); //false | ture
System.out.println((i < j) | (i < k)); //ture | ture
System.out.println("--------------------");
//^ (逻辑异或) 相同为false,不同为true
System.out.println((i > j) ^ (i > k)); //false ^ false
System.out.println((i < j) ^ (i > k)); //ture ^ false
System.out.println((i > j) ^ (i < k)); //false ^ ture
System.out.println((i < j) ^ (i < k)); //ture ^ ture
System.out.println("--------------------");
//! (逻辑非)
System.out.println(i > j); //false
System.out.println(!(i > j)); //!false
System.out.println(!!(i > j)); //!!false
System.out.println(!!!(i > j)); //!!!false
**/
//&& (短路与) 有false则false
System.out.println((i > j) && (i > k)); //false && false
System.out.println((i < j) && (i > k)); //ture && false
System.out.println((i > j) && (i < k)); //false && ture
System.out.println((i < j) && (i < k)); //ture && ture
System.out.println("--------------------");
//|| (短路或) 有true则true
System.out.println((i > j) || (i > k)); //false || false
System.out.println((i < j) || (i > k)); //ture || false
System.out.println((i > j) || (i < k)); //false || ture
System.out.println((i < j) || (i < k)); //ture || ture
System.out.println("--------------------");
//&&与&的区别 (短路与和逻辑与)
System.out.println((i++ > 100) & (j++ > 100)); //false & flase
System.out.println("i:" + i); //i:11
System.out.println("j:" + j); //j:21
System.out.println((i++ > 100) && (j++ > 100)); //false & false
System.out.println("i:" + i); //i:12,当前面为false时,短路与不执行后面语句了
System.out.println("j:" + j); //所以此时j还是21
//||与|的区别 (短路或和逻辑或)
System.out.println((i++ > 100) | (j++ > 100)); //false & flase
System.out.println("i:" + i); //i:13
System.out.println("j:" + j); //j:22
System.out.println((i++ > 100) || (j++ > 100)); //false & false
System.out.println("i:" + i); //i:14,当前面为false时,短路与执行后面语句了
System.out.println("j:" + j); //所以此时j还是23
}
}
6.三元运算符
6.1三元运算符
int m = 30;
int n = 20;
System.out.println((m++ > 20) || (n++ > 10)); //true || true 坐标为true,右边不执行
System.out.println("m:" + m); //m=31
System.out.println("n:" + n); //n=20
6.2案例:两只老虎
/*
三元运算符
格式:
关系表达式 ? 表达式1 : 表达式2
范例:
a > b ? a : b;
执行流程:
首先计算关系表达式的值
如果值为true,表达式1的值就是运算结果
如果值为false,表达式2的值就是运算结果
*/
public class OperatorDemo08 {
public static void main(String[] args) {
//定义两个变量
int a = 10;
int b = 20;
//获取两个数据中的较大值
int max = a > b ? a : b;
//输出结果
System.out.println("max:" + max);
short c = 40;
short d = 30;
short max_ = c > d ? c : d;
System.out.println("max_:" + max_);
//获取两个数中较大的值
//算术表达式中包含多个基本数据类型的时候。整个数据表达式的类型会升级
//bytes、short、char会自动升级成int
short e = 60;
char f = 70;
//short max__ = e > f ? e : f;
int max__ = e > f ? e : f;
System.out.println("max__:" + max__);
}
}
分析:
(1) 定义两个变量用于保存老虎的体重,单位为kg,这里仅仅体现数值即可。
int weight1 = 180;
int weight2 = 200;
(2) 用三元运算符实现老虎体重的判断,体重相同,返回true,否则,返回false.
(weight1 == weight2)? true: false;
(3) 输出结果
6.3案例:三个和尚
/*
两只老虎
需求:
动物园里有两只老虎,已知两只老虎的体重分别为180kg,200kg,
请用程序实现判断两只老虎的体重是否相同。
*/
public class OperatorDemo09 {
public static void main(String[] args) {
//1.定义两个变量用于保存老虎的体重,单位为kg,这里仅仅体现数值即可
int weight1 = 180;
int weight2 = 200;
//2.用三元运算符实现老虎体重的判断,体重相同,返回true,否则,返回false
boolean b = weight1 == weight2 ? true : false;
//3.输出结果
System.out.println("b:" + b);
}
}
分析:
(1) 定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可。
int height1 =150;
int height2 = 210;
int height3 = 165;
(2) 用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。
(height1 > height2) ? height1 : height2;
(3) 用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。
(tempHeight > height3) ? tempHeight:height3;
(4) 输出结果
数据输入
1.1数据输入概述
1.2 Scanner使用的基本方法
/*
三个和尚
需求:
一座寺庙里住着三个和尚,已知他们的身高
分别为150cm、210cm、165cm,请用程序实
现获取这三个和尚的最高身高。
*/
public class OperatorDemo10 {
public static void main(String[] args) {
//1.定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可。
int height1 = 150;
int height2 = 210;
int height3 = 165;
//2.用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。
int tempHeight = (height1 > height2) ? height1 : height2;
//3.用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。
int maxHeight = (tempHeight > height3) ? tempHeight : height3;
//4.输出结果
System.out.println("maxHeight:" + maxHeight);
}
}
1.3案例:三个和尚
/*
数据输入:
导包:
import java.util.Scanner;
创建对象:
Scanner sc = new Scanner(System.in);
接收数据:
int x = sc.nextInt();
*/
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
int x = sc.nextInt();
//输出数据
System.out.println("x:" + x );
}
}
分析
(1) 身高未知,采用键盘录入实现。首先导包,然后创建对象。
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
(2) 键盘录入三个身高分别赋值给三个变量。
int height1 = sc.nextlnt();
int height2 = sc.nextlnt();
int height3 = sc.nextlnt();
(3) 用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。
(height1 > height2) ? height1 : height2;
(4) 用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。
(tempHeight > height3) ? tempHeight:height3;
(5) 输出结果。
/*
三个和尚
需求:
一座寺庙里住着三个和尚,他们的身高
必须经过测量得出,请用程序实现获取
这三个和尚的最高身高。
*/
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
//1.身高未知,采用键盘录入实现。首先导包,然后创建对象。
Scanner sc = new Scanner(System.in);
//2.键盘录入三个身高分别赋值给三个变量。
System.out.println("请输入第一个和尚的身高:");
int height1 = sc.nextInt();
System.out.println("请输入第二个和尚的身高:");
int height2 = sc.nextInt();
System.out.println("请输入第三个和尚的身高:");
int height3 = sc.nextInt();
//3.用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。
int tempHeight = height1 > height2 ? height1: height2;
//4.用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。
int maxHeight = tempHeight > height3 ? tempHeight : height3;
//5.输出结果。
System.out.println("这三个和尚中身高最高的是" + maxHeight + "cm");
}
}
评论列表(0条)