逻辑 与 判断Logics and Conditions
比较运算符The Equality and Relational Operators
药水商店案例
对顾客增收 1元 塑料袋环保费
代码
1 2 3 4 5 6 7 8 9+10+11+12+13+14+15*16+17 18 19 20 public class Store { public static void main(String[] args) { double price = Console.readDouble(); int amount = Console.readInt(); double total = price * amount; Console.println("要袋子么?"); Console.println("1 要"); Console.println("2 不要"); int option = Console.readInt(); if (option == 1) { total += 1; } Console.println(total); }}
有哪些
> < == >= <= !=
特性
比较运算符 运算出来的数据类型 为 boolean
boolean
数据类型
定义
代表对错
boolean
字面量
true false
问题
我想判断 一个值 是不是 在 一个区间内
1!2 3if (10 < price < 100) { ...}
为什么?
逻辑运算符Logical Operators
有哪些
&& || !
是什么
针对布尔值计算的运算符
&&
运算符
并且
都对才对
1 =2 3 =boolean v1 = true && true;true boolean v2 = true && false;false
||
运算符
或者
一个对 就对
1 =2 3 =boolean v1 = false || false;false boolean v2 = true || false;true
!
运算符
逻辑反转
1 =boolean v1 = !true;false
与 比较运算符 组合 表达范围
错
1!2 3if (10 < price < 100) { ...}
对
1*2 3if (10 < price && price < 100) { ...}
断义
当有一个值能断定结果时, 后面的表达式不执行
没有得出结果,会一直执行
1 2 3 4 int min = 3;int max = 7;int x = 4;boolean result = min < x && x < max; = 3 < x && x < max; = 3 < 4 && x < max; = true && x < max; = true && 4 < max; = true && 4 < 7; = true && true; = true;
能断定结果,立刻得出结果
&&
中途得到 false
会立刻终止
1 2 3 4 int min = 3;int max = 7;int x = 1;boolean result = min < x && x < max; = 3 < x && x < max; = 3 < 1 && x < max; = false && x < max; = false;
||
同理
||
中途得到 true
会立刻终止
1 2 3 4 int min = 3;int max = 7;int x = 1;boolean result = x < min || max < x; = 1 < min || max < x; = 1 < 3 || max < x; = true || max < x; = true;
优先级 从高 到低
()
取值
强制类型转换
算数运算符
比较运算符
逻辑运算符
赋值运算符
常备小括号
记不住的时候,加小括号,保险
不好
int result = a && b || c;
好
int result = a && (b || c);
if 语句if Statements
药水商店
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Store { public static void main(String[] args) { double price = Console.readDouble(); int amount = Console.readInt(); double total = price * amount; Console.println("要袋子么?"); Console.println("1 要"); Console.println("2 不要"); int option = Console.readInt(); if (option == 1) { total += 1; } Console.println(total); }}
if
基本认知
格式
if (<condition>) { <statement> ... }
效果
被 if
语句管辖的内容,只有 condition 通过 才会执行。
小要求
<condition>
部分必须为 生产 布尔值的 表达式
缩进Indentation
干什么用
代表从属关系
缩进 与 if
有 缩进的 不一定执行
缩进规则
{
之后 换行 + 缩进
换行 - 缩进 之后 }
基于规则,找大括号
L 大法
重要的层级
层级 | 意义 | 详细 |
---|---|---|
0 | 结构 | 112 里会讲 |
1 | 函数 | 111 之后讲 |
2 | 执行代码 | 目前可以认为一定执行 |
3+ | 执行代码 | 不一定执行 |
if 语句规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15// 只有一句话 if ( ... ) { statement;} // 可以省略大括号if ( ... ) statement; // 可以不缩进if ( ... )statement; // 可以不换行if ( ... ) statement;
if
语句 常用版型
一个 if
语句
1 2 3 4 51if (2) { 3}4
2 如果对 走
1 2 3 4
2 如果不对
1 2 4
两个嵌套的 if
语句
1 2 3 4 5 6 7 8 91if (2) { 3 if (4) { 5 } 6}7
只有 2 对了,才会 执行 3 - 6
如果 2 是错的,4 是对的,程序的执行序列是什么
1 2 7
两个并列 if
语句
1 2 3 4 5 6 7 8 91if (2) { 3}4if (5) { 6}7
两个 if 语句 互不影响
3 6 有没有可能 都 执行?
会
不会
3 6 有没有可能 都不 执行?
会
不会
如果 2 是错的,5 是对的,程序的执行序列是什么
1 2 4 5 6 7
一个 if
语句 带 else
分支
1 2 3 4 5 6 71if (2) { 3} else { 4}5
2 如果对 走
1 2 3 5
2 如果不对 走
1 2 4 5
3 4 有没有可能 都 执行?
会
不会
3 4 有没有可能 都不 执行?
会
不会
一个 if
语句 带 else if
分支
1 2 3 4 5 6 71if (2) { 3} else if (4){ 5}6
2 如果对 走
1 2 3 6
2 如果不对 4 如果对 走
1 2 4 5 6
2 4 都不对 走
1 2 4 6
一个 if
语句 带 else if
分支 和 else
分支
1 2 3 4 5 6 7 8 91if (2) { 3} else if (4){ 5} else { 6}7
2 对 4 不对 会走哪
1 2 3 7
2 对 4 对 会走哪
1 2 3 7
2 不对 4 对 会走哪
1 2 4 5 7
2 不对 4 不对 会走哪
1 2 4 6 7
if
语句 完整规则
一个 if
语句中
if
只能出现 1 次
else if
可以出现 0 - n 次
else
可以出现 0 - 1 次
组合方式
可以并列
可以嵌套
语句 与 分支 对比
一个 if
语句 多个 branch
最多执行 1 个 branch
不可能执行 2 个以上的 branch
多个 if
语句,每个 1 个 branch
可能执行多个 branch
因为 这些 if 语句 互不影响
if 语句 逻辑关系
挑战 1
功能
输入 分数
输出 评分等级
数据
分数 | 等级 |
---|---|
0 - 59 | 挂科 |
60 - 100 | 通过 |
样例
> <78通过
需求的逻辑范围
如果需求上指明,用户绝对不会输入非法范围内的值,则代码不需要考虑
使用前
1 2 3 4 5 6 7 8 int score = 78;if (0 <= score && score < 60) { Console.println("挂科"); } else if (60 <= score && score < 100) { Console.println("通过"); }
使用后
1 2*3 4 5*6 7 int score = 78;if (score < 60) { Console.println("挂科"); } else { Console.println("通过");}
挑战 2
数据
分数 | 等级 |
---|---|
0 - 59 | 挂科 |
60 - 79 | 通过 |
80 - 100 | 优秀 |
样例
> <88优秀
if
语句逻辑关系
每个后面的 condition 只有在前面所有 condition 都不成立的情况下 才 check
只可能走到一个 branch 里
使用前
1 2 3 4 5 6 7 8 9 10 int score = 88;if (score < 60) { Console.println("挂科"); } else if (60 <= score && score < 80) { Console.println("通过"); } else { Console.println("优秀");}
使用后
1 2 3 4 5*6 7 8 9 10 int score = 88;if (score < 60) { Console.println("挂科"); } else if (score < 80) { Console.println("通过"); } else { Console.println("优秀");}
变量作用域Variable Scopes
是什么
决定变量的有效范围
规则
出生:变量声明
死亡:撞到所包含它的最近的回大括号
命名重名
变量命名时,不能跟任何现在可以接触到的变量同名
作用域 x if 语句
可以在 if 语句 分支里,定义变量
该变量不会影响到其它分支,以及 if 语句外面
1 2 3 4 5 6 7 8 9 10 11 12+13*14 15 16 double price = Console.readDouble();int amount = Console.readInt(); double total = price * amount; Console.println("要袋子么?");Console.println("1 要");Console.println("2 不要");int option = Console.readInt(); if (option == 1) { int bagFee = 1; total += bagFee;} Console.println(total);
挑战 1
结账 第一版本
程序让用户输入金额
< >输入金额200
如果金额 不到 100 元
收取固定 6 元 物流费用
< < <商品总价:30 元 物流费:6 元订单总价:36 元
如果金额 达到 100 元
就包邮处理
< < <商品总价:160 元 物流费:免费订单总价:160 元
挑战 2
增加新规则
如果金额 达到 200 元
就 - 40
< < < <商品总价:200 元 优惠:40 元物流费:免费订单总价:160 元
图格子的故事

