循环结构有三种语句,分别是:
for循环
while循环
do…while循环
这三种循环可以做相同的事情,当然它们也有小的区别
这段代码的作用,是用来展示手机信息1000次。
还提到了这里的几个部分,分别是定义变量,条件判断,控制变量的变化,展示手机信息(可能被多次执行)
而for循环语句也是由这几部分组成。
格式:
格式说明:
初始化语句:这里可以是一条或者多条语句,这些语句用来完成初始化操作(int i=1)
条件判断语句:这里使用一个结果值为boolean类型的表达式,这个表达式能决定是否执行循环体语句(i<=1000)
循环体语句:这里可以是任意语句,这些语句可能被多次执行(展示手机信息)
条件控制语句:这里通常是使用一条语句来改变变量的值,从而达到控制循环是否继续向下执行的效果(i+=1)
范例:
执行流程:
演示:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">for循环结构</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ForDemo</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:#770088">for</span>(<span style="color:#008855">int</span> <span style="color:#000000">i</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>; <span style="color:#000000">i</span><span style="color:#981a1a"><=</span><span style="color:#116644">5</span>; <span style="color:#000000">i</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">"HelloWorld"</span>);
? ? ? }
? }
}</span></span>
需求:在控制台输出1-5的数据
首先,我们来简单的分析一下:
分析:
① 反复进行输出的动作,使用循环结构
② 从1开始到5结束,设置初始化从1开始
③ 从1开始到5结束,设置判断条件为没有到5的时候继续执行,执行到超过5程序结束
④ 从1到5,每次增加1,设置条件控制每次+1
⑤ 将反复进行的事情写入循环结构内部,打印对应数据
分析完毕之后,我们到IDEA中去实现一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:在控制台输出1-5的数据</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ForTest01</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:#116644">1</span>);
? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</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:#116644">3</span>);
? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</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:#116644">5</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">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">5</span>; <span style="color:#000000">i</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:#000000">i</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">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">5</span>; <span style="color:#000000">i</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:#000000">i</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:#aa5500">//它跟在变量的后面表示变量的值+1</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</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:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------------"</span>);
?
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>,<span style="color:#000000">i</span><span style="color:#981a1a">++</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:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"--------------"</span>);
?
? ? ? ?<span style="color:#aa5500">//我要获取数据5-1该怎么办呢?</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span><span style="color:#981a1a">=</span><span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">>=</span><span style="color:#116644">1</span>; <span style="color:#000000">i</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:#000000">i</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">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span><span style="color:#981a1a">=</span><span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">>=</span><span style="color:#116644">1</span>; <span style="color:#000000">i</span><span style="color:#981a1a">--</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></span>
需求:求1-5之间的数据和,并把求和结果在控制台输出
首先,我们先到IDEA中去实现一下,然后再回来总结。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:求1-5之间的数据和,并把求和结果在控制台输出</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ForTest02</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">sum</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#aa5500">//我们通过for循环实现获取数据1-5</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>; <span style="color:#000000">i</span><span style="color:#981a1a"><=</span><span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//i,1,2,3,4,5</span>
<span style="color:#aa5500">// ? ? ? ? ? sum = sum + i;</span>
? ? ? ? ? ?<span style="color:#000000">sum</span> <span style="color:#981a1a">+=</span> <span style="color:#000000">i</span>;
? ? ? ? ? ?<span style="color:#aa5500">/*</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">第一次求和:sum = sum + i = 0 + 1 = 1</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">第二次求和:sum = sum + i = 1 + 2 = 3</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">第三次求和:sum = sum + i = 3 + 3 = 6</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">第四次求和:sum = sum + i = 6 + 4 = 10</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">第五次求和:sum = sum + i = 10 + 5 = 15</span>
? ? ? ? ? ? <span style="color:#aa5500">*/</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">"1-5的求和结果是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">sum</span>);
? }
}</span></span>
最后,我们来总结一下求和思想的实现步骤:
实现步骤:
① 求和的最终结果必须保存起来,需要定义一个变量,用于保存求和的结果,初始值为0
② 从1开始到5结束的数据,使用循环结构完成
③ 将反复进行的事情写入循环结构内部
此处反复进行的事情是将数据 i 加到用于保存最终求和的变量 sum 中
④ 当循环执行完毕时,将最终数据打印出来
需求:求1-100之间的偶数和,并把求和结果在控制台输出
首先,我们来简单的分析一下:
分析:
① 对1-100的数据求和与1-5的数据求和几乎完全一样,仅仅是结束条件不同
② 对1-100的偶数求和,需要对求和操作添加限制条件
③ 限制条件是偶数才参与运算,因此条件应该是判断是否是偶数
④ 当循环执行完毕时,将最终数据打印出来
分析完毕之后,我们到IDEA中去实现一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:求1-100之间的偶数和,并把求和结果在控制台输出</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ForTest03</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:求和的最终结果必须保存起来,需要定义一个变量,用于保存求和的结果,初始值为0</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">sum</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#aa5500">//2:对1-100的数据求和与1-5的数据求和几乎完全一样,仅仅是结束条件不同</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>; <span style="color:#000000">i</span><span style="color:#981a1a"><=</span><span style="color:#116644">100</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//3:对1-100的偶数求和,需要对求和操作添加限制条件,判断是否是偶数</span>
? ? ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">i</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">sum</span> <span style="color:#981a1a">+=</span> <span style="color:#000000">i</span>;
? ? ? ? ? }
? ? ? }
?
? ? ? ?<span style="color:#aa5500">//4:当循环执行完毕时,将最终数据打印出来</span>
? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"1-100之间的偶数和是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">sum</span>);
? }
}</span></span>
需求:在控制台输出所有的“水仙花数”
看到这个需求,我们首先要解决的问题就是,什么样的数据是“水仙花数”?它满足如下两个条件:
① 水仙花数是一个三位数
比如:111 222 333 370 371 520 999
② 水仙花数的个位、十位、百位的数字立方和等于原数
比如:
知道了什么是水仙花数后,我们再来回顾一下如何获取一个数据的个位,十位,百位:
假设一个三位数为x,则
个位:x%10
十位:x/10%10
百位:x/100%10
了解了水仙花数相关的基础知识后,下面我们来说一下这个案例的实现步骤:
实现步骤:
① 获取三位数,通过循环实现
② 获取每一个三位数的个位,十位,百位
③ 判断该三位数是否是水仙花数
④ 输出水仙花数
知道了实现步骤,下面我们到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">ForTest04</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:获取三位数,通过循环实现</span>
<span style="color:#aa5500">// ? ? ? for (int i=100; i<=999; i++) {</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? }</span>
?
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">100</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><</span> <span style="color:#116644">1000</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//2:获取每一个三位数的个位,十位,百位</span>
? ? ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">=</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">shi</span> <span style="color:#981a1a">=</span> <span style="color:#000000">i</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">i</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">//3:判断该三位数是否是水仙花数</span>
? ? ? ? ? ?<span style="color:#770088">if</span> ((<span style="color:#000000">ge</span> <span style="color:#981a1a">*</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">*</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">+</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">*</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">*</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">+</span> <span style="color:#000000">bai</span> <span style="color:#981a1a">*</span> <span style="color:#000000">bai</span> <span style="color:#981a1a">*</span> <span style="color:#000000">bai</span>) <span style="color:#981a1a">==</span> <span style="color:#000000">i</span>) {
? ? ? ? ? ? ? ?<span style="color:#aa5500">//4:输出水仙花数</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></span>
需求:统计“水仙花数”一共有多少个,并在控制台输出个数
首先,我们先到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">ForTest05</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">//定义一个统计变量,用于数据的统计,初始化值为0</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">count</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">100</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><</span> <span style="color:#116644">1000</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">=</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">shi</span> <span style="color:#981a1a">=</span> <span style="color:#000000">i</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">i</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:#770088">if</span> ((<span style="color:#000000">ge</span> <span style="color:#981a1a">*</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">*</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">+</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">*</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">*</span> <span style="color:#000000">shi</span> <span style="color:#981a1a">+</span> <span style="color:#000000">bai</span> <span style="color:#981a1a">*</span> <span style="color:#000000">bai</span> <span style="color:#981a1a">*</span> <span style="color:#000000">bai</span>) <span style="color:#981a1a">==</span> <span style="color:#000000">i</span>) {
<span style="color:#aa5500">// ? ? ? ? ? ? ? System.out.println(i);</span>
? ? ? ? ? ? ? ?<span style="color:#aa5500">//1,2,3,...</span>
? ? ? ? ? ? ? ?<span style="color:#000000">count</span><span style="color:#981a1a">++</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">count</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"个"</span>);
? }
}</span></span>
最后,我们来总结一下统计思想的实现步骤:
实现步骤:
① 定义统计变量count,初始化值为0
② 在判定水仙花数的过程中,满足条件不再输出,更改为修改count的值,使count+1
③ 输出统计变量的值
需求:输出所有五位数的回文数
看到这个需求,我们首先要解决的问题就是,什么样的数据是“回文数”?
那什么是“回文数”呢?一个数,无论是从左往右读,还是从右往左读,读起来都相同的数,叫做“回文数”
举例:
10101,12321:是回文数
12345:不是回文数
规律:
个位 = 万位
十位 = 千位
和百位无关
了解了回文数相关的基础知识后,我们来说一下这个案例的实现步骤:
实现步骤:
① 获取五位数,通过循环实现
② 获取每一个五位数的个位,十位,千位,万位
③ 判断该五位数是否是回文数
④ 输出回文数
知道了实现步骤,下面我们到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">ForTest06</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:获取五位数,通过循环实现</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10000</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><</span> <span style="color:#116644">100000</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//2:获取每一个五位数的个位,十位,千位,万位</span>
? ? ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">ge</span> <span style="color:#981a1a">=</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">shi</span> <span style="color:#981a1a">=</span> <span style="color:#000000">i</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">qian</span> <span style="color:#981a1a">=</span> <span style="color:#000000">i</span> <span style="color:#981a1a">/</span> <span style="color:#116644">1000</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span>;
? ? ? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">wan</span> <span style="color:#981a1a">=</span> <span style="color:#000000">i</span> <span style="color:#981a1a">/</span> <span style="color:#116644">10000</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span>;
?
? ? ? ? ? ?<span style="color:#aa5500">//3:判断该五位数是否是回文数</span>
? ? ? ? ? ?<span style="color:#770088">if</span> ((<span style="color:#000000">ge</span> <span style="color:#981a1a">==</span> <span style="color:#000000">wan</span>) <span style="color:#981a1a">&&</span> (<span style="color:#000000">shi</span> <span style="color:#981a1a">==</span> <span style="color:#000000">qian</span>)) {
? ? ? ? ? ? ? ?<span style="color:#aa5500">//4:输出回文数</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:#aa5500">//练习:统计五位数的回文数个数,并在控制台输出个数</span>
?
? }
}</span></span>
需求:朋友聚会的时候可能会玩一个游戏:逢七过。
规则是:从任意一个数字开始报数,当你要报的数字包含7或者是7的倍数时都要说:过。
为了帮助大家更好的玩这个游戏,这里我们直接在控制台打印出1-100之间的满足逢七必过规则的数据。
这样,大家将来在玩游戏的时候,就知道哪些数据要说:过。
首先,我们来简单的分析一下:
分析:
① 数据的范围1-100之间,用循环很容易实现
② 要满足的条件是:数字包含7(个位是7,或者十位是7),或者是7的倍数
假如有一个数据:x
判断个位为7:x%10 == 7
判断十位为7:x/10%10 == 7
判断是7的倍数:x%7 == 0
分析完毕后,我们来说一下实现步骤:
实现步骤:
① 获取1-100之间的数据,通过循环实现
② 判断数字包含7或者是7的倍数
判断数据:要么个位是7,要么十位是7,要么能够被7整除
③ 在控制台输出满足条件的数据
知道了实现步骤,下面我们到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">ForTest07</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:获取1-100之间的数据,通过循环实现</span>
? ? ? ?<span style="color:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">100</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//2:判断数字包含7或者是7的倍数(判断数据:要么个位是7,要么十位是7,要么能够被7整除)</span>
? ? ? ? ? ?<span style="color:#770088">if</span> ((<span style="color:#000000">i</span> <span style="color:#981a1a">%</span> <span style="color:#116644">10</span> <span style="color:#981a1a">==</span> <span style="color:#116644">7</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</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:#981a1a">==</span> <span style="color:#116644">7</span>) <span style="color:#981a1a">||</span> (<span style="color:#000000">i</span> <span style="color:#981a1a">%</span> <span style="color:#116644">7</span> <span style="color:#981a1a">==</span> <span style="color:#116644">0</span>)) {
? ? ? ? ? ? ? ?<span style="color:#aa5500">//3:在控制台输出满足条件的数据</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></span>
接下来,我们学习while循环结构语句
格式:
范例:
知道了while循环的格式后,下面我们到IDEA中去演示一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">while循环结构</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">WhileDemo</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">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
? ? ? ?<span style="color:#770088">while</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><=</span><span style="color:#116644">5</span>) {
? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"HelloWorld"</span>);
? ? ? ? ? ?<span style="color:#000000">i</span><span style="color:#981a1a">++</span>;
? ? ? }
? }
}</span></span>
执行流程:
需求:求1-100之间的奇数和,并把求和结果在控制台输出
首先,我们来简单的分析一下:
分析:
① 对1-100的奇数求和,和前面讲解的偶数求和几乎是完全一样,仅仅是判断条件不同
② 判断条件是奇数才参与运算,因此条件应该是判断是否是奇数
③ 当循环执行完毕时,将最终数据打印出来
分析完毕之后,我们到IDEA中去实现一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:求1-100之间的奇数和,并把求和结果在控制台输出</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">WhileTest01</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">//定义求和变量,初始化值为0</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">sum</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#aa5500">//获取1-100之间的数据</span>
<span style="color:#aa5500">// ? ? ? int i = 1;</span>
<span style="color:#aa5500">// ? ? ? while (i<=100) {</span>
<span style="color:#aa5500">// ? ? ? ? ? //判断数据是否是奇数</span>
<span style="color:#aa5500"> ? ? ? ? ? if(i%2 != 0) {</span>
<span style="color:#aa5500"> ? ? ? ? ? ? ? sum += i;</span>
<span style="color:#aa5500"> ? ? ? ? ? }</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? ? ? if(i%2 == 1) {</span>
<span style="color:#aa5500">// ? ? ? ? ? ? ? sum += i;</span>
<span style="color:#aa5500">// ? ? ? ? ? }</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? ? ? i++;</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">1</span>;
? ? ? ?<span style="color:#770088">while</span> (<span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">100</span>) {
? ? ? ? ? ?<span style="color:#000000">sum</span> <span style="color:#981a1a">+=</span> <span style="color:#000000">i</span>;
? ? ? ? ? ?<span style="color:#000000">i</span> <span style="color:#981a1a">+=</span> <span style="color:#116644">2</span>; <span style="color:#aa5500">//1,3,5,7...99,101</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">"1-100之间的奇数和是:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">sum</span>);
? }
}</span></span>
需求:世界最高峰珠穆朗玛峰(8848.86m),我现在有一张足够大的纸张,厚度为:0.001m。请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?
首先,我们来简单的分析一下:
分析:
① 因为要反复折叠,所以要使用循环,该选for循环还是while循环呢?
小技巧:循环次数明确,使用for循环;循环次数不明确,使用while循环
这里不知道折叠多少次,这种情况下更适合使用while循环
② 纸张每折叠一次,厚度加倍,因此要先定义纸张厚度,在循环中使厚度加倍
③ 什么时候就停止折叠了呢?直到纸张的厚度大于等于珠峰的高度,反言之,就要折叠
④ 要求的是统计折叠次数,所以会用到统计思想
分析完毕后,我们来说一下实现步骤:
实现步骤:
① 定义统计变量,初始化值为0
② 定义纸张厚度变量和珠峰高度变量
③ 用while循环实现反复折叠,判断条件是纸张厚度小于珠峰高度
④ 循环体中要做两件事情:一是纸张厚度加倍,二是统计变量+1
⑤ 当循环结束,输出统计变量的值
知道了实现步骤,下面我们到IDEA中去实现一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:世界最高峰珠穆朗玛峰(8848.86m),我现在有一张足够大的纸张,厚度为:0.001m。</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">WhileTest02</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:定义统计变量,初始化值为0</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">count</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#aa5500">//2:定义纸张厚度变量和珠峰高度变量</span>
? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">paper</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0.001</span>;
? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">zf</span> <span style="color:#981a1a">=</span> <span style="color:#116644">8848.86</span>;
?
? ? ? ?<span style="color:#aa5500">//3:用while循环实现反复折叠,判断条件是纸张厚度小于珠峰高度</span>
? ? ? ?<span style="color:#770088">while</span> (<span style="color:#000000">paper</span> <span style="color:#981a1a"><</span> <span style="color:#000000">zf</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//4:循环体中要做两件事情:一是纸张厚度加倍,二是统计变量+1</span>
? ? ? ? ? ?<span style="color:#000000">paper</span> <span style="color:#981a1a">*=</span> <span style="color:#116644">2</span>;
?
? ? ? ? ? ?<span style="color:#000000">count</span><span style="color:#981a1a">++</span>;
? ? ? }
?
? ? ? ?<span style="color:#aa5500">//5:当循环结束,输出统计变量的值</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">count</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"次"</span>);
? }
}</span></span>
格式:
范例:
知道了do-while循环的格式后,下面我们到IDEA中去演示一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">do...while循环结构</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">DoWhileDemo</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">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
? ? ? ?<span style="color:#770088">do</span> {
? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"HelloWorld"</span>);
? ? ? ? ? ?<span style="color:#000000">i</span><span style="color:#981a1a">++</span>;
? ? ? } <span style="color:#770088">while</span> (<span style="color:#000000">i</span><span style="color:#981a1a"><=</span><span style="color:#116644">5</span>);
? }
}</span></span>
执行流程:
<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">LoopTest</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">// ? ? ? //for循环</span>
<span style="color:#aa5500">// ? ? ? for (int i = 3; i < 3; i++) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("我爱Java");</span>
<span style="color:#aa5500">// ? ? ? }</span>
<span style="color:#aa5500">// ? ? ? System.out.println("--------");</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? //while循环</span>
<span style="color:#aa5500">// ? ? ? int j = 3;</span>
<span style="color:#aa5500">// ? ? ? while (j < 3) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("我爱Java");</span>
<span style="color:#aa5500">// ? ? ? ? ? j++;</span>
<span style="color:#aa5500">// ? ? ? }</span>
<span style="color:#aa5500">// ? ? ? System.out.println("--------");</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? //do...while循环</span>
<span style="color:#aa5500">// ? ? ? int k = 3;</span>
<span style="color:#aa5500">// ? ? ? do {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("我爱Java");</span>
<span style="color:#aa5500">// ? ? ? ? ? k++;</span>
<span style="color:#aa5500">// ? ? ? } while (k < 3);</span>
?
<span style="color:#aa5500">// ? ? ? //for循环</span>
<span style="color:#aa5500">// ? ? ? for (int i = 1; i < 3; i++) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("我爱Java");</span>
<span style="color:#aa5500">// ? ? ? }</span>
<span style="color:#aa5500"> ? ? ? System.out.println(i);</span>
<span style="color:#aa5500">// ? ? ? System.out.println("--------");</span>
<span style="color:#aa5500">//</span>
<span style="color:#aa5500">// ? ? ? //while循环</span>
<span style="color:#aa5500">// ? ? ? int j = 1;</span>
<span style="color:#aa5500">// ? ? ? while (j < 3) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("我爱Java");</span>
<span style="color:#aa5500">// ? ? ? ? ? j++;</span>
<span style="color:#aa5500">// ? ? ? }</span>
<span style="color:#aa5500">// ? ? ? System.out.println(j);</span>
<span style="color:#aa5500">// ? ? ? System.out.println("--------");</span>
?
? ? ? ?<span style="color:#aa5500">//死循环</span>
<span style="color:#aa5500">// ? ? ? for (;;) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("for...");</span>
<span style="color:#aa5500">// ? ? ? }</span>
?
<span style="color:#aa5500">// ? ? ? while (true) {</span>
<span style="color:#aa5500">// ? ? ? ? ? System.out.println("while...");</span>
<span style="color:#aa5500">// ? ? ? }</span>
?
? ? ? ?<span style="color:#770088">do</span> {
? ? ? ? ? ?<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"do...while..."</span>);
? ? ? } <span style="color:#770088">while</span> (<span style="color:#221199">true</span>);
?
? }
}</span></span>
三种循环语句的区别:
for循环和while循环先判断条件是否成立,然后决定是否执行循环体(先判断后执行)
do...while循环先执行一次循环体,然后判断条件是否成立,是否继续执行循环体(先执行后判断)
for和while的区别:
条件控制语句所控制的自增变量,在for循环结束后,就不可以继续使用了
条件控制语句所控制的自增变量,在while循环结束后,还可以继续使用
死循环格式:
for(;;){}
while(true) {}
do{}while(true);
场景一:周一到周五上班,周六日不上班
场景二:成人后一直工作到60岁,60岁之后就可以退休养老了
刚才的场景中,重点介绍了两个跳转关键字:
continue 用在循环中,基于条件控制,跳过某次循环体内容的执行,继续下一次的执行
break 用在循环中,基于条件控制,终止循环体内容的执行,也就是说结束当前的整个循环
了解了这两个跳转关键字之后,下面我们到IDEA中去体验一下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? <span style="color:#aa5500">continue:用在循环中,基于条件控制,跳过某次循环体内容的执行,继续下一次的执行</span>
? <span style="color:#aa5500">break:用在循环中,基于条件控制,终止循环体内容的执行,也就是说结束当前的整个循环</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ControlDemo</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:#770088">for</span> (<span style="color:#008855">int</span> <span style="color:#000000">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>; <span style="color:#000000">i</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">5</span>; <span style="color:#000000">i</span><span style="color:#981a1a">++</span>) {
? ? ? ? ? ?<span style="color:#770088">if</span> (<span style="color:#000000">i</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:#aa5500">// ? ? ? ? ? ? ? continue;</span>
? ? ? ? ? ? ? ?<span style="color:#770088">break</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></span>
需求:小芳的妈妈每天给她2.5元钱,她都会存起来,但是,每当这一天是存钱的第5天或者5的倍数的话,
她都会花去6元钱,请问,经过多少天,小芳才可以存到100元钱。
关于这个案例呢,我们到IDEA中一边分析,一边实现:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/*</span>
? ?<span style="color:#aa5500">需求:小芳的妈妈每天给她2.5元钱,她都会存起来,但是,每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱,请问,经过多少天,小芳才可以存到100元钱。</span>
<span style="color:#aa5500">*/</span>
<span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ControlTest</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">//小芳的妈妈每天给她2.5元钱</span>
? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">dayMoney</span> <span style="color:#981a1a">=</span> <span style="color:#116644">2.5</span>;
?
? ? ? ?<span style="color:#aa5500">//她都会存起来,涉及到了求和思想,定义求和变量,初始化值为0</span>
? ? ? ?<span style="color:#008855">double</span> <span style="color:#000000">sumMoney</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;
?
? ? ? ?<span style="color:#aa5500">//存到100元钱</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">result</span> <span style="color:#981a1a">=</span> <span style="color:#116644">100</span>;
?
? ? ? ?<span style="color:#aa5500">//定义一个统计变量,用来纪录存钱的天数,初始化值为1</span>
? ? ? ?<span style="color:#008855">int</span> <span style="color:#000000">dayCount</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;
?
? ? ? ?<span style="color:#aa5500">//因为不知道要多少天才能够存到100元,所以,这里我们采用死循环来实现,当存到100元的时候,通过break跳转语句让循环结束</span>
? ? ? ?<span style="color:#770088">while</span> (<span style="color:#221199">true</span>) {
? ? ? ? ? ?<span style="color:#aa5500">//存钱</span>
? ? ? ? ? ?<span style="color:#000000">sumMoney</span> <span style="color:#981a1a">+=</span> <span style="color:#000000">dayMoney</span>;
?
? ? ? ? ? ?<span style="color:#aa5500">//判断存的钱是否大于等于100了,如果是,就退出循环</span>
? ? ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">sumMoney</span> <span style="color:#981a1a">>=</span> <span style="color:#000000">result</span>) {
? ? ? ? ? ? ? ?<span style="color:#770088">break</span>;
? ? ? ? ? }
?
? ? ? ? ? ?<span style="color:#aa5500">//每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱</span>
? ? ? ? ? ?<span style="color:#770088">if</span>(<span style="color:#000000">dayCount</span><span style="color:#981a1a">%</span><span style="color:#116644">5</span> <span style="color:#981a1a">==</span> <span style="color:#116644">0</span>) {
? ? ? ? ? ? ? ?<span style="color:#000000">sumMoney</span> <span style="color:#981a1a">-=</span> <span style="color:#116644">6</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">dayCount</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"天花了6元"</span>);
? ? ? ? ? }
?
? ? ? ? ? ?<span style="color:#000000">dayCount</span><span style="color:#981a1a">++</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">dayCount</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"天存了100元"</span>);
? }
}</span></span>