Java循环语句

1:for循环结构

循环结构有三种语句,分别是:

  1. for循环

  2. while循环

  3. do…while循环

这三种循环可以做相同的事情,当然它们也有小的区别

1.1 for循环结构

这段代码的作用,是用来展示手机信息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.2 案例1(输出数据)

需求:在控制台输出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.3 案例2(求和思想)

需求:求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.4 案例3(求偶数和)

需求:求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>

1.5 案例4(水仙花数)

需求:在控制台输出所有的“水仙花数”

看到这个需求,我们首先要解决的问题就是,什么样的数据是“水仙花数”?它满足如下两个条件:

① 水仙花数是一个三位数

比如: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>

1.6 案例5(统计思想)

需求:统计“水仙花数”一共有多少个,并在控制台输出个数

首先,我们先到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

③ 输出统计变量的值

1.7 案例6(回文数)

需求:输出所有五位数的回文数

看到这个需求,我们首先要解决的问题就是,什么样的数据是“回文数”?

那什么是“回文数”呢?一个数,无论是从左往右读,还是从右往左读,读起来都相同的数,叫做“回文数”

举例:

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>

1.8 案例7(逢七过)

需求:朋友聚会的时候可能会玩一个游戏:逢七过。

规则是:从任意一个数字开始报数,当你要报的数字包含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>

2:while循环结构

2.1 while循环结构

接下来,我们学习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>

执行流程:

2.2 案例1(求奇数和)

需求:求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>

2.3 案例2(珠穆朗玛峰)

