运算符
逻辑运算符
&—逻辑与
| —逻辑或
!—逻辑非
&& —短路与
|| —短路或
^ —逻辑异或
-
逻辑运算符用于连接布尔型表达式,
在Java中不可以写成3<x<6,应该写成x>3 & x<6
。 -
“&”和“&&”的区别:
- 单&时,
左边无论真假,右边都进行运算
; - 双&时,
如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算
。public class Z01 { public static void main(String[] args) { int i=0; int k=1; System.out.println(++i !=0 && ++k == 2); System.out.println(++i !=0 & ++k == 2); } }
- 单&时,
-
“|”和“||”的区别同理,||表示:
当左边为真,右边不参与运算
。 -
在不需要逻辑运算两边都参与运算的时候,尽量使用&&和||
-
异或( ^ )与或( | )的不同之处是:当左右都为true时,结果为false。
理解:异或,追求的是“异”!public class Z01 { public static void main(String[] args) { int i=0; int k=1; System.out.println(++i !=0 || ++k == 2); System.out.println(++i !=0 | ++k == 2); } }
试一试
public class Z02 {
public static void main(String[] args) {
//例1
int x=1;
int y=1;
if(x++==2 & ++y==2) {
x=7;
}
System.out.println("x="+x+",y="+y); //2 2
// 例2
// int x=1,y=1;
// if(x++==1 | ++y==1) {
// x=7;
// }
// System.out.println("x="+x+",y="+y); // 7 2
//例3
// int x=1,y=1;
// if(x++==2 && ++y==2) {
// x=7;
// }
// System.out.println("x="+x+",y="+y); //2 1
//例4
// int x=1,y=1;
// if(x++==1 || ++y==1) {
// x=7;
// }
// System.out.println("x="+x+",y="+y); //7 1
}
}
位运算符
三目运算符
- 格式:
(条件表达式)? 表达式1:表达式2;- 为true,运算后的结果是表达式1;
- 为false,运算后的结果是表达式2;
运算符的优先级
-
Java 语言中大部分运算符
也是从左向右结合
的,只有单目运算符、赋值运算符和三目运算符例外,其中,单目运算符、赋值运算符和三目运算符是从右向左结合的,也就是从右向左运算
。 -
算术运算符优先级较高,关系和逻辑运算符优先级较低
。多数运算符具有左结合性,单目运算符、三目运算符、赋值运算符具有右结合性。
-
使用优先级为 1 的小括号可以改变其他运算符的优先级。
-
即如果需要将具有较低优先级的运算符先运算,则可以使用小括号将该运算符和操作符括起来。例如下面的表达式:
(x-y)*z/5
- 在这个表达式中先进行括号内的减法运算,再将结果与 z 相乘,最后将积除以 5 得出结果。整个表达式的顺序按照从左向右执行,比较容易理解。
程序流程控制
顺序结构
- 程序从上到下逐行地执行,中间没有任何判断和跳转。
分支结构
- 根据条件,选择性地执行某段代码。
- 有if…else和switch两种分支语句。
循环结构
- 根据循环条件,重复性的执行某段代码。
- 有while、do…while、for三种循环语句。
- 注:JDK1.5之后提供了foreach循环,方便的遍历集合、数组元素。
- Java中定义成员变量时采用合法的前向引用。如:
public class Test{
int num1 = 12;
int num2 = num1 + 2;
}
错误形式:
public class Test{
int num2 = num1 + 2;
int num1 = 12;
}
分支语句
if-else语句
if语句三种格式:
1. if(true){
执行代码块;
}
2. if(条件表达式){
执行代码块;
}
else{
执行代码块;
}
3. if(条件表达式){
执行代码块;
}
else if (条件表达式){
执行代码块;
}
……
else{
执行代码块;
}
public class Z04 {
public static void main(String[] args) {
// int i=1;
// if(i==1) {
// System.out.println(i);
// }
// int i=2; //判断是奇数还是偶数,输出结果
// if(i%2==0) {
// System.out.println("偶数");
// }else {
// System.out.println("奇数");
// }
//如果是数字1,输出星期一,2输出星期二,3就输出星期三
// int i=1;
// if(i==1) {
// System.out.println("星期一");
// }else if(i==2) {
// System.out.println("星期二");
// }else if(i==3){
// System.out.println("星期三");
// }
//如果是数字1,输出星期一,2输出星期二,3就输出星期三,如果不是任何一个,则输出不知道星期几
int i=6;
if(i==1) {
System.out.println("星期一");
}else if(i==2) {
System.out.println("星期二");
}else if(i==3){
System.out.println("星期三");
}else {
System.out.println("不知道星期几");
}
}
}
例1
public class Z05 {
public static void main(String args[]){
int age = 75;
if (age< 0) {
System.out.println("不可能!");
} else if (age>250) {
System.out.println("是个妖怪!");
} else {
System.out.println("人家芳龄 " + age +" ,马马乎乎啦!");
}
}
}
例2
//判断小明的期末成绩。
//当成绩为100分时,奖励一辆BMW;
//当成绩为(80,99]时,奖励一个台iphone5s;
//当成绩为[60,80]时,奖励一本参考书;
//其它时,什么奖励也没有。
public class Z06 {
public static void main(String args[]){
int score = 75;
if(score==100) {
System.out.println("奖励一辆BMW");
}else if(score>80 && score <=99) {
System.out.println("奖励一个台iphone5s");
}else if(score >=60 && score <=80) {
System.out.println("奖励一本参考书");
}else {
System.out.println("什么奖励也没有");
}
}
}
switch语句
switch(变量){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;
… …
case 常量N:
语句N;
break;
default:
语句;
break;
}
例1
public class Z07 {
public static void main(String[] args) {
int i=2;
switch(i) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
}
}
}
例2
public class Z08 {
public static void main(String[] args) {
int i=8;
switch(i) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
default: //等同于else
System.out.println("不知道星期几");
break;
}
}
}
例3
public class Z09 {
public static void main(String args[]) {
int i = 1;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
default:
System.out.println("default");
break;
}
}
}
例4
public class Z09 {
public static void main(String[] args) {
String str="a";
switch(str) {
case "a":
System.out.println("a");
break;
case "bcd":
System.out.println("bcd");
break;
default:
System.out.println("unknow");
break;
}
}
- switch(表达式)中表达式的
返回值必须是下述几种类型之一:
byte,short,char,int,枚举,String`; - case子句中的
值必须是常量
,且所有case子句中的值应是不同的
; - default子句是
可任选的
,当没有匹配的case时,执行default - break语句用来在执行完一个case分支后使程序跳出switch语句块;
如果没有break,程序会顺序执行到switch结尾
。break必须写!!!
试一试
- 使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输出 “other”。
public class Z09 {
public static void main(String[] args) {
char c='a';
switch(c) {
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
case 'c':
System.out.println("C");
break;
case 'd':
System.out.println("D");
break;
case 'e':
System.out.println("E");
break;
default:
System.out.println("other");
break;
}
}
}
- 对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
public class Z09 {
public static void main(String[] args) {
int score = 75;
switch(score/60) {
case 0:
System.out.println("不合格");
break;
case 1:
System.out.println("合格");
break;
}
}
}
- 根据用于指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季。
public class Z09 {
public static void main(String[] args) {
int month = 5;
switch(month) {
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
}
}
}
- 使用switch语句改写下列if语句:
int a = 3;
int x = 100;
if(a==1)
x+=5;
else if(a==2)
x+=10;
else if(a==3)
x+=16;
else
x+=34;
AC
int a=3;
int x=100;
switch(a) {
case 1:
x+=5;
break;
case 2:
x+=10;
break;
case 3:
x+=16;
break;
default:
x+=34;
break;
}
switch和if语句的对比
-
如果
判断的具体数值不多
,而且符合byte、 short 、int、 char这四种类型。虽然两个语句都可以使用,建议使用swtich语句
。因为效率稍高。 -
其他情况:
对区间判断,对结果为boolean类型判断
,使用if
,if的使用范围更广。