java 运算符 选择语句

发布时间:2024年01月12日

1:运算符

运算符:对字面量或者变量进行操作的符号

表达式:运算符把字面量或者变量连接起来符合java语法的式子就可以称为表达式。不同运算符连接的表达式体现的是不同类型的表达式。

举例说明:**

int a = 10;

int b = 20;

int c = a + b;

+ :是运算符,是算术运算符

a + b:是表达式,由于+是算术运算符,所以这个表达式叫算术表达式

1.1 算术运算符

Java中的算术运算符有如下几种:

了解了Java中的算术运算符之后,下面我们到IDEA中去使用一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">算术运算符</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义两个int类型的变量</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#116644">6</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">b</span> <span style="color:#981a1a">=</span> <span style="color:#116644">4</span>;
?
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">+</span> <span style="color:#000000">b</span>);<span style="color:#aa5500">//10</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">-</span> <span style="color:#000000">b</span>);<span style="color:#aa5500">//2</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">*</span> <span style="color:#000000">b</span>);<span style="color:#aa5500">//24</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">/</span> <span style="color:#000000">b</span>);<span style="color:#aa5500">//1.5? 1</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">%</span> <span style="color:#000000">b</span>);<span style="color:#aa5500">//2</span>
?
 ? ? ? ?<span style="color:#aa5500">//整数相除只能得到整数,要想得到小数,必须有浮点数的参与</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#116644">6.0</span> <span style="color:#981a1a">/</span> <span style="color:#116644">4</span>);
 ?  }
}
?</span></span>

注意事项:

1.2 案例数值拆分

需求:定义一个三位数,将其拆分为个位、十位、百位后,输出在控制台

什么意思呢?

举例:int number = 123;

在控制台输出结果:

个位是:3

十位是:2

百位是:1

知道了要做什么后,下面我们来分析一下如何获取个位,十位,百位:

分析:

① 个位的计算:数值 % 10

123 除以 10(商12,余数为3)

② 十位的计算:数值 / 10 % 10

123 除以 10 (商12,余数为3,整数相除只能得到整数)

12 除以 10 (商1,余数为2)

③ 百位的计算:数值 / 10 / 10 % 10

123 / 10 / 10 % 10(123 / 10 得到12,12 / 10 得到1,1 % 10 得到 1)

分析完毕之后,下面我们到IDEA中去实现一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">需求:定义一个三位数,将其拆分为个位、十位、百位后,输出在控制台</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorTest</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义一个三位数</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">number</span> <span style="color:#981a1a">=</span> <span style="color:#116644">123</span>;
?
 ? ? ? ?<span style="color:#aa5500">//获取个,十,百位数据</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">=</span> <span style="color:#000000">number</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">=</span> <span style="color:#000000">number</span> <span style="color:#981a1a">/</span> <span style="color:#116644">10</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">bai</span> <span style="color:#981a1a">=</span> <span style="color:#000000">number</span> <span style="color:#981a1a">/</span> <span style="color:#116644">100</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span>;
?
 ? ? ? ?<span style="color:#aa5500">//输出结果</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"个位是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">ge</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"十位是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">shi</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"百位是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">bai</span>);
 ?  }
}</span></span>

1.3 +操作的三种情况

1.3.1 数字相加(类型转换)

在Java程序中,数据参与运算,要求类型一致。这里就涉及到了数据的类型转换。而类型转换又分为两种:

  1. 隐式转换

  2. 强制转换

隐式转换:把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量

强制转换:把一个表示数据范围大的数值或者变量赋值给另一个表示数据范围小的变量

格式:数据类型 变量名 = (目标数据类型)(数值或者变量)

