欢迎来到天天文库
浏览记录
ID:28830919
大小:60.00 KB
页数:7页
时间:2018-12-14
《浙大java语言程序设计编程详解5》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验6循环结构程序的设计(一)程序填空,不要改变与输入输出有关的语句。1.求1+1/2+1/3+……+1/n输入一个正整数repeat(02、tri,repeat;inti,n;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0; //注意这条赋初值语句的位置for(i=1;i<=n;i++){sum+=1.0f/i;}System.out.println((int)(sum*1000+0.5)/1000.);}}}2.求n!输入一3、个正整数repeat(04、t=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/fact=1;for(i=1;i<=n;i++)fact*=i;System.out.println(fact);}}}1.求x的n次幂输入一个正整数repeat(05、出2.251024.0importjava.util.Scanner;publicclassTest40003{publicstaticvoidmain(String[]args){intri,repeat;inti,n;doublex,mypow;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){x=in.nextDouble();n=in.nextInt();/*---------------6、-----*/mypow=1;for(i=1;i<=n;i++)mypow*=x;System.out.println(mypow);}}}1.求1+1/3+1/5+1/7+……输入一个正整数repeat(07、st40005{publicstaticvoidmain(String[]args){intri,repeat;inti,n,temp;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/temp=1;sum=0;for(i=1;i<=n;i++){sum+=1.0f/temp;temp+=2;}Syst8、em.out.println(sum);}}}解法二:temp=2*n-1;sum=0;for(i=1;i<=temp;i+=2)sum+=1.0f/i;1.摄氏温度和华氏温度转换表输入一个正整数repeat(0
2、tri,repeat;inti,n;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0; //注意这条赋初值语句的位置for(i=1;i<=n;i++){sum+=1.0f/i;}System.out.println((int)(sum*1000+0.5)/1000.);}}}2.求n!输入一
3、个正整数repeat(04、t=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/fact=1;for(i=1;i<=n;i++)fact*=i;System.out.println(fact);}}}1.求x的n次幂输入一个正整数repeat(05、出2.251024.0importjava.util.Scanner;publicclassTest40003{publicstaticvoidmain(String[]args){intri,repeat;inti,n;doublex,mypow;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){x=in.nextDouble();n=in.nextInt();/*---------------6、-----*/mypow=1;for(i=1;i<=n;i++)mypow*=x;System.out.println(mypow);}}}1.求1+1/3+1/5+1/7+……输入一个正整数repeat(07、st40005{publicstaticvoidmain(String[]args){intri,repeat;inti,n,temp;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/temp=1;sum=0;for(i=1;i<=n;i++){sum+=1.0f/temp;temp+=2;}Syst8、em.out.println(sum);}}}解法二:temp=2*n-1;sum=0;for(i=1;i<=temp;i+=2)sum+=1.0f/i;1.摄氏温度和华氏温度转换表输入一个正整数repeat(0
4、t=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/fact=1;for(i=1;i<=n;i++)fact*=i;System.out.println(fact);}}}1.求x的n次幂输入一个正整数repeat(05、出2.251024.0importjava.util.Scanner;publicclassTest40003{publicstaticvoidmain(String[]args){intri,repeat;inti,n;doublex,mypow;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){x=in.nextDouble();n=in.nextInt();/*---------------6、-----*/mypow=1;for(i=1;i<=n;i++)mypow*=x;System.out.println(mypow);}}}1.求1+1/3+1/5+1/7+……输入一个正整数repeat(07、st40005{publicstaticvoidmain(String[]args){intri,repeat;inti,n,temp;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/temp=1;sum=0;for(i=1;i<=n;i++){sum+=1.0f/temp;temp+=2;}Syst8、em.out.println(sum);}}}解法二:temp=2*n-1;sum=0;for(i=1;i<=temp;i+=2)sum+=1.0f/i;1.摄氏温度和华氏温度转换表输入一个正整数repeat(0
5、出2.251024.0importjava.util.Scanner;publicclassTest40003{publicstaticvoidmain(String[]args){intri,repeat;inti,n;doublex,mypow;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){x=in.nextDouble();n=in.nextInt();/*---------------
6、-----*/mypow=1;for(i=1;i<=n;i++)mypow*=x;System.out.println(mypow);}}}1.求1+1/3+1/5+1/7+……输入一个正整数repeat(07、st40005{publicstaticvoidmain(String[]args){intri,repeat;inti,n,temp;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/temp=1;sum=0;for(i=1;i<=n;i++){sum+=1.0f/temp;temp+=2;}Syst8、em.out.println(sum);}}}解法二:temp=2*n-1;sum=0;for(i=1;i<=temp;i+=2)sum+=1.0f/i;1.摄氏温度和华氏温度转换表输入一个正整数repeat(0
7、st40005{publicstaticvoidmain(String[]args){intri,repeat;inti,n,temp;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/temp=1;sum=0;for(i=1;i<=n;i++){sum+=1.0f/temp;temp+=2;}Syst
8、em.out.println(sum);}}}解法二:temp=2*n-1;sum=0;for(i=1;i<=temp;i+=2)sum+=1.0f/i;1.摄氏温度和华氏温度转换表输入一个正整数repeat(0
此文档下载收益归作者所有