欢迎来到天天文库
浏览记录
ID:28867305
大小:68.50 KB
页数:9页
时间:2018-12-14
《浙大java语言程序设计编程详解6》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验7循环结构程序的设计(二)程序填空,不要改变与输入输出有关的语句。1.求1-1/2+1/3-1/4+……输入一个正整数repeat(02、repeat;inti,n,flag;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0;flag=1;for(i=1;i<=n;i++){sum+=flag*1.0f/i;flag=-flag;}System.out.println((long)(sum*10000+0.5)/10000.);}}}2.求1-1/3+1/5-1/7+3、……输入一个正整数repeat(04、doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*1005、00+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、il.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母7、是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
2、repeat;inti,n,flag;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0;flag=1;for(i=1;i<=n;i++){sum+=flag*1.0f/i;flag=-flag;}System.out.println((long)(sum*10000+0.5)/10000.);}}}2.求1-1/3+1/5-1/7+
3、……输入一个正整数repeat(04、doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*1005、00+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、il.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母7、是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
4、doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*100
5、00+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、il.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母7、是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
6、il.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母
7、是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
8、publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
此文档下载收益归作者所有