IDEA中代码演示如下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">数字相加</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo01</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">b</span> <span style="color:#981a1a">=</span> <span style="color:#116644">13.14</span>;
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">+</span> <span style="color:#000000">b</span>);
?
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">c</span> <span style="color:#981a1a">=</span> (<span style="color:#008855">int</span>)(<span style="color:#000000">a</span> <span style="color:#981a1a">+</span> <span style="color:#000000">b</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">c</span>);
<span style="color:#aa5500">// ? ? ?  double d = a + b;</span>
?
 ? ? ? ?<span style="color:#008855">short</span> <span style="color:#000000">s</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">j</span> <span style="color:#981a1a">=</span> <span style="color:#000000">s</span> <span style="color:#981a1a">+</span> <span style="color:#000000">i</span>;
<span style="color:#aa5500">// ? ? ?  short ss = s + i;</span>
?
 ? ? ? ?<span style="color:#008855">short</span> <span style="color:#000000">ss</span> <span style="color:#981a1a">=</span> (<span style="color:#008855">short</span>) (<span style="color:#000000">s</span> <span style="color:#981a1a">+</span> <span style="color:#000000">i</span>);
 ?  }
}</span></span>

讲解完毕后,大家赶快动手练习一下吧。

1.3.2 字符相加

来,继续啊,这里我们再来说一下字符参与+操作。

先到IDEA中去讲解,然后再回来总结。

IDEA中代码演示如下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">字符相加</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo02</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义两个变量</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">char</span> <span style="color:#000000">ch</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">'A'</span>;<span style="color:#aa5500">//'A'的值是65</span>
 ? ? ? ?<span style="color:#000000">ch</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">'a'</span>;<span style="color:#aa5500">//'a'的值是97</span>
 ? ? ? ?<span style="color:#000000">ch</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">'0'</span>;<span style="color:#aa5500">//'0'的值是48</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">+</span> <span style="color:#000000">ch</span>);
 ?  }
}</span></span>

代码演示完毕后,回到资料总结:

字符参与+操作,其实就是拿字符在计算机底层对应的数值来进行计算的,这里我们了解一下英文大小写字符和数字字符对应的数据值:

  • ‘A’ 65 A-Z是连续的

  • ‘a’ 97 a-z是连续的

  • ‘0’ 48 0-9是连续的

常用的字符及其对应的数值关系:

1.3.3 字符串相加

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">字符串相加</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo03</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"it"</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"heima"</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"itheima"</span> <span style="color:#981a1a">+</span> <span style="color:#116644">666</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#116644">666</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"itheima"</span>);
?
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"itheima"</span> <span style="color:#981a1a">+</span> <span style="color:#116644">6</span> <span style="color:#981a1a">+</span> <span style="color:#116644">66</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#116644">1</span> <span style="color:#981a1a">+</span> <span style="color:#116644">99</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"年itheima"</span>);
 ?  }
}
?</span></span>

代码演示完毕后,回到资料总结:

  • 当“+”操作中出现字符串时,这个“+”是字符串连接符,而不是算术运算

    • “zhongguo”+ 666

  • 当连续进行“+”操作时,从左到右逐个执行

    • 1 + 9999 + “岁zhongguo”

1.4 赋值运算符

Java中的赋值运算符有如下几种:

了解了Java中的赋值运算符之后,下面我们到IDEA中去使用一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">赋值运算符</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"a:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">a</span>);
?
 ? ? ? ?<span style="color:#aa5500">// += : 把左边和右边的数据相加,最后把结果赋值给左边</span>
<span style="color:#aa5500">// ? ? ?  a += 20;</span>
 ? ? ? ?<span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#000000">a</span> <span style="color:#981a1a">+</span> <span style="color:#116644">20</span>;
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"a:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">a</span>);
?
?
 ? ? ? ?<span style="color:#aa5500">//注意:扩展的赋值运算符底层隐含了强制类型转换</span>
 ? ? ? ?<span style="color:#008855">short</span> <span style="color:#000000">s</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
<span style="color:#aa5500">// ? ? ?  s += 2;</span>
 ? ? ? ?<span style="color:#000000">s</span> <span style="color:#981a1a">=</span> (<span style="color:#008855">short</span>) (<span style="color:#000000">s</span> <span style="color:#981a1a">+</span> <span style="color:#116644">2</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"s:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">s</span>);
 ?  }
}</span></span>

