第3章选择语句

3.1比较运算符

  • 大于 >
  • 大于等于 >=
  • 小于 <
  • 小于等于 <=
  • 等于 ==
  • 不等于 !=

3.2逻辑运算符

  • 单与 &
  • 双与 &&:有假则假 同真则真
  • 或 |
  • 双或 ||:有真则真 同假则假
  • 非 !:取反
  • 异或 ^:相同则假,不同则真。

3.3 if语句

  • 单if
if(条件){
		如果条件为真时,执行的语句 B
	}
  • if-else分支
... A
	if(条件){
		如果条件为真时,执行的语句 B
	}else{
		如果条件为假时,执行的语句 C
	}
	... D
  • 多if-else嵌套
... A
	if(条件1){
		条件1为真... B
		if(条件2){
			条件2为真... B1
		}else{
			条件2为假... B2
		}
		... B3
	}else{
		条件1为假... C
	}
	... D
  • 多if-else分支
...A
	if(条件1){ > < >= <= == !=
		条件1为真...B
	}else if(条件2){
		条件2为真...C
	}else if(条件3){
		条件3为真...D
	}else{
		... E
	}
	...F
  • 示例:计算身体质量指数
    在这里插入图片描述
import java.util.Scanner;

public class Test1 {
	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("偏瘦");
		}
	}
}
  • 示例:判断闰年
    在这里插入图片描述
import java.util.Scanner;


public class Test2 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取用户输入的年份
		System.out.print("Enter the year:");
		int year=scanner.nextInt();
		//2.将年份进行判断 year%4==0&&year%100!=0 || year%400==0
		if( year%4==0&&year%100!=0 || year%400==0){
			System.out.println(year+"是闰年");
		}else{
			System.out.println(year+"是平年");
		}
	}
}

  • 示例:彩票
    在这里插入图片描述
import java.util.Scanner;


public class Test3 {
	public static void main(String[] args) {
    	//1.用户输入一个数字(要么一位数,要么两位数)
		int randomNumber=(int) (Math.random()*90+10);
		System.out.println("程序产生:"+randomNumber);
		Scanner scanner=new Scanner(System.in);
		
		System.out.print("输入一个数字n,n∈[0,100):");
		int number=scanner.nextInt();
		//2.程序中随机产生一个数字(要么一位数,要么两位数)
			//Math.random()生成一个0~1之间的任意小数[0,1)
			//[0,1) [0,10) [0,100)
		//假设17 a=7 b=1  29 a=9 b=2
		int a1=number%10;//7
		number/=10;//1
		int b1=number%10;//1
		int a2=randomNumber%10;//9
		randomNumber/=10;//2
		int b2=randomNumber%10;//2
		//一等奖 a1=a2 && b1==b2 19 19
		if(a1==a2&&b1==b2){
			System.out.println("一等奖!");
		//二等奖 a1==b2 && a2==b1 19 91
		}else if(a1==b2&&a2==b1){
			System.out.println("二等奖!");
		//三等奖 a1==a2||a1==b2||b1==a2||b1==b2 12 24
		}else if(a1==a2||a1==b2||b1==a2||b1==b2){
			System.out.println("三等奖!");
		}else{
			System.out.println("谢谢参与!");
		}
	}
}

3.4 switch语句

  • switch
  • 不带case的switch

3.5条件表达式

  • 布尔表达式

3.6常见错误与陷阱

  • 忘记必要的括号:如果块中只有一条语句, 就可以忽略花括号。 但是, 当需要用花括号将多条语句括在一起时, 忘记花括号是一个常见的程序设计错误。 如果通过在没有花括号的 if 语句中添加一条新语句来修改代码, 就必须插入花括号。
  • 在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为 true, 再对 p2 求值; 如果 p1 为 false, 就不再对 P2 求值。 当对 p1ll 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.除开賦值操作符的所有二元操作符都是左结合的, 陚值操作符是右结合的。

编程练习

在这里插入图片描述