参考答案
另外一个思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Checkout { public static void main(String[] args) { int price = Console.readInt(); Console.println("商品总价:" + price + " 元"); int total = price; // 满 200 减 40 if (price >= 200) { int discount = 40; total = price - discount; Console.println("优惠:" + discount + " 元"); } // 满 100 免 运送费 if (price >= 100) { Console.println("运送费:" + "免费"); } else { int shippingFee = 6; total = price + shippingFee; Console.println("运送费:" + shippingFee + " 元"); } // 显示支付 Console.println("您需支付:" + total + " 元"); }}
人工内存
是什么
模拟计算机处理代码的过程
目标
没有运行代码,但已经在脑子里运行了这个代码
要求
你需要知道 计算机是 如何运行代码
你需要知道 在任何一个时间
都有哪些变量活着
并且 这些变量的值 都是多少
样例
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Solution { public static void main(String[] args) { int price = Console.readInt(); Console.println("商品总价:" + price + " 元"); int total = price; // 满 200 减 40 if (price >= 200) { int discount = 40; total = price - discount; Console.println("优惠:" + discount + " 元"); } // 满 100 免 运送费 if (price >= 100) { Console.println("运送费:" + "免费"); } else { int shippingFee = 6; // 不要动它 total = price + shippingFee; Console.println("运送费:" + shippingFee + " 元"); } // 显示支付 Console.println("您需支付:" + total + " 元"); }}
内存
Line price total shippingFee io 3 97 > 97 4 | < 总价:97 元 6 | 97 9 | | = false 16 | | = false 19 | | 6 20 | 103 | 21 | | | < 运送费:6 元 22 | | x 25 | | < 您需支付:103 元
程序调试Debugging
输出调试
检查点
在某些位置输出信息
运行后,在控制台检查是否显示,从而证明这一行有没有执行
1 2 3 4 5 6 ...if (...) { Console.println("Check Point 1"); ...}...
输出数据
在某些位置输出信息
运行后,在控制台检查是否显示,从而证明这一行有没有执行
1 2 3 int a = 0;a = ...;Console.println("a: " + a);
Debug 模式
增加断点
在行号 右边,可以点一下,会点出一个粉刺
就是断点

进入 debug 模式
点击 小三角,出来的菜单里,点击 Debug

之后就可以进入 debug 模式
debug 模式
debug 模式下,程序运行到 断点处,就会暂停
控制面板
检查数据
小跳跃按钮 下一行
继续播放按钮 继续执行
红色方块按钮 结束执行
三目运算符Ternary Operator
样本
使用前
1 2 3 4 5 if (x < 0) { a = -1;} else { a = 1;}
特性
if else
都是一赋值语句
给同一个变量
使用后
1 a = x < 0 ? -1 : 1;
语法
模板
<condition> ? <expression1> : <expression2> ;
样例
1 x *= x < 0 ? -1 : 1;
Switch 语句
样本
使用前
1 2 3 4 5 6 7 if (x == 0) { ...} else if (x == 1) { ...} else if (x == 2) { ...}
特性
if else
都是等于比较
比较一边是一样的变量
另外一遍是字面量
比较的是原类型
使用后
1 2 3 4 5 6 7 8 9 10 11 switch (x) { case 0: ... break; case 1: ... break; case 2: ... break;}
语法
模板
switch (<variable>) { case <value1>: <statement> ... break; case <value2>: <statement> ... break; }
下滑Fall Through
可以不写 break
就会逐句执行, 直到遇见 break
default
可以写 default:
起到 else
的效果
API 补充
字符串比较
字符串比较时,不能使用 ==
符号,需要使用 x.equals(y)
变量和字面量比较
1 2 3 String command = Console.readLine();boolean result = command.equals("要");Console.println(result);
变量和变量比较
1 2 3 4 String str1 = Console.readLine();String str2 = Console.readLine();boolean result = str1.equals(str2);Console.println(result);
随机值生成
1 new Random().nextInt(n);
相比 Math.random()
,这个 API 可以生成 之间的整数
案例
1 2 int roll = new Random().nextInt(6) + 1;Console.println(roll);