注意事项:

2:选择语句

来,继续啊,为了控制程序的执行流程,Java 就提供了一些流程控制语句,我们先来举例说明一下:

顺序结构:从上往下,依次执行

选择结构:

QQ界面的登录,填写账号和密码正确,展示好友列表界面。填写账号或者密码有误,提示:你输入的账号或密码不正确。

这就是通过选择结构来实现的,实际开发中,类似这种逻辑判断的情况非常常见。

循环结构:

比如说,京东商城中,查询手机数据,京东就会返回给你一堆符合你查询要求的数据。

假如,每一部手机数据的展示需要5行代码,如果最终返回了1000部手机,你就需要编写5000行代码来实现数据展示。

这样就太麻烦了,那么,有没有一种流程语句,能够简化这种操作呢?答案肯定是有的,就是我们的循环结构,它就可以通过一个比较简单的结构来控制我们的手机展示1000次。

比如说,带着这样写就可以了,这就是循环结构。

2.1 顺序结构

顺序结构:是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行

执行流程图:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">顺序结构语句</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OrderDemo</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始"</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"语句A"</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"语句B"</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"语句C"</span>);
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"结束"</span>);
 ?  }
}</span></span>

2.2 Debug的基本使用

Debug:是供程序员使用的程序调试工具,它可以用于查看程序的执行流程,也可以用于追踪程序执行过程来调试程序

它的两大作用,查看程序执行流程,调试程序。由于目前我们每个程序的代码内容还比较少,

所以,今天重点掌握查看程序执行流程,当代码复杂的时候,我们需要通过debug来调试程序。那么,debug操作的流程是什么样子的呢?

在讲解操作流程前,我们来说一下:

Debug,又被称为断点调试,断点其实是一个标记,告诉我们从哪里开始查看

知道了Debug被称为断点调试后,我们来讲一下Debug的操作流程:

① 如何加断点

选择要设置断点的代码行,在行号的区域后面单击鼠标左键即可。如图

② 如何运行加了断点的程序

在代码区域右键Debug执行

③ 看哪里

看Debugger窗口,看Console窗口

④ 点哪里

点(Step Over F8) 开始,点(Stop)结束

⑤ 如何删除断点

选择要删除的断点,单击鼠标左键即可。也就是把第一步的动作,再做一次即可。

总结一下,Debug的使用流程:

①如何加断点:选择要设置断点的代码行,在行号的区域后面单击鼠标左键即可

②如何运行加了断点的程序:在代码区域右键Debug执行

③看哪里:看Debugger窗口,看Console窗口

④点哪里:点(Step Over F8) 开始,点(Stop)结束

⑤如何删除断点:选择要删除的断点,单击鼠标左键即可

通过Debug的方式看一下,如下代码的执行流程:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">Debug查看程序执行流程</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">DebugDemo</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义一个int类型的变量,用来表示价格</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">price</span> <span style="color:#981a1a">=</span> <span style="color:#116644">998</span>;
?
 ? ? ? ?<span style="color:#aa5500">//输出变量</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">price</span>);
?
 ? ? ? ?<span style="color:#aa5500">//修改变量的值</span>
 ? ? ? ?<span style="color:#000000">price</span> <span style="color:#981a1a">=</span> <span style="color:#116644">888</span>;
?
 ? ? ? ?<span style="color:#aa5500">//再次输出变量的值</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">price</span>);
 ?  }
}</span></span>

2.3 选择语句之if

选择语句有两种结构:

  1. if语句

  2. switch语句

if语句,它会根据判定的结果(真或假)决定执行某个分支的代码

举例说明一下:

无人驾驶的核心就是需要编写大量的判断逻辑,让车辆的行驶变得更加安全。

了解了交通信号灯的基本逻辑后,下面呢,我们通过if语句来实现过马路的信号灯判断,而if语句呢,有三种格式,这里我们首先来学习第一种格式

