JAVASE 基础模块四
-
IDEA快捷键
- psvm main函数
- sout 输出
- ctrl+/ 注释
- ctrl+shift+/ 多行注释
- ctrl+shift+F10 运行
- 变量.sout 输出变量
-
逻辑运算符
- 或者 |
- |的两端一段为true时结果为true 只有两端都为false结果为false
- 并且 &
- &的两端一段为false结果就为false 只有两端都为true时结果为true
- 取反 !
- true变false false变true
- 异或 ^
- 相同为false 不同为true
- true^true false^false 结果为false
- true^false false^true 结果为true
- &&短路与 ||短路或 &并且 |或者 的区别
- 他们在运算结果上是没有区别的 只是在运算效率上有区别
- & 左右端表达式都需要执行
- && 当左端为false时 右端的表达式就不执行
- && 当左端为true时 右端的表达式会执行
- | 左右端表达式都需要执行
- || 当左端为false时 右端的表达式会执行
- || 当左端为true时 右端的表达式就不执行
- 或者 |
-
位运算符
- &并且 |或 ^异或 ~按位取反 <<左移 >>右移 >>>无符号右移
- & | ^ 如果两端是数值 就是位运算符
- & | ^ 如果两端是布尔类型 就是逻辑运算符
- 注意 位运算符是直接对二进制补码进行运算 运算效率高一点
- &并且 有0则0
- 0 0000011
- 0 0000100
- 0 0000000
- 例如 3&4 结果为0
- |或者 有1则1
- 0 0000011
- 0 0000100
- 0 0000111
- 例如 3|4 结果为7
- ^异或 相同为0 不同为1
- 0 0000011
- 0 0000100
- 0 0000111
- 例如 3^4 结果为7
- ~按位取反
- 例如 3~4 结果为-4
- <<左移
- 0 0000011
- 0 000001100
- 0 0001100
- 3<<2 结果为12
- 被遗弃的高位丢弃 空位补0
- .>>右移
- .>>>无符号右移
- 无论正负 空位都补0
- 规律
- ^位异或 一个数,被另一个数 位异或两次 该数值不变
- <<左移 左边的数据乘以2的移动次幂
- 10<<3
- 10*2 *3
- 80
- .>>右移 左边的数据除以2的移动次幂
- 10>>3
- 10/2 *3
- 1
- &并且 |或 ^异或 ~按位取反 <<左移 >>右移 >>>无符号右移
-
值交换
-
方式一 通过中间变量进行值交换开发中常用
public static void main(String[] args) { int a=10; int b=20; int c=a; a=b; b=c; System.out.println(a); System.out.println(b); }
-
方式二 利用^这个运算符 一个数被另一个数异或两次该数不变
public static void main(String[] args) { int a=10; int b=20; a=a^b; b=a^b; a=a^b; System.out.println(a); System.out.println(b); }
-
其他方式 利用数值的加减运算进行值交换
-
-
三元运算符
-
表达式?结果一:结果二
-
当表达式的结果为true时 返回结果一 为false时 返回结果二
public static void main(String[] args) { int a = 333; int b = 777; int max=a>b?a:b; System.out.println(max);}
-
用三元运算符求三个数的最大值
public static void main(String[] args) { int a = 333; int b = 777; int c=999; int max=a>b?a:b; int max1=c>max?c:max; //int max=(a>b?a:b)>c?(a>b?a:b):c System.out.println(max1);}}
-
键盘录入
-
键盘录入
-
录入包
-
创建对象
-
提示用户输入数据
-
录入用户输入的数据
-
处理数据
//求三个数的最大值 public static void main(String[] args) { Scanner xcxc=new Scanner(System.in); System.out.println("请输入第一个整数"); int one=xcxc.nextInt(); System.out.println("请输入第二个整数"); int two=xcxc.nextInt(); System.out.println("请输入第三个整数"); int th=xcxc.nextInt(); int max=(one>two?one:two)>th?(one>two?one:two):th; System.out.println("最大值为"+max);}
-
CTRL+ALT+V ALT+ENTER 自动补全
流程控制语句
-
顺序结构
-
选择结构
- 选择语句 if
-
if(条件结果是布尔类型的表达式或者值){语句体}else{语句体}
-
当括号里的结果是true时 才会执行if后{}大括号里面的语句体
-
当括号里的结果是false时 才会执行else后{}大括号里面的语句体
-
if else语句是可以嵌套使用的
-
三元表达式能写出来的
-
if语句能写出来的三元表达式不一定能写出来
-
求三个数的最大值
public class MyTest2 { public static void main(String[] args) { Scanner a = new Scanner(System.in); System.out.println("请输入第一个数"); int b = a.nextInt(); Scanner c = new Scanner(System.in); System.out.println("请输入第二个数"); int d = c.nextInt(); Scanner e = new Scanner(System.in); System.out.println("请输入第三个数"); int f = e.nextInt(); int max = 0; if (b > d) {max = b; if (b > f) { max = b;} else {max = f;}} else {max = d; if (d > f) {max = d;} else {max = f;}} System.out.println("最大值"+max);}}
-
多重条件选择
-
if多重选择
public static void main(String[] args) { Scanner a = new Scanner(System.in); System.out.println("请输入你的成绩 0---100"); int b = a.nextInt(); if(b>0&&b<60){ System.out.println("回家种田吧"); }else if(b>=60&&b<80){ System.out.println("出门乞讨吧"); }else if(b>=80&&b<100){ System.out.println("瞅你那损塞"); }else if(b==100){ System.out.println("退学吧"); }else{ System.out.println("你输入了个锤子");} }
-
switch语句
-
switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2; break; case 值3: 语句体3; break; .... default: 语句体n+1; break; }
-
注意事项
- 支持的数据类型 byte short int char string 枚举
- case后面的量 一定是常量 不能写变量
- 多个case的值全部是同一类型
- 多个case项的值 不能有相同的
- break 结束switch语句 这个关键字可以省略不写 但是不写会发生 case穿透的现象
- defaut
-
-
多种选择的使用环境情况
-
switch语句 是针对有限个的常量进行选择 不能对范围进行选择
-
if 既能对常量进行选择 也能对范围进行选择
-
if语句的格式3: if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; }
-
循环语句
-
对于重复要进行的步骤 我们使用循环来做
-
for循环
for(初始化表达式语句;判断条件语句;控制条件语句) { 循环体语句; }
-
while循环
初始化条件语句; while(判断条件语句) { 循环体语句; 控制条件语句; }
-
do…while语句
初始化条件语句; do { 循环体语句; 控制条件语句; }while(判断条件语句);
-
-
for循环
- int i=0; 循环的初始值
- i<10; 循环的条件
- i++ 控制循环的语句 与 自加1
- 第一步执行 int i=0
- 第二步执行 i<10; TRUE
- 第三步执行 循环体 大括号里面的代码
- 第四步执行 i++
- …
- 循环二三四直至跳出循环条件 为FALSE时
public class MyTest { public static void main(String[] args) { for(int i=0;i<10;i++){ System.out.println("第"+(i+1)+"次输出:"+"没什么意义");}}}
-
打印1-10之间的和
public static void main(String[] args) { int sum=0; int sumx=0; int sumc=0; for(int i=1;i<10;i++){ sum+=i; } System.out.println("当前数字总和"+sum); }
-
打印1-100之间奇数的和
for(int l=1;l<100;l+=2){ sumc+=l; } System.out.println("当前奇数总和"+sumc); }
-
打印1-100之间偶数的和
public static void main(String[] args) { for(int j=0;j<100;j+=2){ sumx+=j; } System.out.println("当前偶数总和"+sumx); }
-
do…while循环至少执行一次循环体
-
而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句
待续…
-