Javase day03

第三章 选择语句

3.1 比较运算符

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

3.2 逻辑运算符

  • 单于&
  • 双与&&
  • 或 |
  • 双或 ||
  • 非!
  • 异或^

3.3 if 语句

  • 单if:是指当且仅当条件为true时执行的一个动作。

if(布尔表达式){
语句组;
}

  • if-else分支:根据条件是真或者是假,决定执行的路径。

    if(布尔表达式){
    布尔表达式为真时执行的语句(组);
    }else{
    布尔表达式为假时执行的语句(组);
    }

  • 多if-else嵌套:if语句可以在另一个if语句中,形成嵌套的if语句

    if(i<k){
    if(j>k){
    System.out.print("i ")
    }else{
    System.out.print("j ")
    }
    }

  • 多if-else分支:
    Javase day03

  • 示例
    Javase day03

    import java.util.Scanner;

    public class Test1 {
    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%40&&year%100!=0 || year%4000
    if( year%40&&year%100!=0 || year%4000){
    System.out.println(year+“是闰年”);
    }else{
    System.out.println(year+“是平年”);
    }
    }
    }

3.4 switch语句

  • 选择语句switch

    switch(变量){
    case 选项1:
    如果变量=选项1执行的语句
    break;
    case 选项2:
    如果变量=选项2执行的语句
    break;

    default:
    如果选项n中没有适合变量的值,则执行
    break;
    }

if与switch的区别:

  • if可以对区间值进行比较 也可以对固定值进行比较
  • switch只能对固定的一个值进行比较 推荐

3.5常见错误和陷阱

  • 常见错误1:忘记必要的括号,括号要成对出现,提前输出两个括号,以便写到后面就忘了
  • 常见错误2:在if行出现错误的分号,既不是编译错误也不是运行错误但是逻辑错误,可能提前结束一个局部代码块。
  • 常见错误3:对布尔值的冗余计算,当我们提前定义了布尔值的真假,在if里的条件时不需要再赋值一次,直接填写变量即可。
  • 常见错误4:悬空else出现歧义,这里主要是因为缩进问题,一个if相对应他的else用缩进对齐的方式,方便阅读和查阅,以免造成歧义。
  • 常见错误5:两个浮点值的相等测试

小结

1.boolean 类型变量可以存储值 true 或 false。
2.关系操作符(<、 <= 、 ==、 ! =、 >、 >=) 产生一个布尔值。
3.选择语句用于可选择的动作路径的编程。 选择语句有以下几种类型: 单分支 if 语句、 双分支 ifelse 语句、 嵌套 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.括号用于强制求值的顺序以任何顺序进行。

课后代码示例

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;
//Cramer方程组
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 Demo3_5 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入一个三位数字
		System.out.print("Enter a number:");
		int number=scanner.nextInt();
		//2.将三位数进行拆分
		int a=number%10;//123
		int b=number/100;//
		//3.对比个位与百位
		if(a==b){
			System.out.println("是回文数字");
		}else{
			System.out.println("不是!");
		}
	}
}
-------------------------------------------------------------
import java.util.Scanner;
//石头剪刀布
public class Demo3_6 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入玩家的选择
		System.out.print("Enter :");
		int p=scanner.nextInt();
		//2.随机生成选择
		int c=(int) (Math.random()*3);//[0,1)*3 [0,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;
		}
	}
}
/*
 * 0 1 2
 * 0 1 2
 * 
 * P C
 * 0 0 平0 0+3%3
 * 0 1 输1 1+3%3
 * 0 2 赢2 2+3%3
 * 
 * 1 0 赢2 1+1%3
 * 1 1 平0 2+1%3
 * 1 2 输1 3+1%3
 * 
 * 2 0 输1 2+2%3
 * 2 1 赢2 3+2%3
 * 2 2 平0 4+2%3
 * */--------------------------------------------------------------------
 
import java.util.Scanner;


public class Demo3_7 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入三角形三边
		System.out.print("Enter the three side of a triangle:");
		double side1=scanner.nextDouble();
		double side2=scanner.nextDouble();
		double side3=scanner.nextDouble();
		//2.依次判断每个边的合法性
		if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1){
			//3.如果合法 计算周长
			double l=side1+side2+side3;
			System.out.println("The length of triangle is "+l);
		}else{
			System.out.println("The side is Illegal");
		}
	}
}
-----------------------------------------------------------
import java.util.Scanner;
public class Demo3_8 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取 年份 月 日
		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 month:");
		int day=scanner.nextInt();
		//隐含计算 j 世纪数 k该世纪的第几年
		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(h);
	}
}
------------------------------------------------------
import java.util.Scanner;


public class Demo3_9 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.获取坐标点 x y
		System.out.print("Enter a point with two coordinates:");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		//2.求坐标点到圆心的距离
		double deltX=x-0;
		double deltY=y-0;
		double distance=Math.sqrt(deltX*deltX+deltY*deltY);
		//3.将距离和半径进行个比较
		if(distance>10){
			System.out.println("Point("+x+","+y+") is not in the circle.");
		}else{
			System.out.println("Point("+x+","+y+") is in the circle.");
		}
	}
}-----------------------------------------------------------------------------
import java.util.Scanner;


public class Demo3_10 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入坐标点
		System.out.print("Enter a point:");
		double x=Math.abs(scanner.nextDouble());
		double y=Math.abs(scanner.nextDouble());
		//2.判断该坐标点x的量和y的量
		if(x>5||y>2.5){
			System.out.println("矩形外");
		}else{
			System.out.println("矩形内");
		}
	}
}