2.3.1 if语句格式1

格式:

范例:

执行流程:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">if语句格式1</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfDemo01</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义一个变量,用来表示绿灯的状态</span>
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">false</span>;
?
 ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">isGreen</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ?  }
 ?  }
}</span></span>
2.3.2 if语句格式2和格式3
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">if语句格式2</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfDemo02</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义一个变量,用来表示绿灯的状态</span>
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">false</span>;
?
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">isGreen</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ?  } <span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"不是绿灯,不允许行使"</span>);
 ? ? ?  }
 ?  }
}</span></span>

if语句格式2:

格式:

范例:

执行流程:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfDemo03</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义三个变量,表示交通信号灯的三种状态</span>
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isRed</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isYellow</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">isRed</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"红灯停"</span>);
 ? ? ?  }
?
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">isGreen</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ?  }
?
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">isYellow</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"黄灯亮了等一等"</span>);
 ? ? ?  }
 ?  }
}</span></span>

很明显,这是不合理的,因为我们把三种状态都设置为true了,而实际生活中,只会有一种状态为true,怎么解决呢多种情况的判断呢?

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">if语句格式3</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfDemo03</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义三个变量,表示交通信号灯的三种状态</span>
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isRed</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
 ? ? ? ?<span style="color:#008855">boolean</span> <span style="color:#000000">isYellow</span> <span style="color:#981a1a">=</span> <span style="color:#221199">true</span>;
?
 ? ? ? ?<span style="color:#aa5500">//红灯</span>
<span style="color:#aa5500">// ? ? ?  isGreen = false;</span>
<span style="color:#aa5500">// ? ? ?  isYellow = false;</span>
?
 ? ? ? ?<span style="color:#aa5500">//绿灯</span>
<span style="color:#aa5500">// ? ? ?  isRed = false;</span>
<span style="color:#aa5500">// ? ? ?  isYellow = false;</span>
?
 ? ? ? ?<span style="color:#aa5500">//黄灯</span>
<span style="color:#aa5500">// ? ? ?  isRed = false;</span>
<span style="color:#aa5500">// ? ? ?  isGreen = false;</span>
?
 ? ? ? ?<span style="color:#aa5500">//出现了故障,信号灯都不亮了</span>
 ? ? ? ?<span style="color:#000000">isRed</span> <span style="color:#981a1a">=</span> <span style="color:#221199">false</span>;
 ? ? ? ?<span style="color:#000000">isGreen</span> <span style="color:#981a1a">=</span> <span style="color:#221199">false</span>;
 ? ? ? ?<span style="color:#000000">isYellow</span> <span style="color:#981a1a">=</span> <span style="color:#221199">false</span>;
?
?
 ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">isRed</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"红灯停"</span>);
 ? ? ?  }<span style="color:#770088">else</span> <span style="color:#770088">if</span>(<span style="color:#000000">isGreen</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ?  }<span style="color:#770088">else</span> <span style="color:#770088">if</span>(<span style="color:#000000">isYellow</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"黄灯亮了等一等"</span>);
 ? ? ?  } <span style="color:#770088">else</span>  {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"交通信号灯故障,请在保证安全的情况下通行"</span>);
 ? ? ?  }
 ?  }
}</span></span>

if语句格式3:

格式:

执行流程:

2.3.3 案例1(交通信号灯)

需求:交通信号灯由红灯、绿灯、黄灯组成,请通过程序实现不同的信号灯给出不同的提示。

提示信息:红灯停、绿灯行、黄灯亮了等一等

分析:

① 用哪种类型来表示信号灯的三种状态呢?

用int类型来表示信号灯的三种状态(1表示红灯,2表示绿灯,3表示黄灯)

② 用if语句的哪种格式来实现该需求呢?

一般来说:1种情况的判断用if格式1,2种情况的判断用if格式2,多种情况的判断用if格式3

③ if语句判断的表达式该怎么写呢?

