第三章 选择语句
3.1 比较运算符
- 大于 >
- 大于等于 >=
- 小于 <
- 小于等于 <=
- 等于 ==
- 不等于 !=
比较运算符结果是布尔值
3.2逻辑运算符
- 单与 & 全部执行 再将结果进行逻辑运算
- 双与 && 有假则假 同真则真 如果第一个条件为假 则后面的条件无论真假结果都是假 所以后面的条件不运行
-单或 | 全部执行 再将结果进行逻辑运算 - 双或 || 有真则真 同假则假 如果第一个条件为真 则后面的条件无论真假 结果都是真 所以后面的条件不运行
- 非 !
- 异或 ^
逻辑运算符主要计算布尔值之间的逻辑
3.3 if 语句
-
单if
...A if(条件){ 如果条件为真时,执行语句B } ...C
条件为真时: A->B->C
条件为假时: A->C
-
if-else分支
...A if(条件){ 如果条件为真时,执行的语句B }else{ 如果条件为假时,执行的语句C } ...D
条件为真时:A->B->D
条件为假时: A->C->D
-
多if-else嵌套
...A if(条件2){ 条件2为真...B1 }else{ 条件2为假...B2 } ...B3 }else{ 条件1为假...C } ...D
条件1为真且条件2为真: A->B->B1->B3->D
条件1为真且条件2为假: A->B->B2->B3->D
条件1为假 A->C->D
-
多if-else分支
...A if(条件1){ > < >= <= == != 条件1为真...B }else if(条件2){ 条件2为真...C }else if(条件3){ 条件3为真...D }else{ ... E } ...F
条件1为真:A->B->F
条件1为假,条件2位真:A->C->F
条件1为假,条件2位假,条件3为真:A->D->F
三者都为假:A->E->F
- 选择语句 switch
switch(变量){
case 选项1:
如果变量=选项1执行的语句
break;
case 选项2:
如果变量=选项2执行的语句
break;
...
default:
如果选项n中没有适合变量的值,则执行
break;
}
if与switch的区别:
if可以对区间值进行比较 也可以对固定值进行比较
switch只能对固定的一个值进行比较 推荐使用
-
示例:计算身体质量指数
代码如下:public static void main(String[] args){ Scanner scanner=new Scanner(System.in); //1.获取身高体重 System.out.print("Please enter your weight and hight:"); double weight=scanner.nextDouble; double hight=scanner.nextDouble; //2.BMI=体重/身高^2 double BMI=weight/hight/hight; //3.将BMI进行判断,输出相应的说明 if(BMI>=30.0){ System.out.println("过胖"); }else if(BMI>=25.0){ System.out.println("超重"); }else if(BMI>=18.5){ System.out.println("正常"); }else{ System.out.println("偏瘦"); } } }
3.4 switch语句
- switch
- 不带case的switch
3.5 条件表达式
- 布尔表达式(条件表达式)(三目运算)
数据类型 变量名=布尔表达式?常量值1:常量值2;
3.6 常见的错误和陷进
- 忘记必要的括号
- 在if行出现错误的分号
- 对布尔值的冗余测试
- 悬空else出现的歧义
- 两个浮点数值的相等测试
- 简化布尔变量赋值
- 避免不同情形中的重复代码
本章小结
1、Boolean类型变量可以存储值true或false.
2、关系操作符(<,<=,==,!=,>,>=)产生一个布尔值.
3、选择语句用于可选择的动作路径的编程。选择语句有一下几种类型:单分支if语句、双分支if-else语句、嵌套if语句、多分支if-else语句、switch语句和条件表达式.
4、各种if语句都是基于布尔值表达式来控制决定的。根据表达式的值是true或false,这些语句选择两种可能路径的一种.
5、布尔操作符&&、||、!和^对布尔值和布尔变量进行计算.
6、当对p1&&p2求值时,Java先求p1的值,如果p1为ture,再对p2求值;如果p1为false,就不再对p2求值。当对p1||p2求值时,Java先求p1的值,如果p1为false,再对p2求值;如果p1为true,就不再对p2求值。因此,&&也称为条件与操作符或短路与操作符,而||也称为条件或操作符或短路或操作符.
7、switch语句根据char,byte,short,int或者String类型的switch表达式来进行控制决定.
8、在switch语句中,关键字break是可选的,但它通常用在每个分支的结尾,以中止执行switch语句的剩余部分。如果没有出现break语句,则执行接下来的case语句.
9、表达式中的操作符按照括号、操作符优先级以及操作符结合规则所切丁的次序进行求值.
10、括号用于强制求值的顺序以任何顺序进行.
11、具有更高级优先权的操作符更早地进行操作。对于同样优先级的操作符,它们的结合规则确定操作的顺序.
12、除开赋值操作符的所有二元操作符都是左结合的,赋值操作符是右结合的.
习题
示例:
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a year:");
int year=scanner.nextInt();
if((year%4==0&&year%100!=0)||(year%400==0)){
System.out.println("闰年");
}else{
System.out.println("不是闰年");
}
}
3.1
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a,b,c:");
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
double R=Math.pow(b*b-4*a*c, 0.5);
if(R>0){
double r1=(-b+R)/2*a;
double r2=(-b-R)/2*a;
System.out.println("The equation has two roots "+r1+" and "+r2);
}else if(R==0){
double r=(-b+R)/2*a;
System.out.println("The equation has one root "+r);
}else{
System.out.println("The equation has no roots");
}
}
3.3
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter today's day:");
int day=scanner.nextInt();
System.out.print("Enter the number of days elapsed since today:");
int num=scanner.nextInt();
switch(day%7){
case 1:
System.out.print("Today is Monday ");
break;
case 2:
System.out.print("Today is Tuesday ");
break;
case 3:
System.out.print("Today is Wednesday ");
break;
case 4:
System.out.print("Today is Thursday ");
break;
case 5:
System.out.print("Today is Friday ");
break;
case 6:
System.out.print("Today is Saturday ");
break;
case 0:
System.out.print("Today is Sunday ");
break;
}
switch(day+num%7){
case 1:
System.out.print("and the future day is Monday");
break;
case 2:
System.out.print("and the future day is Tuesday ");
break;
case 3:
System.out.print("and the future day is Wednesday ");
break;
case 4:
System.out.print("and the future day is Thursday ");
break;
case 5:
System.out.print("and the future day is Friday ");
break;
case 6:
System.out.print("and the future day is Saturday ");
break;
case 0:
System.out.print("and the future day is Monday ");
break;
}
}
3.4
代码:
public static void main(String[] args){
//获取输入的九个数字
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as integer:");
int number=scanner.nextInt();
int numberOri=number;
//取出9位编号
int d9=number%10;
number/=10;
int d8=number%10;
number/=10;
int d7=number%10;
number/=10;
int d6=number%10;
number/=10;
int d5=number%10;
number/=10;
int d4=number%10;
number/=10;
int d3=number%10;
number/=10;
int d2=number%10;
number/=10;
int d1=number%10;
int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
//计算第十位
String res="";
if(d1==0){
res+=0;
}
if(d10==10){
res=res+numberOri+"x";
}else{
res=res+numberOri+d10;
}
System.out.println(res);
//拼接ISBN
}
3.5
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//获取用户输入的 三位数字
System.out.print("Enter a three-digit integer:");
int number=scanner.nextInt();
//将三位数进行拆分
int a=number%10;
int b=number/100;
if(a==b){
System.out.print(number+" is a palindrome");
}else{
System.out.print(number+" is not a palindrome");
}
}
3.6
代码:
public static void main(String[] args){
//提示用户输入0,1, 2
Scanner scanner=new Scanner(System.in);
System.out.print("scissor(0),rock(1),paper(2):");
int p=scanner.nextInt();
//计算机随机产生随机数
int c=(int) (Math.random()*3);
//判断输赢 N 0 A
System.out.println("The computer is"+ c);
int result=-1;
String cStr="";
String pStr="";
if(p==0){
result=(p+c+3)%3;
}
if(p==1){
result=(p+c+1)%3;
}
if(p==2){
result=(p+c+2)%3;
}
if(p==0){
pStr="scissor";
}else if(p==1){
pStr="rock";
}else if(p==2){
pStr="paper";
}
if(c==0){
cStr="scissor";
}else if(c==1){
cStr="rock";
}else if(c==2){
cStr="paper";
}
System.out.print("The computer is "+cStr+". You are "+pStr+".");
switch(result){
case 0:
System.out.println(" too!It is draw");
break;
case 1:
System.out.println("You lose.");
break;
case 2:
System.out.println("You won.");
}
}
3.7
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入边长数a b c
System.out.print("Enter the a, b, c:");
int a=scanner.nextInt();
int b=scanner.nextInt();
int c=scanner.nextInt();
//判断 两条边的长度大于第三边a+b>c
if(a+b>c&&a+c>b&&b+c>a){
System.out.print("三角形的周长是:"+(a+b+c));
}else{
System.out.println("输入值不合法!");
}
}
3.8
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//提示用户输入年月日
System.out.print("Enter year (e.g.,2012):");
int year=scanner.nextInt();
System.out.print("Enter month (1~12):");
int month=scanner.nextInt();
System.out.print("Enter day of the month (1~31):");
int day=scanner.nextInt();
if(month==1||month==2){
month+=12;
year--;
}
int j=year/100;
int k=year%100;
int h=(day+26*(month+1)/10+k+k/4+j/4+5*j)%7;
String str="Day of the week is ";
switch(h){
case 0:
System.out.println(str+"Saturday");
break;
case 1:
System.out.println(str+"Sunday");
break;
case 2:
System.out.println(str+"Monday");
break;
case 3:
System.out.println(str+"Tuesday");
break;
case 4:
System.out.println(str+"Wednesday");
break;
case 5:
System.out.println(str+"Thursday");
break;
case 6:
System.out.println(str+"Friday");
break;
}
}
3.9
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//获取输入坐标
System.out.print("Enter a point with two coordinates:");
double x=scanner.nextDouble();
double y=scanner.nextDouble();
//计算坐标与原点距离
double d=Math.sqrt(Math.pow(x-0, 2)+Math.pow(y-0, 2));
//判断是否在圆内
if(d==10||d<10){
System.out.print("Point ("+x+","+y+") is in the circle");
}else{
System.out.print("Point ("+x+","+y+") is not in the circle");
}
}
3.10
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入坐标点
System.out.print("Enter a point with two coordinates:");
double x=Math.abs(scanner.nextDouble());
double y=Math.abs(scanner.nextDouble());
//计算与原点位置
//判断位置
if(x<=10/2&&y<=5/2){
System.out.print("Point ("+x+","+y+") is in the rectangle.");
}else{
System.out.print("Point ("+x+","+y+") is not in the rectangle.");
}
}
3.11
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//[1`13]产生随机数
int rank=(int) (1+Math.random()*13);
//[1~4]产生随机数
int suit=(int) (1+Math.random()*4);
System.out.print("The card you picked is "+rank+" of "+suit);
}
3.12
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入四个点
System.out.println("Enter x1, y1, x2, y2, x3, y3, x4, y4:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double x3=scanner.nextDouble();
double y3=scanner.nextDouble();
double x4=scanner.nextDouble();
double y4=scanner.nextDouble();
//计算交点
double a=(double) (y1-y2);
double b=(double) (x2-x1);
double c=(double) (y3-y4);
double d=(double) (x4-x3);
double e=(double) ((y1-y2)*x1-(x1-x2)*y1);
double f=(double) ((y3-y4)*x3-(x3-x4)*y3);
//判断
double delt=a*d-b*c;
if(delt==0){
System.out.print("The two lines are parallel.");
}else{
double x=(e*d-b*f)/delt;
double y=(a*f-e*c)/delt;
System.out.print("The intersecting point is at ("+x+","+y+").");
}
}
3.13
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入坐标
System.out.print("Enter a point's x- and y-coordinates:");
double x=scanner.nextDouble();
double y=scanner.nextDouble();
//
if(x>=0&&x<=200){
if((200-x)/(y-0)>=2){
System.out.print("The point is in the triangle");
return;//直接结束函数
}
System.out.print("The point is not in the triangle");
}
}
3.14
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter r1's center x-,y-coordinates,width,and height:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double h1=scanner.nextDouble();
double w1=scanner.nextDouble();
System.out.print("Enter r2's center x-,y-coordinates,width,and height:");
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double h2=scanner.nextDouble();
double w2=scanner.nextDouble();
//计算是否在内
if(x2<=(w1/2-w2/2+x1)&&x2>=(x1-w1/2+w2/2)&&y2<=(h1/2-h2/2+y1)&&y2>=(y1-h1/2+h2/2)){
System.out.println("r2 is inside r1");
}else if(x2>=(w1/2+w2/2+x1)||x2<=(x1-w1/2-w2/2)||y2>=(h1/2+h2/2+y1)||y2<=(y1-h1/2-h2/2)){
System.out.println("r2 out r1");
}else{
System.out.println("r2 overlaps r1");
}
}
3.15
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入半径以及圆心坐标
System.out.print("Enter cicle1's center x-,y-coordinates, and radius:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double r1=scanner.nextDouble();
System.out.print("Enter cicle2's center x-,y-coordinates, and radius:");
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double r2=scanner.nextDouble();
//计算圆心距离
double d=Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2));
//判断圆心与半径关系
if(d<=r1-r2){
System.out.print("cicrle2 is inside cicrle1");
}else if(d<=r1+r2){
System.out.print("cicrle2 is overlaps cicrle1");
}else{
System.out.print("cicrle2 does not overlaps cicrle1");
}
}
3.16
代码:
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//输入三个点坐标
System.out.print("Enter three points for p0, p1, p2:");
double x0=scanner.nextDouble();
double y0=scanner.nextDouble();
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double d=(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
//给出判断条件
if(d>0){
System.out.print("("+x0+","+y0+")"+" is on the left side of the line from ("+x1+","+y1+") to ("+x2+","+y2+")");
}else if(d==0){
System.out.print("("+x0+","+y0+")"+" is on the side of the line from ("+x1+","+y1+") to ("+x2+","+y2+")");
}else{
System.out.print("("+x0+","+y0+")"+" is on the right side of the line from ("+x1+","+y1+") to ("+x2+","+y2+")");
}
}