JavaDay_03

JavaDay_03

知识框架

  • 比较运算符、逻辑运算符及boolean数据类型
  • 选择:if与switch
  • 常见的错误和陷阱
  • 示例学习:计算身体质量指数
  • 示例学习:判断闰年和模拟彩票
  • 简单Java程序

3.1 比较运算符、逻辑运算符及boolean数据类型

3.1.1 比较运算符
大于(>)、大于等于(>=)、小于(<)、小于等于(<=)、等于(==)、不等于(!=)
比较运算符的结果一般为boolean
3.1.2 逻辑运算符
主要计算Boolean值之间的逻辑
主要有双与(&&有假则假,同真为真)、双或(||有真则真,同假则假)、非(!取反)、单与、单或、异或。
双与和单与,双或和单或的区别

public class Operation {
	public static void main(String[] args) {
		System.out.println(3>2&&1<2);//true
		System.out.println(1>3||2<6);//true
		System.out.println(1>3^2<6);//异或 同为假 不同为真
		//单与和双与的区别 单或和双或
		int i=0;
		int j=0;
		System.out.println(i++>0&&j++>0);//false
		System.out.println(i);//1
		System.out.println(j);//0
		//双与 && 如果第一个条件为假,则后面的条件无论真假结果都是假,所以后面的条件不运行
		//单与 &  全部执行 再将结果进行逻辑运算
		//双或 || 如果第一个条件为真 则后面的条件无论真假结果都是真 所以 后面的条件不运行
		//单或 |  全部执行 再将结果进行逻辑运算
		boolean b=true;//false 布尔值不能够与数字进行计算 true+1
		//System.out.println(b+1);
		//The operator + is undefined for the argument type(s) boolean, int
	}
}
程序结果:
true
true
true
false
1
0

3.1.3 boolean数据类型
boolean数据类型声明一个具有值true或者false的变量
具有布尔值的变量称为布尔变量(boolean variable), boolean 数据类型用于声明布尔型变量。boolean 型变量可以是以下这两个值中的一个:true 和 false。例如,下述语句将true赋值给变量lightOn:
boolean lightOn=true;
true 和 false 都是直接量,就像 10 这样的数字。它们被当作保留字一样,不能用做程序中的标识符。

3.2 选择:if与switch

3.2.1 if语句
if 语句是一个结构,允许程序确定执行的路径。
Java 有几种类型的选择语句:单分支 if 语句、双分支 if-else 语句、嵌套 if 语句、多分支 if-else 语句、switch 语句和条件表达式。 单分支 if 语句是指当且仅当条件为 true 时执行一个动作。单分支 if 语句的语法如下:
if (布尔表达式){
语句(组);
}
若花括号内只有一条语句,则可以省略花括号。但是,省略括号可以让代码更加简短,但是容易产生错误。当你返回去修改略去代码的时候,容易忘记加上括号。这是一个常犯的错误。
提示用户输入一个整数,如果该数字是5的倍数,打印HiFive。如果该数字能被2整除,打印HiEven。

import java.util.Scanner;
public class Simple {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter a integer:");
		int number=sc.nextInt();
		if(number%5==0){
			System.out.println("HiFive");
		}
		if(number%2==0){
			System.out.println("HiEven");
		}
	}
}

3.2.2 双分支if-else语句
if-else 语句根据条件是真或者是假,决定执行的路径。
使用双分支 if 语句。根据条件为 true 或 false, 双分支 if 语句可以指定不同的操作。下面是双分支 if-else 语句的语法:
if (布尔表达式>{
布尔表达式为其时执行的语句(组);
} else{
布尔表达式为假时执行的语句(组);
}
3.2.3 嵌套的if语句和多分支if-else语句
if语句可以在另外一个 if语句中,形成嵌套的 if语句。嵌套的 if语句可用于实现多重选择。
具体流程图见下图:
在这里插入图片描述
3.2.4 switch语句
switch 语句基于变量或者表达式的值来执行语句。有效地处理多重条件的问题。以下是 switch 语句的完整语法:
switch(switch 表达式){
case 值1:
语句(组)1;
break;
case 值 2:
语句(组)2;
break;
case 值 N:
语句(组)N
break;
default:
R认情况下执行的语句(组)
}
switch 语句遵从下述规则: ① switch 表达式必须能计算出一个 char、byte、short、int或者 String 型值,并且必须总是要用括号括住。
② value1,. . . ,valueN 必须与 switch 表达式的值具有相同的数据类型。注意: value1, valueN 都是常量表达式,也就是说这里的表达式是不能包含变量的,例如,不允许出现 1+X。
③ 当 switch 表达式的值与 case 语句的值相匹配时,执行从该 case开始的语句,直到遇到一个 break 语句或到达该switch 语句的结束。
④ 默认情况(default)是可选的,当没有一个给出的 case 与 switch 表达式匹配时, 用来执行该操作。
⑤ 关键字 break 是可选的。break 语句会立即终止 switch 语句。
不要忘记在需要的时候使用 break 语句。一旦匹配其中一个 case, 就从匹配的case 处开始执行,直到遇到 break 语句或到达 switch 语句的结束。这种现象称为落空行为。
求解一个一元二次方程的根,判断根的个数。

//主要用多if-else分支
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");
		}
	}
}

判断某一元二次方程是否有解
主要用if-else求解

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");
		}
	}
}

获取今天的星期数,以及未来某天的星期数,主要用switch语句

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;
		}
	}
}

3.3 常见的错误和陷进

忘记必要的括号: if语句块中只有一条语句,就可以忽略花括号。但是,当需要用花括号将多条语句括在一 起时,忘记花括号是一个常见的程序设计错误。如果通过在没有花括号的if语句中添加一 条新语句来修改代码,就必须插入花括号。
**在if 行出现错误的分号:**这个错误是很难发现的,因为它既不是编译错误也不是运行错误,而是一个逻辑错误。
对布尔值的冗余测试
悬空 else 出现的歧义
**两个浮点数值的相等测试:**浮点数具有有限的计算精度,涉及浮点数的计算可能引人取整错误。因此,两个浮点数值的相等测试并不可靠。
避免不同情形中的重复代码

示例学习:计算身体质量指数

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("偏瘦");
		}
	}
}

3.5 示例学习:判断闰年和模拟彩票

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) {
		int randomNumber=(int) (Math.random()*100);
		System.out.println("程序产生:"+randomNumber);
		Scanner scanner=new Scanner(System.in);
		//1.用户输入一个数字(要么一位数,要么两位数)
		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.6 简单Java程序

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.");
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值