int light = 1;

if(light == 1) {…}

==:是关系运算符,用来判断两个数据是否相等,如果相等,结果为true,否则为false

分析完毕后,我们来说一下实现步骤:

实现步骤:

① 定义一个int类型的变量用来表示信号灯的状态(1表示红灯,2表示绿灯,3表示黄灯)

② 用if语句格式3进行多种情况的判断

③ 根据不同的情况给出不同的提示信息

知道了实现步骤,下面我们到IDEA中去实现一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">需求:交通信号灯由红灯、绿灯、黄灯组成,请通过程序实现不同的信号灯给出不同的提示。</span>
 ? ?<span style="color:#aa5500">提示信息:红灯停、绿灯行、黄灯亮了等一等</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfTest01</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//1:定义一个int类型的变量用来表示信号灯的状态(1表示红灯,2表示绿灯,3表示黄灯)</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">light</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
 ? ? ? ?<span style="color:#000000">light</span> <span style="color:#981a1a">=</span> <span style="color:#116644">2</span>;
 ? ? ? ?<span style="color:#000000">light</span> <span style="color:#981a1a">=</span> <span style="color:#116644">3</span>;
 ? ? ? ?<span style="color:#000000">light</span> <span style="color:#981a1a">=</span> <span style="color:#116644">4</span>;
?
 ? ? ? ?<span style="color:#aa5500">//2:用if语句格式3进行多种情况的判断</span>
 ? ? ? ?<span style="color:#aa5500">//3:根据不同的情况给出不同的提示信息</span>
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">light</span> <span style="color:#981a1a">==</span> <span style="color:#116644">1</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"红灯停"</span>);
 ? ? ?  } <span style="color:#770088">else</span> <span style="color:#770088">if</span> (<span style="color:#000000">light</span> <span style="color:#981a1a">==</span> <span style="color:#116644">2</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ?  } <span style="color:#770088">else</span> <span style="color:#770088">if</span> (<span style="color:#000000">light</span> <span style="color:#981a1a">==</span> <span style="color:#116644">3</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"黄灯亮了等一等"</span>);
 ? ? ?  } <span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"交通信号灯故障,请在保证安全的情况下通行"</span>);
 ? ? ?  }
 ?  }
}</span></span>

2.3.4 关系运算符

Java中的关系运算符有如下几种:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">关系运算符</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo01</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义三个int类型的变量</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">j</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">k</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
?
 ? ? ? ?<span style="color:#aa5500">//等于:==</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">==</span> <span style="color:#000000">j</span>);<span style="color:#aa5500">//false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">==</span> <span style="color:#000000">k</span>);<span style="color:#aa5500">//true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//不等于:!=</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">!=</span> <span style="color:#000000">j</span>);<span style="color:#aa5500">//true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">!=</span> <span style="color:#000000">k</span>);<span style="color:#aa5500">//false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//大于:></span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">></span> <span style="color:#000000">j</span>);<span style="color:#aa5500">//false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">></span> <span style="color:#000000">k</span>);<span style="color:#aa5500">//false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//大于等于:>=</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">>=</span> <span style="color:#000000">j</span>);<span style="color:#aa5500">//false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">>=</span> <span style="color:#000000">k</span>);<span style="color:#aa5500">//true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//千万不要把==写成了=</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#000000">j</span>); <span style="color:#aa5500">//20</span>
 ?  }
}</span></span>

注意事项:

2.3.5 案例2(奇偶数)

需求:给定一个整数,请用程序实现判断该整数是奇数还是偶数,并在控制台输出该整数是奇数还是偶数

提示:偶数是能够被2整除的整数

首先,我们来简单的分析一下:

分析:

① 如何判断一个整数是奇数还是偶数?

整数对2进行取余,余数为0是偶数,否则为奇数

② 用if语句的哪种格式来实现该需求呢?

该整数要么是偶数,要么是奇数,2种情况,所以用if语句格式2

分析完毕后,我们来说一下实现步骤:

