JAVA编程题全集(100题及答案) 下载本文

内容发布更新时间 : 2024/5/18 16:21:12星期一 下面是文章的全部内容请认真阅读。

} }

【程序18】

题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。 public class lianxi18 {

static char[] m = { 'a', 'b', 'c' }; static char[] n = { 'x', 'y', 'z' }; public static void main(String[] args) { for (int i = 0; i < m.length; i++) { for (int j = 0; j < n.length; j++) { if (m[i] == 'a' && n[j] == 'x') { continue;

} else if (m[i] == 'a' && n[j] == 'y') { continue;

} else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) { continue;

} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) {

continue; } else } } } }

【程序19】

题目:打印出如下图案(菱形) * *** ***** ******* ***** *** *

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和宽必须是相等的奇数 for(int i=0; i<(H+1) / 2; i++) { for(int j=0; j

}

for(int k=1; k<(i+1)*2; k++) {

}

}

for(int i=1; i<=H/2; i++) { for(int j=1; j<=i; j++) {

}

for(int k=1; k<=W-2*i; k++) {

}

} } }

【程序20】

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

public class lianxi20 {

public static void main(String[] args) { int x = 2, y = 1, t;

double sum = 0;

for(int i=1; i<=20; i++) { sum = sum + (double)x / y; t = y; y = x; x = y + t; } } }

【程序21】

题目:求1+2!+3!+...+20!的和 public class lianxi21 {

public static void main(String[] args) { long sum = 0; long fac = 1;

for(int i=1; i<=20; i++) { fac = fac * i; sum += fac; } }

}

【程序22】

题目:利用递归方法求5!。 public class lianxi22 {

public static void main(String[] args) { int n = 5; rec fr = new rec(); } }

class rec{

public long rec(int n) { long value = 0 ; if(n ==1 ) { value = 1; } else {

value = n * rec(n-1); }

return value; } }

【程序23】