import java.util.Scanner;
public class Demo3_1 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取系数 a b c
		System.out.print("Enter a,b,c:");
		double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
		//2.根据delt=b^2-4ac的值 进行判断
		double delt=b*b-4*a*c;
		if(delt>0){
			double x1=(-b+Math.sqrt(delt))/(2*a);
			double x2=(-b-Math.sqrt(delt))/(2*a);
			System.out.print("The equation has two roots "+x1+" and "+x2);
		}else if(delt==0){
			double x1=-b/(2*a);
			System.out.println("The equation has one root "+x1);
		}else{
			System.out.println("The equation has no real roots");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;

public class Demo3_2 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入系数和常数项
		System.out.print("Enter a,b,c,d,e,f:");
		double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
		double d=scanner.nextDouble();
		double e=scanner.nextDouble();
		double f=scanner.nextDouble();
		//2.判断该方程组是否有解
		double delt=a*d-b*c;
		if(delt!=0){
			double x=(e*d-b*f)/delt;
			double y=(a*f-e*c)/delt;
			System.out.println("x is "+x+" , and y is "+y);
		}else{
			System.out.println("has no solution");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;

public class Demo3_3 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取今天的周几
		System.out.print("Enter today's day:");
		int today=scanner.nextInt();
		//2.获取几天后
		System.out.print("Enter the number of days elasped since today:");
		int since=scanner.nextInt();
		switch (today%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;
		}
		//3.输出今天周几 几天后周几
		switch((today+since)%7){
			case 1:
				System.out.print("the future day is Monday ");
				break;
			case 2:
				System.out.print("the future day is Tuesday ");
				break;
			case 3:
				System.out.print("the future day is Wednesday ");
				break;
			case 4:
				System.out.print("the future day is Thursday ");
				break;
			case 5:
				System.out.print("the future day is Friday ");
				break;
			case 6:
				System.out.print("the future day is Saturday ");
				break;
			case 0:
				System.out.print("the future day is Sunday ");
				break;
		}
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo3_4 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取用户输入的前九位编号
		System.out.print("Enter the first 9 digits of an ISBN as integer:");
		int digit=scanner.nextInt();
		int digitOri=digit;
		//2.将九位编号分别取出
		System.out.println(digit);
		int d9=digit%10;
		digit/=10;
		int d8=digit%10;
		digit/=10;
		int d7=digit%10;
		digit/=10;
		int d6=digit%10;
		digit/=10;
		int d5=digit%10;
		digit/=10;
		int d4=digit%10;
		digit/=10;
		int d3=digit%10;
		digit/=10;
		int d2=digit%10;
		digit/=10;
		int d1=digit%10;
		//3.根据取出的前九位数字 计算第十位
		int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
		System.out.println(d10);
		//4.拼接ISBN 输出
		String res="";
		if(d1==0){
			res+=0;//"0"
		}
		if(d10==10){
			res=res+digitOri+"X";
//			System.out.println(digitOri+"X");//+ 字符串连接符
		}else{
			res=res+digitOri+d10;
		}
		System.out.println(res);
	}
}

在这里插入图片描述

import java.util.Scanner;

public class lian3_5 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a number:");
		int number=scanner.nextInt();
		int a=number%10;
		int b=number/100;
		if(a==b){
			System.out.print("是回文数字");
		}else{
			System.out.print("不是回文数字");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;
public class lian3_6 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		//用户输入
		System.out.print("Enter a number(0-2):");
		int p=scanner.nextInt();
		//电脑随机生成
		int c=(int)(Math.random()*3);
		System.out.println("电脑随机生成:"+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{
			pStr="paper";
		}
		if(c==0){
			cStr="scissor";
		}else if(c==1){
			cStr="rock";
		}else{
			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!");
				break;
		}
    }
}

在这里插入图片描述

import java.util.Scanner;

public class lian3_7 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("输入三角形三边:");
		double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
		double L=a+b+c;
		if(a+b>c&&a+c>b&&b+c>a){
			System.out.println("The length of triangle is "+L);
		}else{
			System.out.println("The length of triangle is Error");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;

public class lian3_8 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter year:");
		int year=scanner.nextInt();
		System.out.print("Enter month:");
		int month=scanner.nextInt();
		System.out.print("Enter the day of the year:");
		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;
		System.out.println("Day of week is "+h);
	}
}

在这里插入图片描述

import java.util.Scanner;


