七、运算符与表达式
1.运算符的分类 2.各种运算符的使用方法
六类:
算数运算符(+,-,*,/,%(求余数),++,--);注意:int a = 3/2; (a的值为1,因为a为整型,去除小数点后面的数字);i++,赋值之后自加;++i自加之后赋值。
关系运算符(>,<,>=,<=,==,!=);注意:运算出的结果使用boolean值接收。
布尔逻辑运算符(!,&,|,^异或,&&短路与,||短路或);注意:与有一个假就是假,或有一个真就是真。短路与/或,先判断第一个条件,如能判断结果就不进行第二个条件的计算。短路与或一般较省资源。异或:同真同假为假,否则为真。
位运算符(&,|,^,~,>>,<<,>>>);(使用很少)
赋值运算符(=,扩展赋值运算符:+=,-=,*=,/=);注意:i += 5;即 i = i + 5;
字符串连接运算符(+)。
Another conditional operator is ?:
, which can be thought of as shorthand for anif-then-else
statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as theternary operator because it uses three operands. In the following example, this operator should be read as: "IfsomeCondition
is true
, assign the value of value1
toresult
. Otherwise, assign the value of value2
to result
."
The following program, ConditionalDemo2
, tests the ?:
operator:
class ConditionalDemo2 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
System.out.println(result);
}
}
Because someCondition
is true, this program prints "1" to the screen. Use the?:
operator instead of an if-then-else
statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).
3.什么是表达式
int a = 5;a+5为表达式; 表达式的值为10;
int b = a + 5;
boolean c = a > 10;
a>10为表达式;表达式的值为false;
表达式的类型,是表达式值的类型。
注意:专业术语要多用多想。
八、分支语句
1.程序运行流程的分类
1.顺序结构 一系列的语句按顺序执行下去
2.分支结构 语句一——>判断语句——分支
3.循环结构
2.if…else…分支结构
if(布尔类型表达式){
语句一;
语句二;
……
}
else if(布尔类型表达式){
语句一;
语句二;
……}
else {
语句一;
语句二;
……}
3.switch分支结构
switch(表达式){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;……
case 常量N:
语句n;
break;[defult:
默认语句;
break;]
}
表达式的类型只能是:char byte short int
java 1.6(包括)以前,只是支持等价成int 基本类型的数据:byte ,short,char,int(其他的都不可以)。1.7加入的新特性可以支持String类型的数据。
九、练习二(练习if…else…分支结构)
if…else…是编程中使用最多的分支结构。
Test01.java
public class Test01{
public static void main(String[] args){
int score = 90;
if(score >= 90 && score <= 100){
System.out.println("the score is A!");
}
else if(score >= 80 && score < 90){
System.out.println("the score is B");
}
else if(score >= 70 && score < 80){
System.out.println("the score is C");
}
else if(score >= 60 && score < 70){
System.out.println("the score is D");
}
else if (score >= 0 && score < 60){
System.out.println("the score is F!");
}
else {
System.out.println("the score is wrong!");
}
}
}
用上switch结构:
public class Test01{
public static void main(String[] args){
int score = 90;
if(score >= 0 && score <= 100){
switch(score/10){
case 10:
System.out.println("the score is A!");
break;
case 9:
System.out.println("the score is A!");
break;
case 8:
System.out.println("the score is B!");
break;
case 7:
System.out.println("the score is C!");
break;
case 6:
System.out.println("the score is D!");
break;
default:
System.out.println("the score is F!");
break;
}
}
else {
System.out.println("the score is Wrong!");
}
}
}
Test02.java
public class Test02{
public static void main(String[] args){
char player1 = '锤';
char player2 = '剪';
if(player1 == '布'){
if(player2 == '锤'){
System.out.println("player1 win!");
}
else if(player2 == '剪'){
System.out.println("player2 win!");
}
else if(player2 == '布'){
System.out.println("play even!");
}
}
else if(player1 == '剪'){
if(player2 == '锤'){
System.out.println("player2 win!");
}
else if(player2 == '剪'){
System.out.println("play even!");
}
else if(player2 == '布'){
System.out.println("player1 win!");
}
}
else if(player1 == '锤'){
if(player2 == '锤'){
System.out.println("play even!");
}
else if(player2 == '剪'){
System.out.println("player1 win!");
}
else if(player2 == '布'){
System.out.println("player2 win!");
}
}
}
}