Java序谈之循环结构

来源:互联网 发布:上海银行淘宝金卡功能 编辑:程序博客网 时间:2024/06/06 16:48

循环结构中有哪些循环语句?

1、while(循环条件){循环体}

2、do{循环体 }while(循环条件)

3、for(初始化;循环条件;自增){循环体}


while循环执行规则是什么?

满足while后的循环条件后,才会执行循环体。

1、循环增量 2、循环条件 3、循环体(循环增量要改变)

while循环中的累加思想

int sum=0;int num1=0;while(num1<11) {sum=sum+num1;num1++;}System.out.println(sum);

while循环的实例题目

一个死循环

int num=0;while(true) {//这里写了一个出口,让程序结束执行。if(num==5) {break;}System.out.println("你真美");num++;}
题目:银行定期存款 5年及以上利率7.5% 否则利率 4.5%,输入一个存款年限  本金为10000 计算本金加利息

System.out.print("请输入要存款年数:");Scanner scanner3=new Scanner(System.in);int year=scanner3.nextInt();Double money=10000.0;int temp=1;if(year>=5) {while(temp<=year){money=money+money*0.075;temp++;}}else if(year<5) {while(temp<year) {money=money+money*0.045;temp++;}}System.out.println(money);

do while循环的执行规则是什么?

先执行循环体,然后判断循环条件

1、循环增量初始化 2、循环体 (注意:循环增量要发生改变)3、循环条件

do while循环的实例

题目:从周一开始打印 并询问明天上班不 输入上班继续打印 输入不上班停止程序

int num5=1;Scanner scanner=new Scanner(System.in);String work="";do {System.out.println("星期"+num5);System.out.println("明天上班不?输入上班或者不上班:");work=scanner.nextLine();num5++;}while(work.equals("上班")&&num5<8);scanner.close();

For循环的结构是什么?

For(循环增量;循环条件;循环自增){循环体}

For循环和while循环的区别是什么?

while循环的循环增量定义在循环外,可以在循环外被使用。

For循环的循环增量定义在循环内,循环外不可以使用。

For循环的实例

1、Java的计数器思想

题目:使用for循环 1-100中 是7的倍数 的个数

int num=0;for(int i=1;i<101;i++) {if(i%7==0){num++;//记录符合要求的次数}}

2、用For循环做出以下效果,四行四列*符号列表

思路:外循环控制行数,内循环控制每一行的元素个数。

for(int x=0;x<4;x++) {for(int y=0;y<4;y++){System.out.print("*");}System.out.println();}

****************
3、用For循环做出直角三角形向下和向上的效果

口诀:尖头向下,改变内循环初始化值

for(int x=0;x<4;x++) {for(int y=x;y<4;y++) {System.out.print("*");}System.out.println();}
****************
          尖头向上改变内循环条件

for(int x=0;x<5;x++) {for(int y=0;y<x+1;y++) {System.out.print("*");}System.out.println();}

***************

4、用For循环做出九九乘法表和反序九九乘法表

正序九九乘法表

for(int x=1;x<10;x++) {for(int y=1;y<x+1;y++){System.out.print(x+"*"+y+"="+x*y+"\t");}System.out.println();}

1*1=12*1=22*2=43*1=33*2=63*3=94*1=44*2=84*3=124*4=165*1=55*2=105*3=155*4=205*5=256*1=66*2=126*3=186*4=246*5=306*6=367*1=77*2=147*3=217*4=287*5=357*6=427*7=498*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=649*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81------
反序九九乘法表

for(int x=9;x>0;x--) {for(int y=x;y>0;y--){System.out.print(x+"*"+y+"="+x*y+"\t");}System.out.println();}

9*9=819*8=729*7=639*6=549*5=459*4=369*3=279*2=189*1=98*8=648*7=568*6=488*5=408*4=328*3=248*2=168*1=87*7=497*6=427*5=357*4=287*3=217*2=147*1=76*6=366*5=306*4=246*3=186*2=126*1=65*5=255*4=205*3=155*2=105*1=54*4=164*3=124*2=84*1=43*3=93*2=63*1=32*2=42*1=21*1=1


5、用For循环做出尖头朝上正三角新和尖头朝下正三角形

尖头朝上正三角形

for(int x=0;x<4;x++) {for(int y=x;y<4;y++) {System.out.print("-");}for(int z=0;z<x+1;z++) {System.out.print("* ");}System.out.println();}

----* ---* * --* * * -* * * * 

尖头朝下正三角形

for(int x=0;x<4;x++) {for(int y=0;y<x+1;y++) {System.out.print("-");}for(int z=x;z<4;z++) {System.out.print("* ");}System.out.println();}

-* * * * --* * * ---* * ----* 








原创粉丝点击