Java程序设计第1-4章练习题参考答案(5)

2018-11-28 20:09

3、方法int sigmaDigit(int n)的功能是求出十进制整数n的各位数字之和。例如,n=1234,该方法的返回值是10。 static int sigmaDigit( int n ) { int sum = 0, d; while( n > 0 ){ d=n% 10; ________________ n/= 10; }

___________________ }

4、编写一个类Hello.java,用标准输出语句在屏幕上显示一个字符串“Hello!”,请在空白处填上适当的语句。 {

public static void main(String args[]) {

} }

5、下面的声明了一个颜色类。一种颜色由红、绿、蓝三元色值组成,称为RGB值。一个int整数可表示一种颜色,结构为:最高字节全1,其后3个字节分别存储红、绿蓝单色值,单色值的范围是0~255。例如,0xff00ff00表示绿色,RGB值为(0,255,0)。请在空白处填上适当的语句。

public class Color //颜色类 { private int value; //颜色值 public Color(____________________________) //用三元值构造一个颜色

{ value = 0xff000000 | ((red & 0xFF)<<16) | ((green & 0xFF)<<8) | blue & 0xFF; }

public Color(int rgb) //用一个整数值构造一个颜色 {

value = __________________;//将整数rgb的最高字节全置1 }

public int getRGB()

{ return value; }

public int getRed()

{ return (________________) & 0xFF; }

public int getGreen()

{ return (________________) & 0xFF; }

public int getBlue() {

return getRGB() & 0xFF; } }

6、Fibonacci序列是F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N)用一维数组保存Fibonacci序列前25项的值,请在空白处填上适当的语句。

public class Fib_array {

public static void main(String args[]) {

int n=25,i;

__________________ fib[0]=0; fib[1]=1; for (i=2; i

_______________________

________________________ System.out.print(fib[i]+\); System.out.println(); } }

7、阅读以下程序,给出输出结果。

import java.io.*; class Con_Str {

String s1; String s2;

Con_Str( String str1 , String str2 ) { s1 = str1; s2 = str2; }

public String connectstring( ) { return s1+s2;} }

public class Exam_main

{ public static void main(String args[ ])

{ Con_Str s = new Con_Str( \

hello “ System.out.println(s.connectstring( )); } }

输出结果为:______

8、以下程序的输出结果为_______。 public class AppTest {

public static void main(String args[]) { int i = 10, j = 5; int m = i > j ? i : j;

System.out.println(\ } }

9、以下程序的输出结果为_______。 public class WhileTest {

public static void main(String args[]) { int i = 10; while (i-- > 0) { if (i % 2 == 0) { continue; }

System.out.print(i + \ }


Java程序设计第1-4章练习题参考答案(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:钻井队相关方告知书 )

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: