12.编程求出1-100之间偶数的和。 public class Exam1{
public static void main(String args[]){ (31) ;//定义整型变量sum for(int i=2;i<=100;){ sum+=i;
(32) ; }
System.out.println(\之间偶数的和是:\ }}
答案:(31)int sum =0; (32)i=i+2
13.完成求n!的程序 public class Exam2{
public static void main(String args[]) { factorial(3); factorial(6); factorial(9); } void factorial(int n) { long m=1;
for (int x=1;x<=n; (33) ) (34) ;
System.out.println(n+\}
答案:(33)x++ (34)m=m*x
14.下面的程序定义了一个线程PrintThread,该线程打印输出1~1000之间所有3的倍数,每输出一个数休眠1500毫秒,在main方法中创建了该线程的一个实例,并启动该线程。请将下面的程序填写完整。 class PrintThread extends (35) { public PrintThread(String str) {
(36) ; // 调用父类的构造方法 } public void run() {
for(int i=1;i<=1000;i++) { if(i % 3 ==0)
System.out.println(this.getName()+\
try {
(37) ; // 休眠1500毫秒 } catch (Exception e) { System.out.println(e.toString()); } } } }
public class Exam4{
public static void main(String[] args){
PrintThread myThread = new PrintThread(\
}
(38) ; //启动线程 } 答案:(35)Thread (36)super(str)
(37)sleep(1500) (38)myThread.start()
15.中国有句俗语“三天打鱼两天晒网”,某人从2010年1月1日起三天打鱼两天晒网,编程计算2010年5月1日,他在打鱼还是在晒网。打鱼则输出1,晒网则输出0。请将程序填写完整。 public class Exam4 { }
答案:(40) i
16.调用函数f 输出n的所有质数因子 如n=13860 则输出2 2 3 3 5 7 1 1 public class JModify{ public static void f(int n){ int i=2; while(n>1){ ____ (42) ____{ System.out.println(i); n/=i; } else{
____(43) ____ } }}
public static void main(String args[]){ int n=100; f(n); } }
答案:(42)if(n%i==0) (43)i++
public static void main(String[] args) {
int[ ] dpm={0,31,28,31,30,31,30,31,31,30,31,30,31}; int month=5; // 表示5月 int day=1; // 表示1日
for(int i=0; (39) ; i++ ){ day=day+dpm[i]; }
day = day % 5; if ( (40) ) else
System.out.println(\ // 表示晒网 } System.out.println(\ // 表示打鱼
17.下面的程序通过方法调用从包含4个手机号码的字符串数组中随机抽取一个幸运手机号并输出显示,请根据提示将程序填写完整。
public class RandomTel { }
} }
答案:(44)static
(46)getTel()
18. 宾馆里有100个房间,从1-100进行编号,第一个服务员将所有的房间门都打开,第二个服务员把所有编号是2的倍数的房间“相反处理”,第三个服务员将所有编号是3的倍数的房间再作“相反处理”……,以后每个服务员都是如此操作,当第100个服务员来过后,请编程计算哪几个房间的门是打开的?(所谓“相反处理”是指原来开着的门关上,原来关上的门打开)请将程序填写完整。
public class HotelDoor {
public static void main(String[] args){ } }
答案:(47)a[i]=true (48)j=i
(49)a[j]=!a[j]
19. 以下程序要求从键盘输入一整数,判别该整数是否是素数,并输出“是素数”或“不是素数”,请将程序填写完整。 import java.util.*; public class PrimeExam {
public static void main(String[] args) throws IOException {
public static void main(String[] args){ Scanner sr= new Scanner( (50) ); int flag=0;
boolean[] a=new boolean[101]; final int N=101; int i,j;
for(i=1;i for( (48) ; j if(j%i==0) (49) ; //执行相反处理 public (44) String getTel(){ String[] tels = {\int index = (45) ; //用Math类中的方法生成0~3之间的随机数 return tels [index]; System.out.println(\随机幸运手机号为:\ (46) ); public static void main(String[] args){ (45)(int)(Math.random()*4) for(i=1;i if(a[i]==true) System.out.print(i+\ //显示打开状态的房间编号 int x = sr. (51) ; int y = (int)Math.sqrt(x); for(int i=2;i<=y;i++){ } 答案:(50)System.in (51)nextInt() (52)x % i ==0 (53)flag==0 } if( (52) ){ System.out.println(\不是素数\ flag=1; break; } } if( (53) ) System.out.println(\是素数\