实现步骤:

① 定义一个int类型的整数

② 用if语句格式2进行判断

③ 根据是否是偶数给出不同的提示信息

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">需求:给定一个整数,请用程序实现判断该整数是奇数还是偶数,并在控制台输出该整数是奇数还是偶数</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfTest02</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//1:定义一个int类型的整数</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">number</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#000000">number</span> <span style="color:#981a1a">=</span> <span style="color:#116644">9</span>;
?
 ? ? ? ?<span style="color:#aa5500">//2:用if语句格式2进行判断</span>
 ? ? ? ?<span style="color:#aa5500">//3:根据是否是偶数给出不同的提示信息</span>
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">number</span> <span style="color:#981a1a">%</span> <span style="color:#116644">2</span> <span style="color:#981a1a">==</span> <span style="color:#116644">0</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">number</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"是偶数"</span>);
 ? ? ?  } <span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">number</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"是奇数"</span>);
 ? ? ?  }
 ? ? ? ?
 ?  }
}</span></span>

2.3.6 案例3(手机以旧换新)

需求:张三想买一台7988元的新手机,他的旧手机在二手市场能卖1500元,而手机专卖店推出以旧换新优惠,把他旧手机交给店家,新手机就能够打8折优惠。

为了更省钱,要不要以旧换新?请通过程序告诉张三

首先,我们来简单的分析一下:

分析:

① 计算不使用以旧换新的花费

7988 - 1500

② 计算使用以旧换新的花费

7988 * 0.8

③ 用if语句的哪种格式来实现该需求呢?

if语句格式2

分析完毕后,我们来说一下实现步骤:

实现步骤:

① 定义变量(money1)纪录不使用以旧换新的花费

② 定义变量(money2)纪录使用以旧换新的花费

③ 用if语句格式2进行判断 money1是否大于money2

④ 根据判断结果,给出相应的提示

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">需求:张三想买一台7988元的新手机,他的旧手机在二手市场能卖1500元,而手机专卖店推出以旧换新优惠,</span>
 ? ? ? ?<span style="color:#aa5500">把他的旧手机交给店家,新手机就能够打8折优惠。为了更省钱,要不要以旧换新?请通过程序告诉张三</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfTest03</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//1:定义变量(money1)纪录不使用以旧换新的花费</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">money1</span> <span style="color:#981a1a">=</span> <span style="color:#116644">7988</span> <span style="color:#981a1a">-</span> <span style="color:#116644">1500</span>;
?
 ? ? ? ?<span style="color:#aa5500">//2:定义变量(money2)纪录使用以旧换新的花费</span>
 ? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">money2</span> <span style="color:#981a1a">=</span> <span style="color:#116644">7988</span> <span style="color:#981a1a">*</span> <span style="color:#116644">0.8</span>;
?
 ? ? ? ?<span style="color:#aa5500">//3:用if语句格式2进行判断 money1是否大于money2</span>
 ? ? ? ?<span style="color:#aa5500">//4:根据判断结果,给出相应的提示</span>
 ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">money1</span> <span style="color:#981a1a">></span> <span style="color:#000000">money2</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"使用以旧换新更省钱"</span>);
 ? ? ?  } <span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"不使用以旧换新更省钱"</span>);
 ? ? ?  }
 ? ? ? ?
 ?  }
}</span></span>

2.3.7 案例4(你是青年人吗)

需求:给出你的年龄,通过程序判断你是否属于青年人?

提示:世界卫生组织,经过对全球人体素质和平均寿命进行测定,对年龄划分标准作出了新的规定

首先,我们来简单的分析一下:

分析:

① 如何判断一个年龄是否属于青年人?

年龄要大于等于18,并且小于等于65

② 在Java中如何表示并且的关系呢?

age >=18 && age<=65

&&:是逻辑运算符,用来连接两个结果为boolean类型的表达式,都为true,结果为true,否则为false

分析完毕后,我们来说一下实现步骤:

实现步骤:

