Java程序设计-习题参考答案 联系客服

发布时间 : 星期五 文章Java程序设计-习题参考答案更新完毕开始阅读70d0ba1bfc4ffe473368abba

return false; return true; }

9.编写两个方法,分别求两个整数的最大公约数和最小公倍数,在主方法中由键盘输入两个整数并调用这两个方法,最后输出相应的结果。

import java.io.*; public class Test

{ public static void main(String[] args) {int a=0;

System.out.print(\请输入数a:\ try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); a=Integer.parseInt(br.readLine()); }catch(IOException e){} int b=0;

System.out.print(\请输入数b:\ try

{BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); b=Integer.parseInt(br.readLine()); }catch(IOException e){} if(a<=0||b<=0)

System.out.println(\输入不合法!\

System.out.println(\最大公约数为:\

System.out.println(\最小公倍数为:\ }

public static int Gys(int a,int b) {

int r=0; if(a

b=i; } return a; }

public static int Gbs(int a,int b) {

int ab=a*b;

int r=0; if(a

10.以下程序执行后的输出为 。

public class Test {

static int m1(int a ,int b) { int c; a+=a; b+=b; c=m2(a,b); return(c*c); }

static int m2( int a,int b) { int c; c=a*b%3; return( c );

}

public static void main(String[] args) {

int x=1,y=3,z; z= m1(x,y);

System.out.println(\ } }

z=0

11.编写一个方法,实现求某整数的各位数字之和的功能。

import java.io.*; public class Test

{ public static void main(String[] args) { int i,a=0,s=0;

System.out.print(\请输入数a:\ try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); a=Integer.parseInt(br.readLine()); }catch(IOException e){} for( i=10;a%i!=a;i*=10) {

s+=(a%i)/(i/10);

} s+=a/(i/10);

System.out.println(\各位数之和为\ } }

12.编写完成十进制整数到八进制的转换方法。 import java.io.*; public class Test

{ public static void main(String[] args) { int i,k,a=0,d,s=0;

System.out.print(\请输入数:\ try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(br.readLine()); }catch(IOException e){} d=a%8; s+=d; k=10;

for(i=64;a-d!=0;i*=8) {

s+=(a-d)%i*8/i*k; k*=10; d=a%i;

}

System.out.println(\八进制数为\ } }

13.用于指出数组中某个元素的数字被叫做 ;数组元素之所以相关,是因为它们具有相同的 和 。

14.数组int results[ ] = new int[6] 所占存储空间是 字节。

15.使用两个下标的数组被称为 数组,假定有如下语句:

float scores[ ][ ] = { {1,2,3},{4,5},{6,7,8,9} };

则scores.length的值为: , scores[1].length的值为: , scores[1][1]的值为: 。

16.从键盘上输入10个双精度浮点数后,求出这10个数的和以及它们的平均值。要求分别编写相应求和及求平均值的方法。 import java.io.*; public class Test {

public static void main(String[] args)throws IOException {

InputStreamReader reader=new InputStreamReader(System.in);

二维;3;2;5。 24

下标;数组名和数据类型。