Java EE基础班课程(第二天)
学习目标:
- 类型转换
- 自动类型转换
- 强制类型转换
- 运算符
- 算数运算符
- (+ - * / %)
- 字符的+
- 字符串+
- 关系运算符
- (> >= < <= == !=)
- 逻辑运算符
- & | ! ^
- && ||
- 三元运算符
- 自增自减运算符
- ++
- 算数运算符
- 流程控制语句
- 顺序结构
- 分支结构/选择结构
- 循环结构
一.类型转换
1.1自动类型转换
/*
隐式类型转换/自动类型转换
1.理解 就是小的数据类型 向大的数据类型转换
2.转换规则:
byte --> short
--> int --> long --> float --> double
char
3.注意:
byte short 不能直接相互转换 char
byte short char 不能直接参与运算,必须要先转成int类型的数据在参与运算,结果是int类型的数据
*/
public class Demo07{
public static void main(String[]args){
byte b1 = 1;
short s = 1;
short sum = s+b1;// 不兼容的类型: 从int转换到short可能会有损失
int sum = s+b1;
System.out.println(sum);
long l1 = 123456L;
double d1 = 23456;
double sum2 = l1+d1;
System.out.println(sum2);
byte b2 = 1;
int i = 2000;
int sum3 = b2+i;
System.out.println(sum3);
}
}
1.2强制类型转换
//大的数据类型向小的数据类型转换 需要强制类型转换
public class Test {
public static void main(String[] args) {
byte a = 3; //①
byte b = 4; //②
//byte c = a + b; //③ 出错 不兼容的类型: 从int转换到byte可能会有损失
/*
解决方案:1.直接把byte 类型的c 改成int类型的c
*/
int c = a + b;
/*
解决方案:2.使用强制类型转换
强制类型转换格式:
目标数据类型 变量名 = (目标数据类型)(值或变量)
强制类型转换有可能回造成数据精度损失
举栗子:
int a = 10;
a = 128;
byte b = (byte)a;//-128--127
*/
byte e = (byte)(a + b);
byte d = 3 + 4; //④ ??? 不报错 java有一个常量优化机制
}
}
1.3强制类型转换中数据溢出的问题
public class Demo08{
public static void main(String[]args){
int a = 10;
a = 129;
byte b = (byte)a;//-128--127 所有数据类型在取值范围是首尾相连的,循环计算
System.out.println(b);
}
}
二,运算符
2.1算术运算符
/*
算术运算符:
分类:
+ 求和
- 求差
* 求积
/ 求商
% 求余
*/
public class Demo09{
public static void main(String[]args){
int a = 10 ;
int b = 20 ;
int c = a+b;
System.out.println(c);
int d = a - b;
System.out.println(d);
int e = a * b;
System.out.println(e);
int f = b/a;
System.out.println(f);
int g = a/b;
System.out.println(g);
int h = 6/7;
System.out.println(h);
int i = b%a;//20%10
System.out.println(i); //0
int j = a%b; //10%20
System.out.println(j); // 10
int k = 6%10;
System.out.println(k); // 6
}
}
2.2算术运算符小练习
import java.util.Scanner;
/*
键盘录入一个4位数,求各个位上的数字
分析:
1.用到键盘录入
a.导包
b.创建对象
c.接收键盘录入的数据 1234/100 =12 12%10 = 2
2.求各个位上数字
千位 / 1000
百位 /100%10
十位 /10%10
个位 %10
*/
public class Demo10{
public static void main(String[]args){
//b.创建对象
Scanner sc = new Scanner(System.in);
//c.接收键盘录入的数据
System.out.println("请输入一个4位数:");
int num = sc.nextInt(); //1234%10 = 4
//2.求各个位上数字
int ge = num%10;
int shi = num/10%10; // 1234/10 = 123 123%10 = 3
int bai = num/100%10;// 1234/100= 12 12%10 = 2
int qian = num/1000; // 1234/1000=1 1%10 = 1
System.out.println(ge);
System.out.println(shi);
System.out.println(bai);
System.out.println(qian);
}
}
2.3字符的加操作
/*
字符加操作
字符的加操作结果是int类型值,一般指的编码 单字节时候指的是ASCII码对应的值 '0' = 48 'A' =65 'a' = 97
*/
public class Demo11{
public static void main(String[]args){
char x = 'a';
int y = 1;
int z = x+y;
System.out.println(z); // 98 说明一个问题:字符a的转成int类型的值是97
x = 'b';
int m = x+1;
System.out.println(m); // 99 说明一个问题:字符b的转成int类型的值是98
char i = '0';
int j = i+1;
System.out.println(j);// 49 说明一个问题:字符0的转成int类型的值是48
}
}
2.4字符串的加操作
/*
字符串加操作
就是拼接,字符串的拼接, 遇到字符串才拼接
*/
public class Demo12{
public static void main(String[]args){
System.out.println(1+2+3+"HelloWorld"+4+5+6); //123HelloWorld456
System.out.println(1+2+3+"HelloWorld"+4+5+(6+7+8+9)); // 6HelloWorld4530
}
}
2.5自增自减运算符 ++ –
/*
自增自减运算符
++
对变量自身自增一
单独使用时:
++在变量前面或者在变量的后面,都是对变量自增一
参与操作时:
++在变量的后面,先参与运算,再自增一
++在变量的前面,先自增一,再参与运算
--
对变量自身自减一
单独使用时:
--在变量前面或者在变量的后面,都是对变量自减一
参与操作时:
--在变量的前面,先自减一,再参与运算
--在变量的后面,先参与运算,再自减一
*/
public class Demo13{
public static void main(String[]args){
int a = 10;
//单独使用
//a++;
//++a;
//参与操作
//int b = a++;
//int b = ++a;
//System.out.println(a);
//System.out.println(b);
//单独使用
//a--;
//--a;
//参与操作
//int c = --a;
int c = a--;
System.out.println(a);
System.out.println(c);
}
}
2.6关系运算符
/*
关系运算符
> >= < <= == !=
关系运算符运算的结果是布尔类型的值 true和false
*/
public class Demo14{
public static void main(String[]args){
int a = 10;
int b = 20;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
a = 20;
System.out.println(a>=b);
System.out.println(a<=b);
}
}
2.7 逻辑运算符
/*
逻辑运算符
& 见false则false
| 见true 则true
! 取反
^ 相同为false 不同true
逻辑运算符两边放的是关系表达式,逻辑运算符运算结果是布尔类型的值,true 或 false
*/
public class Demo15{
public static void main(String[]args){
int a = 10;
int b = 20;
int c = 30;
//逻辑与 & 见false则false
System.out.println((a>b) & (a>c)); // false & false ==> false
System.out.println((a>b) & (a<c)); // false & true ==> false
System.out.println((a<b) & (a>c)); // true & false ==> false
System.out.println((a<b) & (a<c)); // true & true ==> true
System.out.println("======================================");
//逻辑或 | 见true 则true
System.out.println((a>b) | (a>c)); // false | false ==> false
System.out.println((a>b) | (a<c)); // false | true ==> true
System.out.println((a<b) | (a>c)); // true | false ==> true
System.out.println((a<b) | (a<c)); // true | true ==> true
System.out.println("======================================");
//逻辑异或 ^ 相同为false 不同true
System.out.println((a>b) ^ (a>c)); // false ^ false ==> false
System.out.println((a>b) ^ (a<c)); // false ^ true ==> true
System.out.println((a<b) ^ (a>c)); // true ^ false ==> true
System.out.println((a<b) ^ (a<c)); // true ^ true ==> false
System.out.println("======================================");
//逻辑非 !
System.out.println(a>b);
System.out.println(!(a>b));
System.out.println(!!(a>b));
System.out.println(!!!(a>b));
}
}
2.8短路的逻辑运算符
/*
短路逻辑运算符
&& 见false则false ,只要左边表达式的值为false 右边就不执行
|| 见true 则true ,只要左边表达式的值为true 右边就不执行
*/
public class Demo16{
public static void main(String[]args){
int a = 10;
int b = 20;
/*System.out.println((a++ > 20) & ( b++ > 30)); //false & false ==> false
System.out.println((a++ > 20) && ( b++ > 30)); //false & false ==> false
System.out.println(a);
System.out.println(b); */
//System.out.println((a++<20) | (b++>30)); // true | false ==> true
System.out.println((a++<20) || (b++>30)); // true
System.out.println(a);
System.out.println(b);
}
}
当用&连接时:
当用&&连接时:
当用|连接时:
当用||连接时:
2.9三元运算符
/*
三元运算符:
格式:
关系表达式 ? 表达式1 :表达式2
运算规则:
1.计算关系表达式的值
2.如果关系表达式的值为true 取表达式1的值
3.如果关系表达式的值为false 取表达式2的值
*/
public class Demo17{
public static void main(String[]args){
int a = 10;
int b = 20;
int x = a > b ? a : b;
System.out.println(x);
int y = a < b ? a : b;
System.out.println(y);
}
}
2.10三元运算符案例
/*
求三个和尚最高身高,使用三元运算符
int hs1 = 165;
int hs2 = 210;
int hs3 = 175;
分析:
1.先使用三元运算符求出前两个和尚的最高身高
2.在使用两个和尚的最高身高和第三个和尚的身高比较
3.最终取出最高身高
*/
public class Demo18{
public static void main(String[]args){
int hs1 = 165;
int hs2 = 210;
int hs3 = 175;
//定义临时的最高身高用来存放 前两个和尚比较的最高身高
int temp = hs1>hs2 ? hs1 : hs2;
//定义最高身高的变量 接收临时最高身高和第三个和尚比较的值
int max = temp>hs3?temp:hs3;
System.out.println("三个和尚的最高身高:"+max+"cm");
}
}
三,流程控制语句
3.1 分类
1.顺序结构
代码从上到下 从左到右依次执行
2.选择结构/分支结构
通过判断条件来决定代码执行流程
分类:
if 语句
switch语句
3.循环结构
通过循环来执行代码,直到判断条件为false 结束循环的执行
分类:
for循环
while循环
do-while循环
3.2顺序结构
/*
顺序结构:
执行流程,代码从左到右,从上到下依次执行
*/
public class Demo19{
public static void main(String[]args){
System.out.println("开始");
System.out.println(1+2+3+4+"执行1"+5+6+7+8+9);
System.out.println("执行2");
System.out.println("执行3");
System.out.println("执行4");
System.out.println("执行5");
System.out.println("执行6");
System.out.println("结束");
}
}
3.3分支结构/选择结构
3.3.1 if 语句
分类1:
if(关系表达式){
语句体
}
1.先判断关系表达式的值
如果关系表达式的值为true 执行语句体
如果关系表达式的值为false 执行if语句外的其他语句
/*
分类1:
if(关系表达式){
语句体
}
1.先判断关系表达式的值
如果关系表达式的值为true 执行语句体
如果关系表达式的值为false 执行if语句外的其他语句
*/
public class Demo20{
public static void main(String[]args){
int a = 10;
a = 20;
System.out.println("开始");
if(a>10){
System.out.println("a大于10");
}
System.out.println("结束");
}
}
分类2:
if(关系表达式){
语句体1
}else{
语句体2
}
执行规则:
1.先判断关系表达式值
2.如果关系表达式的值为true 执行语句体1
3.如果关系表达式的值为false 执行语句体2
/*
分类2:
if(关系表达式){
语句体1
}else{
语句体2
}
执行规则:
1.先判断关系表达式值
2.如果关系表达式的值为true 执行语句体1
3.如果关系表达式的值为false 执行语句体2
*/
public class Demo21{
public static void main(String[]args){
int a = 10;
a=20;
System.out.println("开始");
if(a>10){
System.out.println("a大于10");
}else{
System.out.println("a不大于10");
}
System.out.println("结束");
}
}
/*
分类3:
if(关系表达式1){
语句体1
}else if(关系表达式2){
语句体2
}else {
语句体3
}
执行规则:
1.先判断关系表达式1值
2.如果关系表达式的值1为true 执行语句体1
3.如果关系表达式的值1为false 判断关系表达式2值
3.1 如果关系表达式2的值为true 执行语句体2
3.2 如果关系表达式2的值为false 执行语句体3
*/
public class Demo22{
public static void main(String[]args){
int a = 10;
a = 20;
System.out.println("开始");
if(a>10){
System.out.println("a大于10");
}else if(a==10){
System.out.println("a等于于10");
}else{
System.out.println("a小于10");
}
System.out.println("结束");
}
}
3.4 if语句案例
import java.util.Scanner;
/*
需求:小明快要期末考试了,小明爸爸对他说,会根据他不同的考试成绩,送他不同的礼物,
假如你可以控制小明的得分,请用程序实现小明到底该获得什么样的礼物,并在控制台输出。
礼物:95--100 自行车一辆
90--94 游乐场玩一次
80--89 变形金刚一个
80一下 胖揍一顿
分析:
1.键盘输入
2.判断成绩 输入不同礼物
*/
public class Demo23{
public static void main(String[]args){
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println("请输入小明的成绩:");
int score = sc.nextInt();
if(score<0 || score>100){
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("胖揍一顿");
}
}
}
他说,会根据他不同的考试成绩,送他不同的礼物,
假如你可以控制小明的得分,请用程序实现小明到底该获得什么样的礼物,并在控制台输出。
礼物:95--100 自行车一辆
90--94 游乐场玩一次
80--89 变形金刚一个
80一下 胖揍一顿
分析:
1.键盘输入
2.判断成绩 输入不同礼物
*/
public class Demo23{
public static void main(String[]args){
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println(“请输入小明的成绩:”);
int score = sc.nextInt();
if(score<0 || score>100){
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(“胖揍一顿”);
}
}
}
[外链图片转存中...(img-61NS5OT0-1627220782175)]
![image-20210725214216979](https://img-blog.csdnimg.cn/img_convert/88bad7bc791ea1b88707ab994f07139c.png)