-----01运算符-----
运算符的作用是什么?
把常量或者是变量连接起来的符号。
运算符:对常量或者变量进行操作的符号。
表达式:通过运算符连接起来的式子。
例如:
+ 是运算符,10+20 是表达式
- 是运算符,a - b 是表达式
常用的运算符:
符号 | 作用 | 说明 |
- | 减 | 进行减法操作 |
+ | 加 | 对数字进行加法运算,对字符串进行拼接。 |
* | 乘 | 进行乘法操作 |
/ | 除 | ==整数相除的结果只能是整数,想要得到小数,必须有浮点数参与运算。== |
% | 取余 | ==a%b,获得a和b相除的余数。== |
/* 算术运算符 */ public class OperatorDemo01 { public static void main(String[] args) { //定义两个变量 int a = 6; int b = 4; System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / b); System.out.println(a % b); //除法得到的是商,取余得到是余数 //整数相除只能得到整数,要想得到小数,必须有浮点数的参与 System.out.println(6.0 / 4); } }
/* 字符的"+"操作 */ public class OperatorDemo02 { public static void main(String[] args) { //定义两个变量 int i = 10; char c = 'A'; //'A'的值是65 c = 'a'; //'a'的值是97 c = '0'; //'0'的值是48 System.out.println(i + c); //char ch = i + c; //char类型会被自动提升为int类型 int j = i + c; System.out.println(j); //int k = 10 + 13.14; double d = 10 + 13.14; } }
-
-
两个不同类型的数据在运算的时候,结果取决于大的数据类型。
// 1. byte\ short\char数据类型在运算的时候会自动提升至int类型 byte b1 = 1; byte b2 = 2; int b3 = b1+b2; System.out.println(b3); //2.两个不同类型的数据在运算的时候,结果取决于大的数据类型。 int i = 10; double d = 3.0; double result4 = i+d; System.out.println(result4); int a = 10; int b =3; int result5 = a/b; //3.33333333333 System.out.println(result5); //3
拓展
【笔试题】 有两个变量定义如下: byte b = 10; short s = 20; 指出下面一段代码中有问题的代码行号( 1,2,4 ) 1 short s2 = b + s; //有问题, 结果是int类型。 2 short s3 = s + 5; //有问题, 结果是int类型。 3 short s4 = 10 + 20; //没问题。,java编译器在编译的时候可以检查到常量的。 4 float f = b + 1.0; //有问题 , 结果是double类型 5 double d = s + 1.0F; //没问题 -------------------------------------------------------------- 【单选题】以下关于数据类型的转换,说法错误的是(B ) A:int a = 1; byte b = 1; System.out.println(a+b); 程序不会报错并且打印2 B:int类型的数据转为double类型,需要强制转换。 C:int类型转成short砍掉2个字节,可能造成数据丢失。 D:short h = 1; h=h+1; 需要使用强制转换才能编译-成功。
字符参与运算的规则:
==将字符换算成编码表里对应的数值进行运算。==
编码表:
计算机底层使用二进制运算,所以文字和符号必须转换成二进制数才能被计算机识别,编码表就是将文字等符号编成对应的数的表格,因为二进制记录起来比较麻烦,所以使用十进制数进行文字编码。
世界上第一张编码表ASCII(美国标准信息交换码)。常用编码表都兼容ASCII码表。
常用的编码:
ASCII编码:美国标准信息交换码,世界上第一张编码表。
GB2312编码:简体中文编码,对常用汉字进行了编码,兼容ASCII。
GBK:加强版的中文编码表,对更多汉字进行了编码,兼容ASCII
UTF-8编码:万国码表,常作为浏览器上的网页编码,可以统一显示各国文字,兼容ASCII。
常见字符对应数:
‘a' ~ 'z' : 97 ~ 122
'A' ~ 'Z' : 65 ~ 90
'0' ~ '9' : 48 ~ 57
基本汉字:19968 ~ 40869
/* 字符的"+"操作 */ public class OperatorDemo02 { public static void main(String[] args) { //定义两个变量 int i = 10; char c = 'A'; //'A'的值是65 c = 'a'; //'a'的值是97 c = '0'; //'0'的值是48 System.out.println(i + c); //char ch = i + c; //char类型会被自动提升为int类型 int j = i + c; System.out.println(j); //int k = 10 + 13.14; double d = 10 + 13.14; } }
字符串使用+的特点
/* 字符串的"+"操作 */ public class OperatorDemo03 { public static void main(String[] args) { System.out.println("it" + "heima"); System.out.println("itheima" + 666); System.out.println(666 + "itheima"); System.out.println("华为" + 6 + 66); System.out.println(1 + 99 + "年华为"); } }
小结
-
字符串使用+并不是做加法运算,而是做拼接。
-
任何数据与字符串使用+的时候结果都是字符串。
以下语句的输出结果是什么? System.out.println("hello" + 3 + 4); // hello34 System.out.println(3 + 4 + "hello"); // 7hello System.out.println(3 + 4 + "hello" + 5 + 6); //7hello56 System.out.println(3 + 4 + "hello" + (5 + 6)); 7hello11
--------
--------赋值运算符有哪些?赋值运算符的特点
符号 | 作用 | 说明 |
= | 赋值 | a=10,将10赋值给变量a |
+= | 加后赋值 | a+=b,将a+b的值给a |
-+ | 减后赋值 | a-=b,将a-b的值给a |
*= | 乘后赋值 | a*=b,将a×b的值给a |
/= | 除后赋值 | a/=b,将a÷b的商给a |
%= | 取余后赋值 | a%=b,将a÷b的余数给a |
/* 赋值运算符 */ public class OperatorDemo { public static void main(String[] args) { //把10赋值给int类型的变量i int i = 10; System.out.println("i:" + i); // += 把左边和右边的数据做加法操作,结果赋值给左边 i += 20; //i = i + 20; System.out.println("i:" + i); //注意:扩展的赋值运算符底层隐含了强制类型转换 short s = 10; s += 20; //s = (short)(s + 20); System.out.println("s:" + s); } }
赋值运算符在运算的时候隐式进行强制类型转换。
练习1:
//以下代码是否会报错? short s = 10; s = s + 10; //报错, 需要强制类型转换 s+=10; //不会, 因为赋值运算符隐式维护了强制类型转换。
练习2:
【练习题】以下不属于赋值运算符的是(D,E ) A:= B:*= C:%= D:++ E:==
使用说明
1、++ 、 -- 可以放在变量的前面,也可以放在变量的后面。
单独进行运算时放在前面或者后面,效果一样。
2、参与其他运算的时候:
==a++ :先将a的值参与其他运算,运算结束后再自身加1。==
==++a :先将a自身加1,再将自增后的值参与其他运算。==
自减与自增规则相同。
3、最常见用法:单独使用。
/* 自增自减运算符 */ public class OperatorDemo { public static void main(String[] args) { //定义变量 int i = 10; System.out.println("i:" + i); //单独使用 //i++; //++i; //System.out.println("i:" + i); //参与操作使用 //int j = i++; int j = ++i; System.out.println("i:" + i); System.out.println("j:" + j); } }
-
-
自增本质上就是操作数+1 比如: int a = 1 ; a++ 相当于 a = a+1
-
自减的本质上就是操作数-1. 比如: int a = 1 ; a-- 相当于 a = a-1
-
-
前自增、后自增独立运算与其他操作数运算的特点?
-
独立运算的时候前自增与后自增没有任何的区别。
-
自增与其他操作数在在运算的时候是有区别的:
-
后自增: 先给别人去使用,然后再自增。
-
前自增: 先自增,然后再给比人去使用
-
-
符号 | 作用 | 说明 |
== | 等于 | a == b,判断如果a等于b,则为true,否则为false |
!= | 不等于 | a!=b,判断如果a不等于b,则为true,否则为false |
> | 大于 | a>b,如果a大于b,则为true,否则为false |
>= | 大于等于 | a>=b,如果a大于等于b,则为true,否则为false |
< | 小于 | a<b,如果a小于b,则为true,否则为false |
<= | 小于等于 | a<=b,如果a小于等于b,则为true,否则为false |
/* 关系运算符案例代码 */ public class GuanXiDemo { 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); } }
= 是赋值
== 判断是否等于
-------- --------
逻辑运算符有哪些?
符号 | 说明 | 格式 | 运算结果 |
& | 逻辑与 | 所有表达式均为true,结果才为true | |
| | 逻辑或 | 任意表达式为true,结果就为true | |
! | 逻辑非 | ||
^ | 逻辑异或 | 左右两边结果不同为true,相同为false |
/* 逻辑运算符 */ public class OperatorDemo01 { 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)); //true & false System.out.println((i > j) & (i < k)); //false & true System.out.println((i < j) & (i < k)); //true & true System.out.println("--------"); //| 有true则true System.out.println((i > j) | (i > k)); //false | false System.out.println((i < j) | (i > k)); //true | false System.out.println((i > j) | (i < k)); //false | true System.out.println((i < j) | (i < k)); //true | true System.out.println("--------"); //^ 相同为false,不同为true System.out.println((i > j) ^ (i > k)); //false ^ false System.out.println((i < j) ^ (i > k)); //true ^ false System.out.println((i > j) ^ (i < k)); //false ^ true System.out.println((i < j) ^ (i < k)); //true ^ true 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 OperatorDemo02 { 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)); //true && false System.out.println((i > j) && (i < k)); //false && true System.out.println((i < j) && (i < k)); //true && true System.out.println("--------"); //|| 有true则true System.out.println((i > j) || (i > k)); //false || false System.out.println((i < j) || (i > k)); //true || false System.out.println((i > j) || (i < k)); //false || true System.out.println((i < j) || (i < k)); //true || true System.out.println("--------"); //&&和& //System.out.println((i++ > 100) & (j++ > 100)); //false & false System.out.println((i++ > 100) && (j++ > 100)); //false && false System.out.println("i:" + i); System.out.println("j:" + j); } }
小结
-
什么时候需要使用逻辑运算符?
逻辑运算符是用于连接关系型表达式,两个条件。
public class Demo1{ public static void main(String[] args){ //年龄 int age = 15; //工作经验经验 int workAge = 1; System.out.println(age>=16 & workAge>=1); } }
2.逻辑运算符的规律?
-
-
| 或者 ,左右两边表达式只有有一个true,结果则为true。
-
^ 异或 , 左右两边表达式的结果如果不一样则为true,一样为false。
-
! 非, 取反。
3.短路与和单与的区别?
-
-
短路或只要左边的表达式为true,那么结果则为true。不会再运算右边的表达式了。
--------
执行流程:
执行判断表达式,判断结果是boolean类型,为true时执行表达式1并返回结果,false执行表达式2并返回结果。
/* 三元运算符 格式: 关系表达式 ? 表达式1 : 表达式2; 范例: a > b ? a : b; 执行流程: 首先计算关系表达式的值 如果值为true,表达式1的值就是运算结果 如果值为false,表达式2的值就是运算结果 */ public class OperatorDemo { public static void main(String[] args) { //定义两个变量 int a = 10; int b = 20; //获取两个数据中的较大值 int max = a > b ? a : b; //输出结果 System.out.println("max:" + max); } }
案例:两只老虎
/* 两只老虎 需求: 动物园里有两只老虎,已知两只老虎的体重分别为180kg、200kg, 请用程序实现判断两只老虎的体重是否相同。 */ public class OperatorTest01 { 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); } }
案例:三只和尚
/* 三个和尚 需求: 一座寺庙里住着三个和尚,已知他们的身高分别为150cm、210cm、165cm, 请用程序实现获取这三个和尚的最高身高。 */ public class OperatorTest02 { 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:值2
-
三元运算符使用的时候要注意的事项
-
使用三元运算符的时候一定需要定义变量接收结果或者是直接使用结果。
-
public static void main(String[] args){ //从a与b两个变量中获取最大值 int a = 10; int b = 20; /* //方式一: 定义变量接收结果。 int result = a>b?a:b; System.out.println(result); */ //方式二: 直接使用结果 System.out.println("最大值为:"+ (a>b?a:b)); }
## Scanner键盘录入 #### 目标 使用Scanner的步骤? 使用Scanner完成升级三个和尚小案例 #### 随堂代码 **scanner基本使用** ```java /* 数据输入: 导包: 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); } }
案例: 使用Scanner完成三个和尚
/* 三个和尚 需求: 一座寺庙里住着三个和尚,他们的身高必须经过测量得出,请用程序实现获取这三个和尚的最高身高。 */ import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { //身高未知,采用键盘录入实现。首先导包,然后创建对象。 Scanner sc = new Scanner(System.in); //键盘录入三个身高分别赋值给三个变量。 System.out.println("请输入第一个和尚的身高:"); int height1 = sc.nextInt(); System.out.println("请输入第二个和尚的身高:"); int height2 = sc.nextInt(); System.out.println("请输入第三个和尚的身高:"); int height3 = sc.nextInt(); //用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。 int tempHeight = height1 > height2 ? height1 : height2; //用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。 int maxHeight = tempHeight > height3 ? tempHeight : height3; //输出结果。 System.out.println("这三个和尚中身高最高的是:" + maxHeight +"cm"); } }
小结
-
使用Scanner的步骤?
-
使用Scanner的作用: 可以接收键盘录入数字。
-
使用Scanner的步骤
-
导包 import java.util.Scanner
-
创建对象 Scanner 变量名 = new Scanner(System.in);
-
接收数据 变量类型 变量名= 变量名.nextInt();
-
-
--------
/* 顺序结构 */ public class OrderDemo { public static void main(String[] args) { System.out.println("开始"); System.out.println("语句A"); System.out.println("语句B"); System.out.println("语句C"); System.out.println("结束"); } }
小结
-
什么是顺序结构语句
从上到下按照代码的顺序去执行的语句称作为顺序结构
-
顺序结构语句的执行特点?
从上到下按照代码的顺序去执行的语句称作为顺序结构
if语句格式1
格式:
if (boolean表达式) { 语句体; }
执行流程:
①首先计算关系表达式的值。
②表达式的值为 true 时执行语句体,为 false 时不执行语句体。
③往下执行 if结构 后面的语句内容。
if(关系表达式){ 语句体1; }else{ 语句体2; }
执行流程:
①首先计算关系表达式的值。
②表达式的值为 true 时执行语句体1,为 false 时执行语句体2。
③继续执行 if-else 结构后面的语句内容。
//需求1:键盘录入一个整数,使用if-else结构该数是奇数还是偶数。 public class IfDemo2{ public static void main(String[] args){ } }
//需求2:使用if-else结构求出两个数的较大值 public class IfDemo3{ public static void main(String[] args){ int a = 10; int b = 20; } }
if(关系表达式1){ 语句体1; }else if(关系表达式2){ 语句体2; } else{ 语句体n+1; } 【注意】:该格式中可以有多个else if结构,也可以没有else结构。
执行流程:
①首先计算关系表达式1的值。
②如果为 true 则执行语句体1,为 false 时计算关系表达式2的值。
③如果为 true 则执行语句体2,为 false 时计算关系表达式3的值,以此类推。
④所有关系表达式都为false时,执行else里的语句体n+1。
//案例:键盘录入一个1~7的整数,输出对应的星期一到星期日。 //导包 import java.util.Scanner; public class IfTest{ public static void main(String[] args){ //创建Scanner对象 Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数(1~7):"); //接收键盘录入的整数 int week = sc.nextInt(); //使用if-elseif结构判断 if(week==1){ System.out.println("星期一"); }else if(week==2){ System.out.println("星期二"); }else if(week==3){ System.out.println("星期三"); }else if(week==4){ System.out.println("星期四"); }else if(week==5){ System.out.println("星期五"); }else if(week==6){ System.out.println("星期六"); }else if(week==7){ System.out.println("星期日"); }else{ System.out.println("您输入的数字无效!"); } } }
【练习】以下代码的打印结果是什么? AA int a = 10; int b = 10; if(a==b){ System.out.println("AA"); }else if(a==b){ System.out.println("BB"); }else{ System.out.println("CC"); }
/* * 案例2: * 键盘录入学生成绩,根据成绩判断学生属于哪个级别 * 90-100 优秀 * 80-90 好 * 70-80 良 * 60-70 及格 * 60以下 不及格 */ /* 考试奖励 需求: 小明快要期末考试了,小明爸爸对他说,会根据他不同的考试成绩,送他不同的礼物,假如你可以控制小明的得分, 请用程序实现小明到底该获得什么样的礼物,并在控制台输出。 奖励: 95~100 山地自行车一辆 90~94 游乐场玩一次 80~89 变形金刚玩具一个 80以下 胖揍一顿 */ import java.util.Scanner; public class IfTest02 { public static void main(String[] args) { //小明的考试成绩未知,可以使用键盘录入的方式获取值 Scanner sc = new Scanner(System.in); System.out.println("请输入一个分数:"); int score = sc.nextInt(); //由于奖励种类较多,属于多种判断,采用if...else...if格式实现 //为每种判断设置对应的条件 //为每种判断设置对应的奖励 /* if(score>=95 && score<=100) { System.out.println("山地自行车一辆"); } else if(score>=90 && score<=94) { System.out.println("游乐场玩一次"); } else if(score>=80 && score<=89) { System.out.println("变形金刚玩具一个"); } else { System.out.println("胖揍一顿"); } */ //数据测试:正确数据,边界数据,错误数据 if(score>100 || score<0) { System.out.println("你输入的分数有误"); } else if(score>=95 && score<=100) { System.out.println("山地自行车一辆"); } else if(score>=90 && score<=94) { System.out.println("游乐场玩一次"); } else if(score>=80 && score<=89) { System.out.println("变形金刚玩具一个"); } else { System.out.println("胖揍一顿"); } } }
小结
-
if语句的三种结构的特点与格式?
-
格式一: 只能适用于一种情况下去使用
if(关系表达式){ 符合条件执行的语句; }
==注意:符合条件的时候如果只有一个语句需要执行,那么可以省略大括号不写==
-
if(关系表达式){ 如果关系表达式是true,则执行语句1 }else{ 如果关系表达式是false,则执行语句2 }
==if语句的格式二与三元运算符的区别:==
-
-
三元运算符的比较简洁。
-
//需求: 比较a与b两个变量的大小,如果a大于b,输出a大。 否则输出b大. int a = 10; int b =4; /* if(a>b){ System.out.println("a大"); }else{ System.out.println("b大"); } */ //把上述的需求转换为三元运算符 a>b?a:b;
-
格式三: 符合多种条件
if(条件1){ 符合条件1执行的语句 }else if(条件2){ 符合条件2执行的语句 }else if(条件3){ 符合条件3执行的语句 }....else{ 如果上述条件都不满足的情况执行的语句 }
-
三种结构的应用场景?
格式一: 如果只有一种情况下去使用
格式二: 有两种情况下去使用
格式三: 有多种情况下去使用
-