Linux运维系统工程师与java基础学习系列-6
Java天生驕傲系列-6
?程序流程控制(續(xù))
? ?循環(huán)結(jié)構(gòu)
? ? ? ??代表語句:while, do while, for
? ? While語句格式:
? ? ? ? while(條件表達(dá)式)
? ? ? ? ? ? {
? ? ? ? ? ? ??執(zhí)行語句;
? ? ? ? ? ??}
? ? ? ? ? ? ?牛刀小試:
???????????package test.myeclipse;
? ? ? ? ? ? ?publicclass test1 {
?
??????????????publicstaticvoid main(String[] args) {
???????????????????????int x=1;
? ? ? ? ? ? ? ? ? ? ? ??while (x<4)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("x="+x);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x++;
??????????
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
??????
??????
??????
?
? ? ? ? ? ? ? ? }
? ? ? ? ? ??}
????????????????????????運(yùn)行結(jié)果: x=1
x=2
? ? ? ? ? ? ? ? ? ? ? ? x=3
???do while 語句格式:
?????????????????????? do
?????????????????????? {
????????????????????????????執(zhí)行語句;
?????????????}while(條件表達(dá)式);
????????????????????????????牛刀小試:
package test.myeclipse;?
publicclass test1 {
???????????????????????????????publicstaticvoid main(String[] args) {
??????????????????????????????? int x=1;
??????????????????????????????? do
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
?????????????????????????????????? System.out.println("x="+x);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }while(x<4);
??????
??????
??????
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? ? }
運(yùn)行結(jié)果:
x=1
x=2
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x=3
??for語句格式:
? ? for(初始化表達(dá)式;循環(huán)條件表達(dá)式;循環(huán)后的操作表達(dá)式)
? ? ? ?{
? ? ? ? ? ? ?執(zhí)行語句;
? ? ? ?}
? ? ? ? ? ? ?牛刀小試:
????????????????package test.myeclipse;
? ? ? ? ? ? ? ? ??publicclass test1 {? ?
???????????????????publicstaticvoid main(String[] args) {
?????????????????????for (int x=1;x<4;x++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println("x="+x);
? ? ? ? ? ? ? ? ? ? ? ? }?????
?
? ? ? ? ? ? ? ? ? ? ?}
?
?????????????????}
? ? ? ? ? ? ? ? ?運(yùn)行結(jié)果:
? ? ? ? ? ? ? ? ? ? x=1
? ? ? ? ? ? ? ? ? ??x=2
? ? ? ? ? ? ? ? ? ? x=3
? ?
? ? ? 練習(xí)一例:
????? ????package test.myeclipse;?
publicclass test1 {?
?????? ?????????? publicstaticvoid main(String[]args) {
?????????? ?????????? int x=1;
?????????? ?????????? for (System.out.println("a");x<3;System.out.println("c"))
?????????? ?????????? {
????????????? ?????????? System.out.println("d");
????????????? ?????????? x++;
?????????? ?????????? }?????
?
??? ????????????? }
?
? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? 運(yùn)行結(jié)果:
a
d
c
d
c
?語句嵌套
????? 牛刀小試1:
????????????????? package test.myeclipse;?
? ? ? ? ? ?publicclass test1 {
?
? ? ? ? ? ? ? ? ? ? ?publicstaticvoid main(String[]args) {
??????????
? ? ? ? ? ? ? ? ? ? ? ??for (int x=0;x<3;x++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ??for(int y=0;y<4;y++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("ok");
? ? ? ? ? ? ? ? ? ? ? ? ? ? }?????
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?}
?
? ? ? ? ? ?}
????運(yùn)行結(jié)果:
????? ? ? ? ? ?ok
????ok
????ok
????ok
????ok
????ok
????ok
????ok
????ok
????ok
????ok
????ok
? ? ? ?牛刀小試2:
????????????????package test.myeclipse;
? ? ? ? ? ? ? ? public ?class test1 {?
? ? ? ? ? ? ? ? ? ??publicstaticvoid main(String[]args) {
? ? ? ? ? ? ? ? ? ? ?int z=5;
? ? ? ? ? ? ? ? ? ? ?for (int x=0;x<5;x++)
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ??for(int y=0;y<z;y++)
? ? ? ? ? ? ? ? ? ? ? ? {
????????????????? ??? ??? System.out.print("*");
? ? ? ? ? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println();
? ? ? ? ? ? ? ? ? ? ? ? ? ?z--;
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ?}
????????運(yùn)行結(jié)果:
?????????????? *****
????????****
????????***
????????**
????????*
????????????優(yōu)化一下:
????? ????package test.myeclipse;?
??????????publicclass test1 {?
?????? ????????publicstaticvoid main(String[] args) {
?????????? ????????for (int x=0;x<5;x++)
?????????????????? {
????????????? ????????for(int y=x;y<5;y++)
????????????? ????? ??{
? ? ? ? ? ? ? ? ? ? ? ? ?System.out.print("*");
? ? ? ? ? ? ? ? ? ? }??
? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println();
?????????????
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ?}
????運(yùn)行結(jié)果:
?????????? *****
????****
????***
????**
????? ? ? ?*
未完待續(xù)。。。。。。
轉(zhuǎn)載于:https://blog.51cto.com/2489843/1541986
總結(jié)
以上是生活随笔為你收集整理的Linux运维系统工程师与java基础学习系列-6的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过流进行字符集编码转换
- 下一篇: VMware Workstation中L