欢迎来到天天文库
浏览记录
ID:18983323
大小:76.50 KB
页数:7页
时间:2018-09-27
《浙大 java语言程序设计编程答案8new》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验9-1Method的使用(二)1.将一个整数逆序输出输入一个正整数repeat(02、repeat;longn,res;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/res=reverse(n);System.out.println(res);}}/*---------*/staticlongreverse(longnumber){intflag=1;longa=0,digit;if(number<0){flag=-1;number=-number;}while(number>0){dig3、it=number%10;//分离出个位数字a=a*10+digit;//形成当前的逆序数number=number/10;}returnflag*a;}}1.十进制转换二进制输入一个正整数repeat(04、mportjava.util.Scanner;publicclassTest50010{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/dectobin(n);System.out.println();}}/*---------*/staticvoiddectobin(intn){Stringt="5、";//保存二进制数do{t=n%2+t;//n除2后的余数拼接到t的前面n=n/2;//获得除2后的商}while(n>0);System.out.print(t);//本方法无返回值,需要在方法体中输出结果}}说明:本题中方法dectobin(n)的输出虽然与要求有所出入,但上传是正确的。1.用函数求三个数的最大值输入一个正整数repeat(06、repeat=3)输入:589(a=5,b=8)-1-10-5(a=-1,b=-10)111(a=1,b=1)输出:max(5,8,9)=9max(-1,-10,-5)=-1max(1,1,1)=1importjava.util.Scanner;publicclassTest50011{publicstaticvoidmain(Stringargs[]){intri,repeat;inta,b,c,maximun;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){7、a=in.nextInt();b=in.nextInt();c=in.nextInt();/*-----------------*/maximun=maximun(a,b,c);System.out.println("max("+a+","+b+","+c+")="+maximun);}}/*-------------------*/staticintmaximun(inta,intb,intc){intmax;max=a;if(max
2、repeat;longn,res;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/res=reverse(n);System.out.println(res);}}/*---------*/staticlongreverse(longnumber){intflag=1;longa=0,digit;if(number<0){flag=-1;number=-number;}while(number>0){dig
3、it=number%10;//分离出个位数字a=a*10+digit;//形成当前的逆序数number=number/10;}returnflag*a;}}1.十进制转换二进制输入一个正整数repeat(04、mportjava.util.Scanner;publicclassTest50010{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/dectobin(n);System.out.println();}}/*---------*/staticvoiddectobin(intn){Stringt="5、";//保存二进制数do{t=n%2+t;//n除2后的余数拼接到t的前面n=n/2;//获得除2后的商}while(n>0);System.out.print(t);//本方法无返回值,需要在方法体中输出结果}}说明:本题中方法dectobin(n)的输出虽然与要求有所出入,但上传是正确的。1.用函数求三个数的最大值输入一个正整数repeat(06、repeat=3)输入:589(a=5,b=8)-1-10-5(a=-1,b=-10)111(a=1,b=1)输出:max(5,8,9)=9max(-1,-10,-5)=-1max(1,1,1)=1importjava.util.Scanner;publicclassTest50011{publicstaticvoidmain(Stringargs[]){intri,repeat;inta,b,c,maximun;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){7、a=in.nextInt();b=in.nextInt();c=in.nextInt();/*-----------------*/maximun=maximun(a,b,c);System.out.println("max("+a+","+b+","+c+")="+maximun);}}/*-------------------*/staticintmaximun(inta,intb,intc){intmax;max=a;if(max
4、mportjava.util.Scanner;publicclassTest50010{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*---------*/dectobin(n);System.out.println();}}/*---------*/staticvoiddectobin(intn){Stringt="
5、";//保存二进制数do{t=n%2+t;//n除2后的余数拼接到t的前面n=n/2;//获得除2后的商}while(n>0);System.out.print(t);//本方法无返回值,需要在方法体中输出结果}}说明:本题中方法dectobin(n)的输出虽然与要求有所出入,但上传是正确的。1.用函数求三个数的最大值输入一个正整数repeat(06、repeat=3)输入:589(a=5,b=8)-1-10-5(a=-1,b=-10)111(a=1,b=1)输出:max(5,8,9)=9max(-1,-10,-5)=-1max(1,1,1)=1importjava.util.Scanner;publicclassTest50011{publicstaticvoidmain(Stringargs[]){intri,repeat;inta,b,c,maximun;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){7、a=in.nextInt();b=in.nextInt();c=in.nextInt();/*-----------------*/maximun=maximun(a,b,c);System.out.println("max("+a+","+b+","+c+")="+maximun);}}/*-------------------*/staticintmaximun(inta,intb,intc){intmax;max=a;if(max
6、repeat=3)输入:589(a=5,b=8)-1-10-5(a=-1,b=-10)111(a=1,b=1)输出:max(5,8,9)=9max(-1,-10,-5)=-1max(1,1,1)=1importjava.util.Scanner;publicclassTest50011{publicstaticvoidmain(Stringargs[]){intri,repeat;inta,b,c,maximun;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){
7、a=in.nextInt();b=in.nextInt();c=in.nextInt();/*-----------------*/maximun=maximun(a,b,c);System.out.println("max("+a+","+b+","+c+")="+maximun);}}/*-------------------*/staticintmaximun(inta,intb,intc){intmax;max=a;if(max
此文档下载收益归作者所有