① 定义一个int类型的变量来表示你的年龄

② 用if语句格式2进行判断看你是否属于青年人

③ 根据是否是青年人给出不同的提示信息

知道了实现步骤,下面我们到IDEA中去实现一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">需求:给出你的年龄,通过程序判断你是否属于青年人?</span>
 ? ?<span style="color:#aa5500">提示:青年人的年龄范围 18~65</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">IfTest04</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//1:定义一个int类型的变量来表示你的年龄</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">age</span> <span style="color:#981a1a">=</span> <span style="color:#116644">22</span>;
 ? ? ? ?<span style="color:#000000">age</span> <span style="color:#981a1a">=</span> <span style="color:#116644">100</span>;
?
 ? ? ? ?<span style="color:#aa5500">//2:用if语句格式2进行判断看你是否属于青年人</span>
 ? ? ? ?<span style="color:#aa5500">//3:根据是否是青年人给出不同的提示信息</span>
 ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">age</span><span style="color:#981a1a">>=</span><span style="color:#116644">18</span> <span style="color:#981a1a">&&</span> <span style="color:#000000">age</span><span style="color:#981a1a"><=</span><span style="color:#116644">65</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"你是青年人"</span>);
 ? ? ?  } <span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"你不是青年人"</span>);
 ? ? ?  }
 ? ? ? ?
 ?  }
}</span></span>

2.3.8 逻辑运算符

Java中常用的逻辑运算符有如下几种:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">逻辑运算符</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo02</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义变量</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">j</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">k</span> <span style="color:#981a1a">=</span> <span style="color:#116644">30</span>;
?
 ? ? ? ?<span style="color:#aa5500">//逻辑与:&&</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>) <span style="color:#981a1a">&&</span> (<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">k</span>));<span style="color:#aa5500">//false && false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">j</span>) <span style="color:#981a1a">&&</span> (<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">k</span>));<span style="color:#aa5500">//true && false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>) <span style="color:#981a1a">&&</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">k</span>));<span style="color:#aa5500">//false && true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">j</span>) <span style="color:#981a1a">&&</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">k</span>));<span style="color:#aa5500">//true && true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"-----------------------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//逻辑或:||</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">k</span>));<span style="color:#aa5500">//false || false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">j</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">k</span>));<span style="color:#aa5500">//true || false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">k</span>));<span style="color:#aa5500">//false || true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>((<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">j</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><</span><span style="color:#000000">k</span>));<span style="color:#aa5500">//true || true</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"-----------------------"</span>);
?
 ? ? ? ?<span style="color:#aa5500">//逻辑非:!</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#981a1a">!</span>(<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>));<span style="color:#aa5500">//!false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#981a1a">!!</span>(<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>));<span style="color:#aa5500">//!!false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#981a1a">!!!</span>(<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>));<span style="color:#aa5500">//!!!false</span>
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#981a1a">!!!!</span>(<span style="color:#000000">i</span><span style="color:#981a1a">></span><span style="color:#000000">j</span>));<span style="color:#aa5500">//!!!!false</span>
 ? ? ? ?
 ?  }
}</span></span>

2.3.9 三元运算符

格式:关系表达式 ?表达式1 :表达式2;

范例:a > b ? a : b;

执行流程:

  • 首先计算关系表达式的值

  • 如果值为true,表达式1的值就是运算结果

    如果值为false,表达式2的值就是运算结果

了解了三元运算符的执行流程后,下面我们到IDEA中去使用一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">三元运算符</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorDemo03</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">b</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
?
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">max</span> <span style="color:#981a1a">=</span> <span style="color:#000000">a</span> <span style="color:#981a1a">></span> <span style="color:#000000">b</span> <span style="color:#981a1a">?</span> <span style="color:#000000">a</span> : <span style="color:#000000">b</span>;
?
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"较大的值是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">max</span>);
 ?  }
}</span></span>

2.3.10 三元运算符和if语句格式2的区别

