Lesson 03

表达式Expressions

什么是表达式

字面量 运算符 变量 其它

案例
1
int result = v1 + v2; ^^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

表达式缩合

定义

计算机如何计算表达式

规则

小学生算数学题,一样

一步一步算

样例
1 2 3
int v1 = 3;int v2 = 4;int result = v1 + v2; = 3:int + v2; = 3:int + 4:int; = 7:int;

缩合 与 变量类型

规则

数值计算时,运算符左右两边如果不一致,小类型 会自动转为 大类型

1
3 + 4.03:int + 4.0:double 7.0:double
练习
1 2 3
int v1 = 3;int v2 = 4;int result = v1 + v2 * 3.0 - 2;

算术运算符Arithmetic Operators

有哪些

+   -   *   /   %
+=  -=  *=  /=  %=
++  --

运算优先级

优先级
1 =
int a = 2 + 2 * 5;12

先算 乘除

再算 加减

再 赋值

括号

() 可以提高优先级

1 =
int a = (3 + 2) * 5;30

/ 运算符

观察
1 2 3
int v1 = 3;int v2 = 4;double result = v1 / v2;
原因分析

为什么?

解决方案
1 2 3!
double result = 1.0 * v1 / v2;double result = (double)v1 / v2;double result = (double)(v1 / v2);

% 运算符

案例
1 =2 =3 =4 =
Console.println(7 % 2);1Console.println(7 % 3);1Console.println(7 % 4);3Console.println(8 % 4);0
用法

分析一个递增序列 0, 1, 2, 3, 4, 5 对 固定数 比如 2 求余,你会得到什么?

外卖结单案例

代码
1 2 3 4 5 6 7
public class Main { public static void main(String[] args) { int price = 9; price = price + 1; Console.println("请支付 " + price + "元"); }}

算数同时赋值

规则

当出现

案例
x = x ? a;

可缩写为

x ?= a;
案例
price = price + 1;

等效于

price += 1;
同样适用于
+=  -=  *=  /=  %=

++--

规则

当出现

案例
x ?= 1;

可缩写为

x??;
案例
price += 1;

等效于

price++;
同样适用于
++  --
注意

出现这个运算符时,请单独占一行,不要像下面这么写

int a = 4;
int b = a++;

练习

背景

一人从一栋大楼上高空坠落,不幸遇难。

名侦探张大妈,过来调查情况,想知道死者是从哪层跳下来的。

但周围人都自觉没用手机录像,唯独有一个人有录了一段微信语音

语音中听到 死者生前大叫 啊.....

请你写一个程序,帮助张大妈算出 他是从那一层掉下来的

物理知识

自由落体公式是

    1     2
h = - · gt
    2

已知 尖叫时间长达 3.2 秒

假设 g 为 9.8

已知 大楼 每层 高 2米

代码模板
1 2 3 4 5 6
public class Main { public static void main(String[] args) { double second = 3.2; Console.println(10); }}

运算符的种类

单目
a++
!a
双目
a + b
a -= c
三目
a ? b : c

运算符空格规范

双目 和 三目 运算符 左右两边 加 空格

int sum = 4 + 7;
int sum=4+7;

流程控制语句Control Flow Statements

外卖结单案例

1 2 3 4 5+6+7+8+9+10+11*12+13 14 15 16
public class Main { public static void main(String[] args) { int price = 9; Console.println("要加米饭么?"); Console.println("1 要"); Console.println("2 不要"); int option = Console.readInt(); if (option == 1) { price = price + 1; } Console.println("请支付 " + price + "元"); }}

if 基本认知

格式
if (<condition>) {
    <statement>
    ...
}
效果

if 语句管辖的内容,只有 condition 通过 才会执行。

小要求

<condition> 部分必须为 生产 布尔值的 表达式

比较运算符The Equality and Relational Operators

有哪些

>   <   ==
>=  <=  !=

特性

比较运算符 运算出来的数据类型 为 boolean

boolean 数据类型

定义

代表对错

boolean 字面量
true
false

问题

我想判断 一个值 是不是 在 一个区间内

1!2 3
if (10 < price < 100) { ...}

为什么?

逻辑运算符Logical Operators

有哪些

&&  ||  !

是什么

针对布尔值计算的运算符

&& 运算符

并且

都对才对

1 =2 3 =
boolean v1 = true && true;true boolean v2 = true && false;false

|| 运算符

或者

一个对 就对

1 =
boolean v1 = !true;false

! 运算符

逻辑反转

1 =2 3 =
boolean v1 = false || false;false boolean v2 = true || false;true

与 比较运算符 组合 表达范围

1!2 3
if (10 < price < 100) { ...}
1!2 3
if (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;

赋值运算符

是什么

=

赋值运算符表达式缩合

赋值也是运算符

赋值语句也是表达式

赋值语句也有表达式结果值, 结果值是赋值

1 2 3
int a, b;a = 5;5b = a = 6;b = 66

优先级

优先级 从高 到低

()

取值

强制类型转换

算数运算符

比较运算符

逻辑运算符

赋值运算符

常备小括号

记不住的时候,加小括号,保险

不好
int result = a && b || c;
int result = a && (b || c);

ZZAX 微信公众

文档一更新,立刻告诉你