public class lian3_9 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a point  with tow coordinates(X,Y):");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		
		//double d=Math.sqrt(x*x+y*y);
		double d=Math.pow(x*x+y*y, 0.5);
		if(d<=10){
			System.out.print("Point ("+x+","+y+") is in the circle!");
		}else{
			System.out.print("Point ("+x+","+y+") is no in the circle!");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;
public class lian3_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<5&y<2.5){
			System.out.println("Point ("+x+","+y+") is in the rectangle");
		}else{
			System.out.println("Point ("+x+","+y+") is no in the rectangle");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;

public class lian3_11 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		//1.随机生成数字[1-13]和花色[1-4]
		int num=(int)(Math.random()*13+1);
		int color=(int)(Math.random()*4+1);
		//2.对牌大小
		switch(num){
			case 1:
				System.out.print("The card you picked is Ace ");
				break;
			case 11:
				System.out.print("The card you picked is Jack ");
				break;
			case 12:
				System.out.print("The card you picked is Queen ");
				break;
			case 13:
				System.out.print("The card you picked is King ");
				break;
			default:
				System.out.print("The card you picked is "+num);
				break;
		}
		//3.对花色进行选择
		switch(color){
			case 1:
				String str1="Clubs";
				System.out.print(" of "+ str1);
				break;
			case 2:
				String str2="Diamonds";
				System.out.print(" of "+ str2);
				break;
			case 3:
				String str3="Hearts";
				System.out.print(" of "+ str3);
				break;
			case 4:
				String str4="Spades";
				System.out.print(" of "+ str4);
				break;
		}	
	}
}

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;


public class lian3_13 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a point:");
		//输入坐标点
		double x1=scanner.nextDouble();
		double y1=scanner.nextDouble();
		//斜边
		double y=-0.5*x1+100;
		
		if(x1>=0&&x1<=200&&y1<=y){
			System.out.println("The point is in the triangle!");
		}else{
			System.out.println("The point is not in the triangle!");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;


public class lian3_14 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		//1.输入两个矩形的中心坐标和长 宽
		System.out.println("Enter the center coordinates, length and width of the first rectangle:");
		double x1=scanner.nextDouble();
		double y1=scanner.nextDouble();
		double L1=scanner.nextDouble();
		double W1=scanner.nextDouble();
		System.out.println("Enter the center coordinates, length and width of the second rectangle:");
		double x2=scanner.nextDouble();
		double y2=scanner.nextDouble();
		double L2=scanner.nextDouble();
		double W2=scanner.nextDouble();
		//2.计算是否r2在r1内
		if(x2<=(L1/2-L2/2+x1)&&x2>=(x1-L1/2+L2/2)&&y2<=(W1/2-W2/2+y1)&&y2>=(y1-W1/2+W2/2)){
			System.out.println("R2 in R1");
			//计算r2是否在r1外
		}else if(x2>=(L1/2+L2/2+x1)&&x2<=(x1-L1/2-L2/2)&&y2>=(W1/2+W2/2+y1)&&y2<=(y1-W1/2-W2/2)){
			System.out.println("R2 out R1");
		}else{
			System.out.println("R2 overlaps R1");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;


public class lian3_15 {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		//1.输入两个圆的中心坐标和半径
		System.out.println("Enter the Circle1 and R1:");
		double x1=scanner.nextDouble();
		double y1=scanner.nextDouble();
		double r1=scanner.nextDouble();
		
		System.out.println("Enter the Circle2 and R2:");
		double x2=scanner.nextDouble();
		double y2=scanner.nextDouble();
		double r2=scanner.nextDouble();
		//2.计算圆心距
		double d=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		//double d=Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
		//3.判断两个圆之间的关系 
		if(d<=Math.abs(r1-r2)){
			System.out.println("Cricle2 is inside Cricle1");
		}else if(d<=(r1+r2)){
			System.out.println("Cricle2 is out Cricle1");
		}else{
			System.out.println("Cricle2 does not overlaps Cricle1");
		}
	}
}

在这里插入图片描述

import java.util.Scanner;


public class lian3_16 {
	public static void main(String[] args){
		//1.输入三个点的坐标p0,p1,p2
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter 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();
		//2.计算
		double n=(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
		if(n>0){
			System.out.println("左侧");
		}else if(n<0){
			System.out.println("右侧");
		}else{
			System.out.println("线上");
		}
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值