需求:世界最高峰珠穆朗玛峰(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>

3:do-while循环结构

3.1 do-while循环结构

格式:

范例:

知道了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>

执行流程:

3.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">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);

4:continue和break

4.1 continue和break

场景一:周一到周五上班,周六日不上班

场景二:成人后一直工作到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>

4.2 综合案例(小芳存钱)

需求:小芳的妈妈每天给她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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/616004.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

能见度监测站在交通中有哪些作用

【TH-NJD10】能见度监测站在交通中的作用主要体现在以下几个方面&#xff1a; 提高交通安全&#xff1a;能见度是影响交通安全的重要因素之一。能见度监测站可以实时监测并发布路面或空中的能见度数据&#xff0c;帮助驾驶员和交通管理部门做出安全决策。在低能见度情况下&…

组合和聚合

不是c的语法要求&#xff0c;是一种建模思想 目录 1.组合 1. 使用 -- 在一个类中创建另外一个类的对象 代码中的解释: 代码结果&#xff1a; 组合&#xff1a; 2. 使用 -- 在一个类中创建另外一个类的指针 2.使用类定义一个指针 -- 不是创建一个对象 3.聚合 1. 使…

Python自动化测试数据驱动解决数据错误

数据驱动将测试数据和测试行为完全分离&#xff0c;实施数据驱动测试步骤如下&#xff1a; A、编写测试脚本&#xff0c;脚本需要支持从程序对象、文件或者数据库读入测试数据&#xff1b; B、将测试脚本使用的测试数据存入程序对象、文件或者数据库等外部介质中&#xff1b;…

密码输入检测 - 华为OD统一考试

OD统一考试(C卷) 分值: 100分 题解: Java / Python / C++ 题目描述 给定用户密码输入流input,输入流中字符 ‘<’ 表示退格,可以清除前一个输入的字符,请你编写程序,输出最终得到的密码字符,并判断密码是否满足如下的密码安全要求。 密码安全要求如下: 密码长度&…

浅研究下 DHCP 和 chrony

服务程序&#xff1a; 1.如果有默认配置&#xff0c;请先备份&#xff0c;再进行修改 2.修改完配置文件&#xff0c;请重启服务或重新加载配置文件&#xff0c;否则不生效 有些软件&#xff0c;安装包的名字和系统里服务程序的名字不一样&#xff08;安装包名字&#xff1a;…

【Spring Cloud】Sentinel流量限流和熔断降级的讲解

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是Java方文山&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的专栏《Spring Cloud》。&#x1f3af;&#x1f3af; &am…

golang 反序列化出现json: cannot unmarshal string into Go value of type model.Phone

项目场景&#xff1a; 今天在项目公关的过程中&#xff0c;需要对interface{}类型进行转换为具体结构体 问题描述 很自然的用到了resultBytes, _ : json.Marshal(result)&#xff0c;然后对resultBytes进行反序列化转换为对应的结构体err : json.Unmarshal(resultBytes, &…

OpenCV-Python(35):BRIEF算法

算法介绍 BRIEF&#xff08;Binary Robust Independent Elementary Features&#xff09;是一种用于计算机视觉中特征点描述子的算法。它是一种二进制描述子&#xff0c;通过比较图像上不同位置的像素值来生成特征点的描述子。 BRIEF算法的基本思想是选取一组固定的像素对&…

12GoF之代理模式

解决问题的思维&#xff1a;AOP 解决问题技术&#xff1a;代理技术 代理技术太麻烦&#xff0c;因此使用框架 Spring AOP框架&#xff08;底层是代理技术&#xff1a;jdk动态daili&#xff0c;cglib&#xff09; 代理模式是GoF23种设计模式之一。属于结构型设计模式。 代理…

做一个个人博客第一步该怎么做?

做一个个人博客第一步该怎么做&#xff1f; 好多零基础的同学们不知道怎么迈出第一步。 那么&#xff0c;就找一个现成的模板学一学呗&#xff0c;毕竟我们是高贵的Ctrl c v 工程师。 但是这样也有个问题&#xff0c;那就是&#xff0c;那些模板都&#xff0c;太&#xff01;…

C 语言每日一题——旋转数组的最小数字

一、题目内容 提供一下该OJ题的链接&#xff1a;旋转数组的最小数字_牛客题霸_牛客网 (nowcoder.com) 二、题目分析 通过示例1可知&#xff0c;我们写代码的目的是在数组中找到一个最大值&#xff0c;并且返回来&#xff1b; 我们很容易的会想到创建一个变量&#xff1a;int…

【Linux】 nohup命令使用

nohup命令 nohup是Linux和Unix系统中的一个命令&#xff0c;其作用是在终端退出时&#xff0c;让进程在后台继续运行。它的全称为“no hang up”&#xff0c;意为“不挂起”。nohup命令可以让你在退出终端或关闭SSH连接后继续运行命令。 nohup 命令&#xff0c;在默认情况下&…

pyx文件编译为pyd/so文件(分别在windows/linux系统下)

Python有以下几种类型的文件&#xff1a; py&#xff1a;Python控制台程序的源代码文件pyx&#xff1a;是Python语言的一个编译扩展&#xff0c;它实际上是Cython语言的源代码文件&#xff08;可以理解为既支持Python语言也支持C/C&#xff09;。pyc&#xff1a;Python字节码文…

DevOps搭建(十六)-Jenkins+K8s部署详细步骤

​ 1、整体部署架构图 2、编写脚本 vi pipeline.yml apiVersion: apps/v1 kind: Deployment metadata:namespace: testname: pipelinelabels:app: pipeline spec:replicas: 2selector:matchLabels:app: pipelinetemplate:metadata:labels:app: pipelinespec:containers:- nam…

深入了解鸿鹄电子招投标系统:Java版企业电子招标采购系统的核心功能

随着市场竞争的加剧和企业规模的扩大&#xff0c;招采管理逐渐成为企业核心竞争力的重要组成部分。为了提高招采工作的效率和质量&#xff0c;我们提出了一种基于电子化平台的解决方案。该方案旨在通过电子化招投标&#xff0c;使得招标采购的质量更高、速度更快&#xff0c;同…

基于JAVA的数据可视化的智慧河南大屏 开源项目

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块三、系统展示四、核心代码4.1 数据模块 A4.2 数据模块 B4.3 数据模块 C4.4 数据模块 D4.5 数据模块 E 五、免责说明 一、摘要 1.1 项目介绍 基于JAVAVueSpringBootMySQL的数据可视化的智慧河南大屏&#xff0c;包含了GDP、…

iOS 调试工具CocoaDebug

1、使用pod工具在项目里面添加CocoaDebug的SDK。 platform :ios, 11.0target ShopService doproject ShopServiceuse_frameworks!pod CocoaDebug, :configurations > [Debug]end2、之后就可以在项目里面看到效果了 APP上显示的是一个黑色背景的小圆圈。 上面39表示调用了39…

使用 vue-json-viewer 工具在界面显示json格式数据

安装vue-json-viewer npm install vue-json-viewer --save 引入&#xff1a; import JsonViewer from vue-json-viewer Vue.use(JsonViewer) 使用&#xff1a; <json-viewer :value"jsonData" show-double-quotes :preview-mode"true" :show-array…

three.js 使用 tweenjs绘制相机运动动画

效果&#xff1a; 代码&#xff1a; <template><div><el-container><el-main><div class"box-card-left"><div id"threejs" style"border: 1px solid red"></div><div class"box-right"…

数据交付变革:研发到产运自助化的转型之路

作者 | Chris 导读 本文讲述为了提升产运侧数据观察、分析、决策的效率&#xff0c;支持业务的快速迭代&#xff0c;移动生态数据研发部对数仓建模与BI工具完成升级&#xff0c;采用宽表建模与TDA平台相结合的方案&#xff0c;一站式自助解决数据应用需求。在此过程中&#xff…