总结:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">三元运算符和if语句格式2的区别</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">OperatorTest</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义两个int类型的变量</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">a</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">b</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
?
 ? ? ? ?<span style="color:#aa5500">//需求:获取两个数中的较大值</span>
 ? ? ? ?<span style="color:#aa5500">//if语句实现</span>
<span style="color:#aa5500">// ? ? ?  int max;</span>
<span style="color:#aa5500">// ? ? ?  if (a > b) {</span>
<span style="color:#aa5500">// ? ? ? ? ?  max = a;</span>
<span style="color:#aa5500">// ? ? ?  } else {</span>
<span style="color:#aa5500">// ? ? ? ? ?  max = b;</span>
<span style="color:#aa5500">// ? ? ?  }</span>
<span style="color:#aa5500">// ? ? ?  System.out.println("较大的值是:" + max);</span>
?
 ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">a</span> <span style="color:#981a1a">></span> <span style="color:#000000">b</span>) {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"较大的值是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">a</span>);
 ? ? ?  }<span style="color:#770088">else</span> {
 ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"较大的值是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">b</span>);
 ? ? ?  }
?
 ? ? ? ?<span style="color:#aa5500">//三元运算符实现</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">max</span> <span style="color:#981a1a">=</span> <span style="color:#000000">a</span> <span style="color:#981a1a">></span> <span style="color:#000000">b</span><span style="color:#981a1a">?</span> <span style="color:#000000">a</span>: <span style="color:#000000">b</span>;
 ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"较大的值是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">max</span>);
 ?  }
}</span></span>

总结:

  • 如果if语句控制的内容:是用变量纪录数据值,可以通过三元运算符改进

  • 如果if语句控制的内容是:是通过输出语句直接输出数据值,无法通过三元运算符改进

2.4 选择语句之switch

格式:

格式说明:

执行流程:

了解完switch语句后,下面我们到IDEA中去使用一下:

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
 ? ?<span style="color:#aa5500">switch语句</span>
 <span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">SwitchDemo</span> {
 ? ?<span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
 ? ? ? ?<span style="color:#aa5500">//定义一个int类型的变量用来表示信号灯的状态(1表示红灯,2表示绿灯,3表示黄灯)</span>
 ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">light</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
<span style="color:#aa5500">// ? ? ?  light = 2;</span>
<span style="color:#aa5500">// ? ? ?  light = 3;</span>
<span style="color:#aa5500">// ? ? ?  light = 4;</span>
?
 ? ? ? ?<span style="color:#aa5500">//用switch语句实现交通信号灯案例</span>
 ? ? ? ?<span style="color:#770088">switch</span> (<span style="color:#000000">light</span>) {
 ? ? ? ? ? ?<span style="color:#770088">case</span> <span style="color:#116644">1</span>:
 ? ? ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"红灯停"</span>);
 ? ? ? ? ? ? ? ?<span style="color:#770088">break</span>;
 ? ? ? ? ? ?<span style="color:#770088">case</span> <span style="color:#116644">2</span>:
 ? ? ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"绿灯行"</span>);
 ? ? ? ? ? ? ? ?<span style="color:#770088">break</span>;
 ? ? ? ? ? ?<span style="color:#770088">case</span> <span style="color:#116644">3</span>:
 ? ? ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"黄灯亮了等一等"</span>);
 ? ? ? ? ? ? ? ?<span style="color:#770088">break</span>;
 ? ? ? ? ? ?<span style="color:#770088">default</span>:
 ? ? ? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"交通信号灯故障,请在保证安全的情况下通行"</span>);
 ? ? ? ? ? ? ? ?<span style="color:#770088">break</span>;
 ? ? ?  }
?
 ?  }
}</span></span>

注意事项:

注意:在switch语句中,如果case控制的语句体后面不写break,将出现穿透现象,在不判断下一个case值的情况下,向下运行,直到遇到break,或者整个switch语句结束

文章来源:https://blog.csdn.net/weixin_68522070/article/details/135543850
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。