第三章 选择
3.1 boolean数据类型
boolean数据类型声明一个具有值true或false的变量。
3.2 if语句双分支if-else语句
if语句是一个结构,允许程序确定执行的路径。单if语句是指当且 仅当条件为true时执行动作。
3.3 双分支if-else语句
if-else是根据条件是真或假,决定执行的路径。
if(布尔表达式){
布尔表达式为真时执行的语句;
}
else {
布尔表达式为假时执行的语句;
}
3.4 嵌套的if语句和多分支if-else语句
if语句可以在另一个if语句中,形成嵌套的if语句。
if 或 if-else 语句中的语句可以是任意合法的 Java 语句,甚至可以是其他的 if 或ifelse语句。内层 if 语句称为是嵌套在外层 if 语句里的。内层 if 语句还可以包含其他的 if语句;事实上,对嵌套的深度没有限制。
3.5 常见的错误和陷阱
常见错误 1: 忘记必要的括号
常见错误 2: 在 if 行出现错误的分号
常见错误 3: 对布尔值的冗余测试
常见错误 4: 悬空 else 出现的歧义
常见错误 5: 两个浮点数值的相等测试
常见陷阱 1: 简化布尔变量賦值
常见陷阱 2: 避免不同情形中的重复代码
3.6 产生随机数
使用 Math.randomO 来获得一个 0.0 到 1.0 之间的随机 double 值,不包括 1.0。
如何产生[5,15)之间的随机数?
5+Math.random()10
如何产生[11,89]之间的随机数?
Math.random()79+11
0~77+11 11~88
如何产生[a,b)之间的随机数?
a+Math.random()(b-a)
[a,b]
a+Math.random()(b-a+1)
3.7 示例学习:计算身体质量指数
import java.until.Scanner;
public class ComputeAndInterpretBMI{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
//获取身高和体重
System.out.print("Enter weight in pounds");
double weight=scanner.nextDouble();
System.out.print("Enter height in pounds");
double height=scanner.nextDouble();
//BMI=体重/身高^2
double BMI=weight/height/height;
//将BMI进行判断,输出相应的说明
if(BMI>=30.0){
System.out.println("Obese");
}else if{BMI>=25){
System.out.println("Overweight");
}else if{BMI>=18.5){
System.out.println("Normal");
}else{
System.out.println('Underweight");
}
}
3.8 逻辑操作符
逻辑操作符!,&&,||和^可以用于产生复合布尔表达式。有时候,是否执行一条语句是由几个条件的组合来决定的。可以使用逻辑操作符组合这些条件。逻 辑 操 作 符( logical operator ) 也称为布尔操作符 ( boolean operator), 是对布尔值进行的运算,它会创建新的布尔值。
&& 双与 有假则假 同真则真
|| 双或 有真则真 同假则假
! 非 取反
& 单与
| 单或
^ 异或
3.10 示例学习:判断闰年
import java.until.Scanner;
public class ComputeAndInterpretBMI{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
//输入年份
System.out.print(“ Enter a year ”);
int year =scanner.nextInt();
//将年份进行判断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+"是平年”);
}
}
3.11 示例学习:彩票
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.12 switch 语句
switch语句基于变量或者表达式的值来执行语句。
switch 语句遵从下述规则:
① switch 表达式必须能计算出一个 char、byte,short,int 或者 String 型值,并且必须总是要用括号括住。(char 和 String 类型将在下一章介绍。)
② valuel,…,valueN 必须与switch 表 达 式 的 值 具 有 相 同 的 数 据 类 型。注 意:
valuel, valueN 都是常量表达式,也就是说这里的表达式是不能包含变量的,例如,不允许出现 1+X。
③ 当 switch 表达式的值与 case 语句的值相匹配时,执行从该 case 开始的语句,直到遇到一个 break 语句或到达该 switch 语句的结束。
④ 默认情况(default)是可选的,当没有一个给出的 case 与 switch 表达式匹配时,用来执行该操作。
⑤ 关键字 break 是可选的。break 语句会立即终止 switch 语句。
3.13 条件表达式
条件表达式基于一个条件计算表达式的值。有时可能需要给有特定条件限制的变量賦值。
3.14 操作符的优先级和结合规则
操作符的优先级和结合规则确定了操作符计算的顺序。
第四章 数学函数,字符和字符串
4.1 常用数学函数
Java 在 Math 类中提供了许多实用的方法,来计算常用的數学函数。这些方法分为三类:三角函数方法(trigonometric method)、指教函数方法(exponent method) 和服务方法(service method)。服务方法包括取整 、求最小值、求最大值、求绝对值和随机方法。除了这些方法之外Math 类还提供了两个很有用的 double 型常量,PI ( TT ) 和 E (自然对数的底)。可以在任意程序中用Math.PI 和Math.E 的形式来使用这两个常量。
4.1.1 三角函数方法
sin、cos 和 tan 的参数都是以弧度为单位的角度。asin 和 atan的返回值是-PI/2~PI/2的一个弧度值,acos 的返回值在 0到之间。1° 相当于 PI /180 弧度,90° 相当于PI /2 弧度,而 30° 相当于PI/6 弧度。
-例如:
Math.toDegrees(Math.PI / 2) 返 回 90.0
Hath.toRadians(30)返 回 0.5236 (PI/6)
Math.sin(0) returns 0.0
Math.sin(Math.toRadians(270)) 返 回 -1.0
Math,sin(Math.PI / 6) 返 回 0.5
Math,sin(Math.PI / 2)返 回 1.0
Math.cos(0) 返 回 1.0
Math,cos(Math.PI / 6) 返 回 0.866
Math,cos(Math.PI / 2) 返 回 0
Math.asin(0.5)返 回 0.523598333 (PI/6)
Math.acos(0.5) 返回 1.0472 (PI/3)
Math.atanf(1.0) 返回 0.78S398 (PI/4)
4.1.2 指数函数方法
4.1.3 取整方法
Math包括五个取整方法:
例如:
Math.ceil(2.1) 返回3.0
Math.ceil(2 .0) 返回 2.0
Math.ceil(-2.0)返回 -2.0
Math.ceil(-2.1) 返回 -2.0
Math.floor(2.1) 返回2 .0
Math.floor(2.0) 返回 2.0
Math.floor(-2.0) 返回-2.0
Math.floor(-2.1)返回-3.0
Math.rint(2.1)返回 2.0
Math.rint(-2.0)返回 -2.0
Math.rint(-2.1) 返回-2.0
Math.rint(2.5) 返回2.0
Math.rint(4.5) 返回4.0
Math.rint(-2.5)返回 -2.0
Math,round(2.6f) 返回3 // Returns int
Math.round(2.0) 返回2 // Returns long
Math.round(-2.0f) 返回-2 // Returns int
Math.round(-2.6) 返回-3 // Returns long
Math.round(-2.4) 返回-2 // Returns long
数字后面加f的是单精度,不加是双精度。
单精度返回Int值,向下取整。
4.1.4 min,max,和abs方法
min 和 max 方法用于返回两个数(int、long、float 或 double 型)的最小值和最大值。例如,max(4.4,5.0) 返回 5.0, 而 min(3,2) 返回 2。abs 方法以返回一个数(int、long、float 或 double 型)的绝对值。
4.1.5 random方法
生 成 大 于 等 于 0.0 且 小 于 1.0 的 double 型 随 机 数(0.0<=Math.random()<1.0)。可以使用它编写简单的表达式,生成任意范围的随机数。
(int)(Math.random ) * 10) → 返回 0 ~9 之间的一个随机整数
*50+(int)(Math.random()50→返回50~90之间的随机数 (????)
a+Math.random()*b→返回a~a+b之间的一个随机数,不包括a+b.
4.2 字符数据类型和操作
字符数据类型表示单个字符。
字符数据类型 char 用来表示单个字符。字符型直接量用单引号括住。考虑以下代码:
char letter =‘A’;
char numCh将字符 A 賦值给 char 型变量 letter。第二条语句将数字字符 4 賦值给 char型变量 numChar。
警告:字符串直接量必须括在双引号中。而字符直接量是括在单引号中的单个字符。因此"A" 是一个字符串,而 ’A’ 是一个字符。
4.2.1 Unicode和ASCII码
4.2.2 特殊字符的转义序列
反斜杠 \ 被称为转义字符。它是一个特殊字符。要显示这个字符,需要使用转义序列\.
4.2.3 字符型数据与数值型数据之间的转换
char 型数据可以转换成任意一种数值类型,反之亦然。将整数转换成 char•型数据时,只用到该数据的低十六位,其余部分都被忽略。
4.2.4 字符的比较和测试
两个字符可以使用关系操作符进行比较,如同比较两个数字一样。
4.3 String类型
字符串是一个字符序列。char类型只能表示一个字符。为了表示一串字符,使用称为 String (字符串)的数据类型。例如,下述代码将 message 声明为一个字符串,其值为 “Welcome to Java”:
String message = “Welcome to Java”;
String 实际上与 System 类和 Scanner 类一样,都是 Java 库中一个预定义的类。String类型不是基本类型,而是引用类型(reference type)。任何 Java 类都可以将变量表示为引用类型。使用引用类型声明的变量称为引用变量,它引用一个对象。这里,message 是一个变量,它引用一个内容为 Welcome to Java 的字符串对象。
表 4-7 列出了获得字符串长度、访问字符串中字符、连接字符串、转换字符串为大写或者小写,以及裁剪字符串的 String 方法。
4.3.1 求字符串长度
可以调用字符串的length() 方法获取它的长度。例如,下面代码
String message=" welcom to Java";
System.out.println("The length of " +message+ “is” +message.length());
显示The length of welcome to java is 15
4.3.2 从字符串中获取字符
方法 s.charAt(index)可用于提取字符串 s 中的某个特定字符,其中下标 index 的取值范围在 0 s.length()-1之间。例如,message.charAt(0)返回字符 W.
4.3.3 连接字符串
可以使用 concat 方法连接两个字符串。例如,如下所示的语句将字符串 s1 和 S2 连接构成 S3:
String s3 = s1.concat(s2);
4.3.4 字符串的转换
方法 toLowerCase()返回一个新字符串,其中所有字母小写;方toUpperCase() 返回一个新字符串,其中所有字母大写。
4.3.5 从控制台读取字符串
为了从控制台读取字符串,调用 Scanner 对象上的 next() 方法。
4.3.6 从控制台读取字符
为了从控制台读取字符,调用 nextLine() 方法读取一个字符串,然后在字符串上调用charAt(0)来返回一个字符。
4.3.7 字符串比较
4.3.8 获得子字符串
方法 s.charAt(index) 可用于提取字符串 s 中的某个特定字符。也可以使用 String 类中的 substring 方法从字符串中提取子串
例如,
String message=“Welcome to Java”;
String message = message.substring(0, 11) + “HTML”;
字符串 message 变成了 Welcome to HTML.
4.3.9 获取字符 串中的字符或者字串
String 类提供了几个版本的 indexOf 和 lastlndexOf 方法,它们可以在字符串中找出一个字符或一个子串.
4.3.10 字符串和数字间的转换
可以将数值型字符串转换为数值。要将字符串转换为 int 值,使Integer.parselnt
方法.
4.4 示例学习
将十六进制数转换为十进制数
十六进制记数系统有 16 个数字:0~9, A~F字母 A、B 、C、D、E 和 F 对应于十进制数字 10、11 、12、13、14 和 15。
编程练习题
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);
System.out.print("Enter today's day");
int today=scanner.nextInt;
System.out.print("Enter the number of days elapsed since today: ");
int since=scanner.nextInt;
swicth(day%7){
case 1:
System.out.println("Today is Monday");
break;
case 2:
System.out.println("Today is Tuesday");
break;
case 3:
System.out.println("Today is Wednsday");
break;
case 4:
System.out.println("Today is Thirsday");
break;
case 5:
System.out.println("Today is Friday");
break;
case 6:
System.out.println("Today is Saturday");
break;
case 0:
System.out.println("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("不是!");
}
}
}