首页 JAVA考试题库

JAVA考试题库

举报
开通vip

JAVA考试题库Java 机考试题 一、第一类题 1. 编写程序,从标准输入读入华氏度,将其转换为摄氏度,并在标准输出打印转换结果。 转 换公式为:摄氏度 = (5/9)*(华氏度-32)。 import javax.swing.JOptionPane; public class Exercise2_1{ public static void main(String[] args){ String str = JOptionPane.showInputDialog(null,"请输入华氏温度:"); double ...

JAVA考试题库
Java 机考试题 一、第一类题 1. 编写程序,从 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 输入读入华氏度,将其转换为摄氏度,并在标准输出打印转换结果。 转 换 公式 小学单位换算公式大全免费下载公式下载行测公式大全下载excel公式下载逻辑回归公式下载 为:摄氏度 = (5/9)*(华氏度-32)。 import javax.swing.JOptionPane; public class Exercise2_1{ public static void main(String[] args){ String str = JOptionPane.showInputDialog(null,"请输入华氏温度:"); double temp = Double.parseDouble(str); double c = (5.0/9)*(temp - 32); String output = "将其转化为摄氏温度为:" + c; JOptionPane.showMessageDialog(null,output); } } 2. 编写程序读入圆柱体的半径和高,计算圆柱的体积,并打印圆柱体的体积。 import javax.swing.JOptionPane; public class Exercise2_2 { public static void main(String[] args) { double radius, height; double area, volume; radius = Double.parseDouble(JOptionPane.showInputDialog("输入半径:")); height = Double.parseDouble(JOptionPane.showInputDialog("输入高度:")); area = Math.PI * radius * radius; volume = area * height; volume = (int)(volume*100)/100.0; JOptionPane.showMessageDialog(null, "半径为:" + radius + ", 高度为:" + height + "的圆柱体积是:" + volume); } } 3. 编写程序读入球的半径,计算球的体积和表面积,并显示结果。 package eayang; import javax.swing.JOptionPane; public class Test1 { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 double r; double area,volume; r=Double.parseDouble(JOptionPane.showInputDialog("输入半径:")); area=4*3.14*r*r; volume=4*3.14*r*r/3.0; JOptionPane.showMessageDialog(null, "此圆的面积为:"+area+" 此圆的体积:"+volume); } } 4. 从命令行读入一些参数,打印参数个数和参数列表。 public class Test2{ public static void main(String[] arg){ for(int i=0;i127){ System.out.println("输入有误~程序终止运行"); System.exit(0); } System.out.println("ASCII码为:"+ num +" 的字符是: "+(char)num); } } 11. (判断三角形〉编写程序输入三角形的三个边,判断输入是否有效。 mport javax.swing.JOptionPane; public class Exercise3_1{ public static void main(String[] args){ double edge1 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第一条边长:")); double edge2 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第二条边长:")); double edge3 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第三条边长:")); boolean isTriangle = isTriangle(edge1,edge2,edge3); String output = "Can deges " + edge1 + " , " + edge2 + " and " + edge3 + " from a triangle? "+ isTriangle; JOptionPane.showMessageDialog(null,output); } private static boolean isTriangle(double edge1,double edge2,double edge3){ double temp; if(edge1num2){ temp = num1; num1 = num2; num2 = temp; } if(num1>num3){ temp = num1; num1 = num3; num3 = temp; } if(num2>num3){ temp = num3; num3 = num2; num2 = temp; } output +=" 排序的结果是:\n" + num1 + " , " + num2 + " , " + num3; JOptionPane.showMessageDialog(null,output); } } 16 (计算三角形的周长)编写程序,读入三角形的三边,如果输入有效,计算它的周长;否 则,显示 输入无效。如果任意两边的和大于第三边,输入有效。 import javax.swing.JOptionPane; public class Exercise3_9{ public static void main(String[] args){ double side1,side2,side3; boolean isTriangle = false; side1 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第一条边长:")); side2 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第二条边长:")); side3 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第三条边长:")); isTriangle = ((side1 + side2 > side3)&&(side1 + side3 > side2)&&(side2 + side3 >side1)); if(isTriangle) JOptionPane.showMessageDialog(null,"三角形的周长为:" + (side1+side2+side3)); else JOptionPane.showMessageDialog(null,"输入的数据,不能组成三角形"); } } 17 (查找当月的天数)编写程序,提示用户输入年和月,而后显示该月的天数。例如,如果用户 输入 2000 年 2 月时,应该显示 2000 年 2 月有 29 天。如果用户输入 2005 年 3 月时,应该显示 2005 年 3 月有 31 天。 import javax.swing.JOptionPane; public class Exercise3_11{ public static void main(String[] args){ int day = 0; int year = Integer.parseInt(JOptionPane.showInputDialog("输入年份:")); int month = Integer.parseInt(JOptionPane.showInputDialog("输入月份:")); boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0)||(year% 400 == 0)); if(isLeapYear && month == 2) day = 29; else if(month==4||month==6||month==9||month==11) day = 30; else if(month==2) day = 28; else day = 31; String output = year + " 年 " + month + " 月有 " + day + " 天"; JOptionPane.showMessageDialog(null,output); } } 18 (统计正数和负数的个数并计算这些数的平均数)编写程序,读入个数不确定的整数,求出读人的 正数和负数个数,并计算它们的总和及平均值,0 不参与计数。当输入为 0 时, 程序结束。将平均值作为一个浮点数来显示。(例如,如果输入 1、2 和 0,平均值应当 为 1.5。) import javax.swing.JOptionPane; public class Exercise4_2 { public static void main(String[] args){ int sum = 0,count1 = 0,count2 = 0,num; String output = ""; while(true){ num = Integer.parseInt(JOptionPane.showInputDialog("输入整数求平均数,以 0 为结束标志")); if(num == 0) break; sum += num; output += num + " , "; if(num > 0) count1 ++; else count2 ++; } output += " 的平均数为:" + (double)sum/(count1 + count2) + "\n正数的个数为:" + count1 + "\n负数的个数为:" + count2; JOptionPane.showMessageDialog(null,output); } } 19 (千克转换成磅)编一个显示下列表格的程序(注意,1 千克为 2.2 磅): 千克 磅 1 2.2 3 6.6 197 433.4 199 437.8 public class Exercise4_3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("\t千克\t磅"); for(int i = 1;i < 200; i += 2){ System.out.println("\t" + i + "\t" + (int)(i*2.2*10)/10.0); } } } 20 (英里转换成千米)编一个显示下列表格的程序(注意,1 英里为 1.609 千米): 英里 千米 1 1.609 2 3.218 ... 9 14.481 10 16.09 package eayang public class Test44 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("\t英里\t千米"); for(int i=1;i<=10;i++) { System.out.println("\t"+i+"\t"+(i*1.609)) ; } }} 21 (千克与磅互换〉编写一个程序,并排显示下列两个表格(注意,1 千克为 2.2 磅): 千克 磅 磅 千克 1 2 . 2 20 9.09 3 6.6 25 11.36 ... 197 433.4 510 231.82 199 437.8 515 234.09 package eayang; public class Test45 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("\t千克\t磅\t磅\t千克"); for(int i=1,n=20;i<=200;i+=2,n+=5) { System.out.println("\t" + i + "\t" + (int)(2.2*i*100)/100.0); System.out.println("\t" + n + "\t" + (int)(n/2.2*100)/100.0); } } } 22 (英里与千米互换)编写一个程序,并排显示下列两个表格(注意,1 英里为 1.609 千 米): 英里 千米 千米 英里 1 1.609 20 12.430 2 3.218 25 15.538 ... 9 14.481 60 37.290 10 16.09 65 40.398 package eayang; public class Test46 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("\t英里\t千米\t千米\t英里"); for(int i=1,n=20;i<=10;i++,n+=5) { System.out.print("\t"+i+"\t"+(i*1.609)); System.out.println("\t"+n+"\t"+(int)(n/1.609*1000)/1000.0); } } } 23(计算将来的学费)假设今年某一大学的学费为$10000,学费的年增长率为 5%。使用循环语句编写程序,计算 10 年内的学费。 package eayang; public class Test47 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int cost=10000; double rate=0.05; int sum=0; for(int i=1;i<=10;i++) { cost+=cost*rate; sum+=cost; } System.out.println("10 年内的学费为:"+sum); } } 24 (查找最高分)编写程序,提示用户输入学生的数量及每个学生的名字和得分,而后显示最高分的学生。 import javax.swing.JOptionPane; public class Exercise4_8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num = Integer.parseInt(JOptionPane.showInputDialog("输入学生数量:")); String name = ""; double score = 0; String output = "最高分为:\n"; for(int i = 0;i < num;i++){ String oneName = JOptionPane.showInputDialog("输入姓名:"); double scoreOfOne = Double.parseDouble(JOptionPane.showInputDialog("输入分数:")); if(scoreOfOne > score){ score = scoreOfOne; name = oneName; }else if(score == scoreOfOne){ output += "姓名:" + oneName + " 分数: " + scoreOfOne + "\n"; } } output += "姓名:" + name + " 分数: " + score + "\n"; JOptionPane.showMessageDialog(null, output); } } 25 (查找最低分)编写程序,提示用户输入学生的数量及每个学生的名字和得分,而后显示最低分的学生。 package soft; import javax.swing.JOptionPane; public class Test3{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num = Integer.parseInt(JOptionPane.showInputDialog("输入学生数量:")); String name = ""; double score = 0; String output = "最低分为:\n"; for(int i = 0;i < num;i++){ String oneName = JOptionPane.showInputDialog("输入姓名:"); double scoreOfOne = Double.parseDouble(JOptionPane.showInputDialog("输入分数:")); if(scoreOfOne score){ score = scoreOfOne; name = oneName; }else if(score == scoreOfOne){ output += "姓名:" + oneName + " 分数: " + scoreOfOne + "\n"; } } output += "姓名:" + name + " 分数: " + score + "\n"; JOptionPane.showMessageDialog(null, output); } } 28 (查找能被 5 和 6 整除的数〉编写程序,显示从 100 到 1000 之间所有能被 5 和 6 都整除的数,每行显示 10 个。 public class Exercise4_10 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n = 0; System.out.println("输出100~1000之间能被5和6整除的数:"); for(int i =100;i < 1000; i++){ if(i % 5 == 0 && i % 6 == 0){ n++; if(n % 10 == 0) System.out.println(i); else System.out.print(i + " "); } } } } 29(查找被 5 或 6 整除,但不能都整除的数)编写程序,显示从 100 到 200 之间所有能被 5 或6 整除,但不 能同时整除的数,每行显示 10 个数。 public class Exercise4_11 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n = 0; System.out.println("输出100~1000之间能被5或6整除的数,但不能同时被整除:"); for(int i =100;i < 1000; i++){ if(i % 5 == 0 ^ i % 6 == 0){ n++; if(n % 10 == 0) System.out.println(i); else System.out.print(i + " "); } } } } 30 (求满足 n >12000 的最小数 n)用 while 循环求 n 平方大于 12 000 的最小数 n。 public class Exercise4_12 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n = 100; while(n*n < 12000){ n++; } System.out.println("n的平方大于12000的最小整数是:" + n); } } 31 (求满足 n <12000 的最大数 n)用 while 循环求 n 立方小于 12 000 的最大数 n。 public class Exercise4_13 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n = 1; while(n*n*n < 12000){ n++; } System.out.println("n的立方小于12000的最大整数是:" + (n - 1)); } } 32 (显示 ASCII 字符表)编写一个程序,打印八字符表中的 128 个字符。每行打印 10 个字符。 public class Exercise4_14 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n = 0; System.out.println("输出'!'到'~'的ACSII码值:"); for(int i = (int)'!';i < (int)'~';i++){ n++; if(n % 10 == 0) System.out.println((char)i); else System.out.print((char)i + " "); } } } 33 (求最大公约数)用循环求两个整数 n1 和 n2 的最大公约数,首先求 n1 和 n2 的最 小值 d, 然后依次检验 d,d-1,d-2,„,2,1 是否是 n1 和 n2 的公约数。这样所求的第一个公约数就是 n1 和 n2 的最大公约数。 import javax.swing.JOptionPane; public class Exercise4_15 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num1 = Integer.parseInt(JOptionPane.showInputDialog("求最大公约数,输入第一个数:")); int num2 = Integer.parseInt(JOptionPane.showInputDialog("求最大公约数,输入第二个数:")); int d = num1 > num2 ? num1 : num2; while(d > 0){ if(num1 % d == 0 && num2 % d == 0) break; d--; } JOptionPane.showMessageDialog(null,num1 + " , " + num2 +" 最大公约数为:" + d); } } 34 (求整数的因子)编写程序,读入一个整数显示它的所有素数因子。例如,若输入整数为 120,输出应为 2,2,2,3,5。 import javax.swing.JOptionPane; public class Exercise4_16 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num = Integer.parseInt(JOptionPane.showInputDialog("输入一个整数:")); String output = num + " 的所有素数因子: "; int i = 2; while(i < num){ if(num % i == 0){ output += i + " , "; num /= i; }else i++; } output += i;//目的是为了去掉输出中最后的逗号。如果循环条件是(i<=num)则输出结果多一个逗号。 JOptionPane.showMessageDialog(null, output); } } 35 编程打印下面的图案: public class Exercise4_18a { /** Print Pattern I */ public static void main(String[] args) { for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) System.out.print(j + " "); System.out.println(); } } } 36 编程打印下面的图案: public class Exercise4_18b { /** Print Pattern II */ public static void main(String[] args) { for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 7 - i; j++) System.out.print(j + " "); System.out.println(); } } } 37 编程打印下面的图案: public class Exercise4_18c { /** Print Pattern III */ public static void main(String[] args) { for (int i = 1; i <= 6; i++) { // Print leading space for (int j = 6 - i; j >= 1; j--) System.out.print(" "); for (int j = i; j >= 1; j--) System.out.print(j + " "); System.out.println(); } } } 38 编程打印下面的图案: public class Exercise4_18d { /** Print Pattern IV */ public static void main(String[] args) { for (int i = 1; i <= 6; i++) { // Print leading space for (int j = i; j > 1; j--) System.out.print(" "); for (int j = 1; j <= 6 + 1 - i; j++) System.out.print(j + " "); System.out.println(); } } } 39 编程打印下列图案: public class Exercise4_19 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final int NUM = 7; int row = NUM; int column = 0; int number = 0; String output = ""; for(int i = 0;i <= row; i++){ for(column = 1; column <= 7-i;column ++) System.out.print(" "); for(int j = 0;j<=i;j++){ number = (int)Math.pow(2, j); if(number < 10) output = " "; else if(number < 100) output = " "; else output = " "; output += number; System.out.print(output); } for(int j = i-1; j >= 0;j--){ number = (int)Math.pow(2, j); if(number < 10) output = " "; else if(number < 100) output = " "; else output = " "; output += number; System.out.print(output); } System.out.println(); } } } 40 打印 2 到 1000 之间的所有素数,每行显示 8 个素数。 public class Test4_20 { public static void main(String[] args) { final int PI = 1000; int count = 0; int num = 2; while(num 0;i--){ sumRihgtToLeft += 1.0/i; } System.out.println(sumRihgtToLeft); System.out.println("结果相差:" +(sumRihgtToLeft - sumLeftToRight)); } } 43 计算下列级数的和: public class Exercise4_24{ public static void main(String[] args){ double sum = 0; for(int i = 1;i <= 97;i++) sum +=(double)i / (i + 2); System.out.println("1/3 + 3/5 + ... +97/99 = " + sum); } } 44 用下列级数计算π的近似值(显示 i=10000,20000,„,100000 时π的值): public class Exercise4_25 { public static void main(String[] args) { //注意变量count和pi的作用域。如果设成全局变量,就会出现问题。 for(int i = 10000; i <= 100000; i += 10000){ int count = 0; double pi = 0; for(int j = 1; j <= (2*i + 1);j += 2){ count++; if(count % 2 == 0) pi -= 1.0/j; else pi += 1.0/j; } System.out.println("当 i = " + i + " 时π的值为:" + pi*4); } } } 45 用下列级数计算 e 的近似值(显示 i=10000,20000,„,100000 时 e 的值): public class Exercise4_26 { public static void main(String[] args) { for(int i = 10000;i <= 100000; i += 10000){ double sum = 1; double item = 1; for(int j = 1; j <= i;j++){ item *= j; sum += 1.0 / item; } System.out.println("当 i = " + i + " 时,e 的值为:" + sum); } } } 46 编写程序显示 21 世纪(2000 年到 2100 年)的所有闰年,每行 10 个。 public class Exercise4_27 { public static void main(String[] args) { boolean isLeapYear = false; int count = 0; System.out.println("21世纪所有的闰年是:"); for(int year = 2001;year <= 2100;year++){ isLeapYear = ((year % 4 ==0&& year % 100 != 0)||(year % 400 == 0)); if(isLeapYear){ count++; if(count % 10 == 0) System.out.println(year); else System.out.print(year + " "); } } } } 47 编写程序,提示用户输入年号,和该年第一天是星期几,显示该年每月第一天是星期几。 import javax.swing.JOptionPane; public class Exercise4_28{ public static void main(String[] args){ boolean isLeapYear = false; String weekName = ""; String monthName=""; int month; int year = Integer.parseInt(JOptionPane.showInputDialog("输入年份:")); int startDay = Integer.parseInt(JOptionPane.showInputDialog("输入该年第一天的星期数?")); System.out.println("输出结果为:"); isLeapYear = ((year%4==0 && year % 100 != 0)||(year % 400 == 0)); for(int monthOfNumber = 1;monthOfNumber<=12;monthOfNumber++){ switch(monthOfNumber){ case 1: monthName = "January";break; case 2: monthName = "February";break; case 3: monthName = "March";break; case 4: monthName = "April";break; case 5: monthName = "May";break; case 6: monthName = "June";break; case 7: monthName = "July";break; case 8: monthName = "Auguse";break; case 9: monthName = "September";break; case 10: monthName = "October";break; case 11: monthName = "November";break; case 12: monthName = "December";break; } month = monthOfNumber - 1; if(isLeapYear&&month == 2) startDay = (startDay + 29)%7; else if (month==4||month==6||month==9||month==11) startDay = (startDay + 30)%7; else if(month == 2) startDay = (startDay + 28)%7; else if(month == 0) startDay %= 7; else startDay = (startDay + 31)%7; switch(startDay){ case 0: weekName = "Sunday";break; case 1: weekName = "Monday";break; case 2: weekName = "Tuesday";break; case 3: weekName = "Wednesday";break; case 4: weekName = "Thursday ";break; case 5: weekName = "Friday";break; case 6: weekName = "Saturday";break; } System.out.println(monthName + " 1 , " + year + " is " + weekName); } } } 48 编写程序,提示用户输入年号,和该年第一天是星期几,然后显示该年的月历。例如,输 入年号 2005,以及 2005 年 1 月 1 日是星期六的 6,程序显示如下: import javax.swing.JOptionPane; public class Exercise4_28{ public static void main(String[] args){ boolean isLeapYear = false; String weekName = ""; String monthName=""; int month; int year = Integer.parseInt(JOptionPane.showInputDialog("输入年份:")); int startDay = Integer.parseInt(JOptionPane.showInputDialog("输入该年第一天的星期数?")); System.out.println("输出结果为:"); isLeapYear = ((year%4==0 && year % 100 != 0)||(year % 400 == 0)); for(int monthOfNumber = 1;monthOfNumber<=12;monthOfNumber++){ switch(monthOfNumber){ case 1: monthName = "January";break; case 2: monthName = "February";break; case 3: monthName = "March";break; case 4: monthName = "April";break; case 5: monthName = "May";break; case 6: monthName = "June";break; case 7: monthName = "July";break; case 8: monthName = "Auguse";break; case 9: monthName = "September";break; case 10: monthName = "October";break; case 11: monthName = "November";break; case 12: monthName = "December";break; } month = monthOfNumber - 1; if(isLeapYear&&month == 2) startDay = (startDay + 29)%7; else if (month==4||month==6||month==9||month==11) startDay = (startDay + 30)%7; else if(month == 2) startDay = (startDay + 28)%7; else if(month == 0) startDay %= 7; else startDay = (startDay + 31)%7; switch(startDay){ case 0: weekName = "Sunday";break; case 1: weekName = "Monday";break; case 2: weekName = "Tuesday";break; case 3: weekName = "Wednesday";break; case 4: weekName = "Thursday ";break; case 5: weekName = "Friday";break; case 6: weekName = "Saturday";break; } System.out.println(monthName + " 1 , " + year + " is " + weekName); } } } 49 使用下述方法头,编写一个将大写字母转换成小写字母的方法: public static char upperCaseToLowerCase(char ch) package eayang; import javax.swing.JOptionPane; public class Test1 { /** * @param args */ public static void main(String[] args) { 自动生成方法存根 // TODO char upperCase=JOptionPane.showInputDialog(null,"请输入一个大写字母:").toCharArray()[0]; System.out.println("输入的大写字母"+upperCase+"转换后的小写字母 为:"+Zhuanhuan(upperCase)); } public static char Zhuanhuan(char upperCase) { int offset = (int)'a' -(int)'A'; char xiaoxie= (char)((int)upperCase + offset); return xiaoxie; } } 50使用下述方法头编写方法,计算一个整数各位数字之和: public static int sumDigits(long n) package eayang; import javax.swing.JOptionPane; public class Test2 { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 int number=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入一个数字:")); System..println("输入的数字"+number+"各个数字之间的和out 为:"+(number)); Zhihe } public static int Zhihe(long number) { int sum = 0; while(number>0){ sum += number%10; number /= 10; } return sum; } } 51编写一个类,包含如下两个方法: public static double celsiusToFahrenheit(double celsius) public static double fahrenheitToCelsius (double fahrenheit) 转换公式为: 华氏度=(9.0/5)*摄氏度+32 并编写一个测试程序,调用这两个方法。 public class Exercise5_8{ public static void main(String[] args){ double celsius = 40; double fahrenheit = 120; System.out.println("\t摄氏\t华氏\t华氏\t摄氏"); for(int i = 0;i<10;i++){ System.out.print("\t" + celsius + "\t" + celsiusToFahrenheit(celsius)); System.out.println("\t" + fahrenheit + "\t" + fahrenheitToCelsuis(fahrenheit)); fahrenheit -= 10; celsius -= 1.0; } } public static double celsiusToFahrenheit(double celsius){ return Math.round(((9.0/5)*celsius + 32)*100)/100.0; } public static double fahrenheitToCelsuis(double fahrenheit){ return Math.round((fahrenheit-32)*5/9.0*100)/100.0; } } 52 用如下方法编写一个程序反序显示一个整数,并测试: public static void reverse(int number) public class Exercise5_8{ public static void main(String[] args){ double celsius = 40; double fahrenheit = 120; System.out.println("\t摄氏\t华氏\t华氏\t摄氏"); for(int i = 0;i<10;i++){ System.out.print("\t" + celsius + "\t" + celsiusToFahrenheit(celsius)); System.out.println("\t" + fahrenheit + "\t" + fahrenheitToCelsuis(fahrenheit)); fahrenheit -= 10; celsius -= 1.0; } } public static double celsiusToFahrenheit(double celsius){ return Math.round(((9.0/5)*celsius + 32)*100)/100.0; } public static double fahrenheitToCelsuis(double fahrenheit){ return Math.round((fahrenheit-32)*5/9.0*100)/100.0; } } 53用如下方法返回一个整数的反序号,并测试: public static int reverse(int number) import javax.swing.JOptionPane; public class Exercise5_3{ public static void main(String[] args){ int number = Integer.parseInt(JOptionPane.showInputDialog(null,"输入一个整数:")); reverse(number); } public static void reverse(int number){ while(number>0){ System.out.print(number%10); number /= 10; } } } 54 编写一个类,使用下列方法求两个给定正整数的最大公约数: Public static int gcd(int m,int n) 编写一个测试程序计算gcd(24,16)和gcd(255,25)。 public class Exercise5_8{ public static void main(String[] args){ double celsius = 40; double fahrenheit = 120; System.out.println("\t摄氏\t华氏\t华氏\t摄氏"); for(int i = 0;i<10;i++){ System.out.print("\t" + celsius + "\t" + celsiusToFahrenheit(celsius)); System.out.println("\t" + fahrenheit + "\t" + fahrenheitToCelsuis(fahrenheit)); fahrenheit -= 10; celsius -= 1.0; } } public static double celsiusToFahrenheit(double celsius){ return Math.round(((9.0/5)*celsius + 32)*100)/100.0; } public static double fahrenheitToCelsuis(double fahrenheit){ return Math.round((fahrenheit-32)*5/9.0*100)/100.0; } } 55 用如下方法编写三个数字的增序排序,并编写测试程序: public static void sort(double num1, double num2, double num3)。 public static void sort(double num1, double num2, double num3)。 import javax.swing.JOptionPane; public class Exercise5_5{ public static void main(String[] args){ double num1 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第一个 数:")); double num2 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第二个 数:")); double num3 = Double.parseDouble(JOptionPane.showInputDialog(null,"输入第三个 数:")); sort(num1,num2,num3); } public static void sort(double num1,double num2,double num3){ double temp; if(num1>num2){ temp = num1; num1 = num2; num2 = temp; } if(num1>num3){ temp = num1; num1 = num3; num3 = temp; } if(num2>num3){ temp = num2; num2 = num3; num3 = temp; } System.out.println("排序结果为:" + num1 + " , " + num2 + " , " + num3); } } 56 用如下方法编写英尺和米的转换程序,编写程序测试。 /** Converts from feet to meters */ public static double footToMeter(double foot) /** Converts from meters to feet */ public static double meterToFoot(double meter) 公式:1米=0.305英尺 public static void displayPattern(int n) import javax.swing.JOptionPane; public class Exercise5_6{ public static void main(String[] args){ int num = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入金字塔的行数:")); displayPattern(num); } public static void displayPattern(int n){ for(int i = 1;i<=n;i++){ for(int k=n-i;k>0;k--) System.out.print(" "); for(int k=i;k>0;k--){ if(k<10) System.out.print(" "+k); else if(k<100) System.out.print(" "+k); else System.out.print(" "+k); } System.out.println("\n"); } } } 57 编写一个方法显示如下图案: 1 2 1 3 2 1 n n-1 ... 3 2 1 方法申明如下: public static void displayPattern(int n) import javax.swing.JOptionPane; public class Exercise5_6{ public static void main(String[] args){ int num = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入金字塔的行数:")); displayPattern(num); } public static void displayPattern(int n){ for(int i = 1;i<=n;i++){ for(int k=n-i;k>0;k--) System.out.print(" "); for(int k=i;k>0;k--){ if(k<10) System.out.print(" "+k); else if(k<100) System.out.print(" "+k); else System.out.print(" "+k); } System.out.println("\n"); } } } 58使用下述方法头,编写一个方法打印字符: 该方法打印从ch1到ch2之间的字符,每行按指定个数打印。编写测试程序,每行打印10 个从‘A’ 到‘Z’的字符。 public static void printChars(char ch1, char ch2, int numberPerLine) import java.util.Scanner; public class Exercise5_12{ public static void main(String[] args){ final int NUMBER_PER_LINE = 10; Scanner input = new Scanner(System.in); System.out.print("输入开始字符:"); char startChar = input.next().toCharArray()[0]; System.out.print("输入结束符:"); char endChar = input.next().toCharArray()[0]; System.out.print("打印 "+ startChar + " 和 " + endChar + " 之间的字符.\n "); printChars(startChar,endChar,NUMBER_PER_LINE); System.out.println(); } public static void printChars(char ch1,char ch2,int numberPerLine){ int start,end,count=0; if((int)ch1 > (int)ch2){ start = (int)ch2; end = (int)ch1; } else{ start = (int)ch1; end = (int)ch2; } while(start <= end){ count ++; if(count % numberPerLine == 0) System.out.println((char)start); else System.out.print((char)start + " "); start ++; } } } 59 写一个形如下述的方法判断一个数是否为素数,并编写程序打印1000以内的素数,每行 打印10个。 • public static boolean isPrime(int num) public class Exercise5_16{ public static void main(String[] args){ final int NUMBER_OF_PRIMES = 1000; final int NMBER_OF_PRIMES_PER_LINE = 10; int count = 1; int number = 2; System.out.println("The first prime number numbers are \n"); while(count <= NUMBER_OF_PRIMES){ if(isPrime(number)){ if(number<10) System.out.print(" "); else if(number<100) System.out.print(" "); else if(number<1000) System.out.print(" "); if(count % NMBER_OF_PRIMES_PER_LINE == 0){ System.out.println(number); }else{ System.out.print(number + " "); } count ++; } number ++; } } public static boolean isPrime(int number){ boolean isPrime = true; for(int divisor = 2;divisor <= number/2; divisor++){ if(number % divisor == 0){ isPrime = false;break; } } return isPrime; } } 60 编写一个方法计算下面的序列: 并编写测试程序,显示下表: i m(i) 2 0.5 3 1.1667 ... 19 15.4523 20 16.4023 public class Exercise5_13{ public static void main(String[] args){ System.out.println("i\tm(i)"); for(int i = 2;i<=20; i++){ System.out.println( i + "\t" +m(i-1)); } } public static double m(int i){ double sum = 0; for(int k = i;k>=1 ;k--) sum = sum + (double)k/(k+1); return sum; } } 61 编写一个方法计算下面的序列: import java.util.Scanner; public class Exercise5_14{ public static void main(String[] args){ System.out.print("输入i的值: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); System.out.println("当 i = " + num +" 时,m(i)= " + m(num)); } public static double m(int i){ double sum = 1; double item = 0; for(int k = i;k>=1;k--){ item = 1.0 / (2*k+1); if(k % 2 == 0) sum += item; else sum -= item; } return 4*sum; } } 62 编写一个方法显示n*n的矩阵,矩阵的每个元素随机产生为0或者1,并写一个测试程 序打印3*3的矩阵,方法头如下: public static void printMatrix(int n) public static void printMatrix(int n) import javax.swing.JOptionPane; public class Exercise5_17{ public static void main(String[] args){ int number = Integer.parseInt(JOptionPane.showInputDialog("输入n代表n×n的矩阵")); System.out.println("打印结果为:\n"); printMatrix(number); } public static void printMatrix(int n){ for(int column = 0; column < n;column ++){ for(int row = 0; rowi){ nextGuess = (lastGuess + (num/lastGuess))/2; lastGuess = (lastGuess + nextGuess)/2; count ++; } System.out.println(count); return lastGuess; } } 70 生成a-z之间的随机字符10个,并打印。 71 编写一个非递归的求阶乘方法,并编写程序测试该方法。 72 采用递归的方式编写一个求阶乘的方法,并编写测试程序测试该方法。 73编写一个非递归方法计算斐波那契夫数,并编写程序测试该方法。 74 采用递归的方式编写一个计算斐波那契夫数的方法,并编写程序测试该方法。 75 用递归的方式编写计算最大公约数的方法,并编写程序测试该方法。 76 用递归的方法求一个整数各位数字之和,并编写程序测试该方法。 77 编写程序打印当前时间。 78 编写程序打印当前日期。 79 编写一个将毫秒转换到时分秒的函数,其头申明如下: public static String convertMillis(long millis) 该方法返回一个形如 hours:minutes:seconds 的串,例如: convertMillis(5500) 返回 串 0:0:5, convertMillis(100000) 返回串 0:1:40, convertMillis(555550000) 返回串 154:19:10. 二、第二类题 80 编写递归的方法计算: public class Exercise5_13{ public static void main(String[] args){ System.out.println("i\tm(i)"); for(int i = 2;i<=20; i++){ System.out.println( i + "\t" +m(i-1)); } } public static double m(int i){ double sum = 0; for(int k = i;k>=1 ;k--) sum = sum + (double)k/(k+1); return sum; } } 81编写递归的方法计算: 82编写递归的方法计算: 83 编写程序,读入是10个数到数组中,计算他们的平均值,统计多少个数字在平均值之上。 import javax.swing.JOptionPane; public class Exercise6_1 { public static void main(String[] args){ double[] num = new double[11]; for(int i =1;i< num.length;i++){ num[i] = Double.parseDouble(JOptionPane.showInputDialog("输入10个数,求平均值")); num[0] += num[i]; } num[0]/= (num.length - 1); int count = 0; String output = ""; for(int i = 1; i< num.length;i++){ if(num[0] < num[i]) count++; output += num[i] + " , "; } output += "\n的平均数为:" + num[0] + "\n大于平均数的个数为:" + count; JOptionPane.showMessageDialog(null,output); } } 84编写程序,读入10个整数并按与读入相反的顺序显示出来。 import javax.swing.JOptionPane; public class Exercise6_3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String output = "输入的数列为:"; int[] num = new int[10]; for(int i = 0;i < num.length; i++){ num[i] = Integer.parseInt(JOptionPane.showInputDialog("输入10个数,这是第 " + (i+1) + " 个")); output += num[i] + " , "; } output += "\n倒置的数列为:" ; for(int i = num.length - 1;i>0;i--){ output += num[i] + " , "; } output += num[0]; JOptionPane.showMessageDialog(null, output); } } 85编写一个程序,读入数目不确定的考试分数,并且判断有多少个分数高于或等于平均 分, 有多少个分数低于平均分。输人一个负数标志输入结束,假设最高分为100。 // Exercise6_4.java: Analyze scores public class Exercise6_4 { // Main method public static void main(String[] args) { double[] scores = new double[100]; double sum = 0; int count = 0; java.util.Scanner input = new java.util.Scanner(System.in); do { System.out.print("Enter a new score: "); scores[count] = input.nextDouble(); sum += scores[count]; } while (scores[count++] >= 0); double average = (sum - scores[count]) / (count - 1); int numOfAbove = 0; int numOfBelow = 0; for (int i = 0; i < count - 1; i++) if (scores[i] >= average) numOfAbove++; else numOfBelow++; System.out.println("Average is " + average); System.out.println("Number of scores above or equal to the average " + numOfAbove); System.out.println("Number of scores below the average " + numOfBelow); } } 86编写一个程序,使它能够读入10个数并且显示其中互不相同的数。 import javax.swing.JOptionPane; public class Exercise6_5{ public static void main(String[] args){ int[] num = new int[10]; boolean zero = false; int j = 0; for(int i = 0;i < 10 ;i++){ int number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number:")); if(number == 0) zero = true; if(number != 0) for(j = 0;j <= i; j++) if(num[j] == number) break; if(j >= i) num[i] = number; } if(zero) System.out.println(0); for(int i = 0;i < num.length;i++) if(num[i] != 0) System.out.println(num[i]); } } 87编写一个程序,生成0~9 之间的 100个随机整数并且显示每一个数的个数。 88编写两个重载方法,返回一个数组的平均数,它们具有如下的方法头: public static int average(int[] array); public static double average(double[] array); 用{1, 2, 3, 4, 5, 6} 和 {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}来测试方法。 public class Exercise6_8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] array1 = {1,2,3,4,5}; double[] array2 = {1.2,1.3,2.3,2.5}; System.out.println("数组array1的平均数为: " + average(array1)); System.out.println("数组array2的平均数为: " + average(array2)); } private static double average(double[] array) { // TODO Auto-generated method stub double sum = 0; for(int i = 0;i < array.length;i++) sum += array[i]; return sum/array.length; } private static int average(int[] array) { // TODO Auto-generated method stub int sum = 0; for(int i = 0;i < array.length;i++) sum += array[i]; return sum/array.length; } } 89编写一个方法,求出一个整数数组中的最小元素。用{1, 2, 4, 5, 10, 100, 2, –22}来 public class Exercise6_8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] array1 = {1,2,3,4,5}; double[] array2 = {1.2,1.3,2.3,2.5}; System.out.println("数组array1的平均数为: " + average(array1)); System.out.println("数组array2的平均数为: " + average(array2)); } private static double average(double[] array) { // TODO Auto-generated method stub double sum = 0; for(int i = 0;i < array.length;i++) sum += array[i]; return sum/array.length; } private static int average(int[] array) { // TODO Auto-generated method stub int sum = 0; for(int i = 0;i < array.length;i++) sum += array[i]; return sum/array.length; } } 测试方法。 90 编写一个方法,求出一个整数数组中最小元素的下标。如果这样的元素个数 大于1,则返回最小数的下标。用{1, 2, 4, 5, 10, 100, 2, –22}来测试方法。 public class Exercise6_9 { /** * @param args */ public static void main(String[] args) { int[] num = {1,2,4,5,10,100,2,-22}; System.out.println("数组num的最小元素为:" + min(num)); } private static int min(int[] num) { int min = num[0]; for(int i = 1;i < num.length;i++) if(num[i]< min) min = num[i]; return min; } } 91 将一个数组反序后拷贝到一个新的数组。 public class Exercise6_12 { /** * @param args */ public static void main(String[] args) { int[] array = new int[10]; for(int i = 0; i < array.length; i++) array[i] = i + 1; System.out.println("原始数列为: "); for(int i =0; i < array.length;i++) System.out.print(" " + array[i]); array = reverse(array); System.out.println("\n倒置的数列为:"); for(int i =0; i < array.length;i++) System.out.print(" " + array[i]); } private static int[] reverse(int[] array) { int temp = 0; int j = array.length - 1; for(int i = 0; i <= j;i++){ temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } return array; } } 92 编写一个数组的选择排序方法。并用{1, 2, 4, 5, 10, 100, 2, –22}来测试方法。 93 编写一个数字的冒泡排序方法,并用{1, 2, 4, 5, 10, 100, 2, –22}来测试方法。 94 用插入排序算法编写一个排序方法,并用{1, 2, 4, 5, 10, 100, 2, –22}来测试方法。 95 编写一个方法,其参数个数可变,假定参数都是double,计算传入的参数的平均值。 96 编写一个程序,提示输入学生数量、学生姓名和他们的成绩,并按照成绩的 降序来打印 学生的姓名。 import javax.swing.JOptionPane; public class Exercise6_19 { public static void main(String[] args) { int count = Integer.parseInt(JOptionPane.showInputDialog("输入学生数量:")); String[] names = new String[count]; double[] scores = new double[count]; for(int i = 0; i < names.length;i++){ names[i] = JOptionPane.showInputDialog("输入学生姓名:"); scores[i] = Double.parseDouble(JOptionPane.showInputDialog("输入学生成绩:")); } sort(scores,names); String output = "输出学生成绩:\n"; for(int i = scores.length - 1;i >=0 ;i--) output += names[i] + " " + scores[i] + "\n"; JOptionPane.showMessageDialog(null,output); } private static void sort(double[] list, String[] array) { if(list.length != array.length){ System.out.println("数组大小不一致,程序终止"); System.exit(0); } for(int i = 1; i < list.length;i++){ double currentElement = list[i]; String currentName = array[i]; int k =0; for(k = i - 1; k >= 0&& list[k] > currentElement; k--){ list[k+1] = list[k]; array[k+1] = array[k]; } list[k+1] = currentElement; array[k+1] = currentName; } } } 97编写一个方法,求整数矩阵中所有整数的和。用{{1, 2, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13}, {14, 15, 16, 17}}来测试方法。 public class Exercise6_20 { public static void main(String[] args) { int[][] list = {{1,2,4,5},{6,7,8,9},{10,11,12,13},{14,15,16,17}}; int sum = sumMatrix(list); System.out.println("整数矩阵所有整数的和为:" + sum); } private static int sumMatrix(int[][] list) { int sum = 0; for(int row = 0;row < list.length;row ++) for(int column = 0; column < list[row].length;column ++) sum += list[row][column]; return sum; } } 98编写一个方法,求整数矩阵中主对角线上所有整数的和。用{{1, 2, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13}, {14, 15, 16, 17}}来测试方法。 99假定每个雇员每周工作的小时数存储在一个二维数组中。每行用七列记录一个雇员七天的 工作时间。例如,下列数组存储了8个雇员的工作时间。编写一个程序,按照总工时降序的 方式显示雇员和他们的总工时。 100编写两个矩阵相加的方法。方法头如下: public static int[][] addMatrix(int[][] a, int[][] b) public class Exercise6_24 { public static void main(String[] args) { // TODO Auto-generated method stub int[][] a = {{1,2,3},{1,2,3},{1,2,3}}; int[][] b = {{1,2,3},{1,2,3},{1,2,3}}; int[][] c = addMatrix(a,b); for(int i = 0; i< c.length;i++){ for(int j = 0;j < c[i].length;j++) System.out.print(c[i][j] + " "); System.out.println(); } } public static int[][] addMatrix(int[][] a,int[][] b){ int i,j; for(i = 0;i < a.length;i++) for(j = 0;j < a[i].length;j++) a[i][j] += b[i][j]; return a; } } 101编写两个矩阵相乘的方法。方法头如下: public static int[][] multiplyMatrix(int[][] a, int[][] b) 102 编写程序,在三子棋棋盘 (3x3格)上随机地填入0 和 1,打印棋盘,并找出全是0 (或 1)的行、列或对角线。使用二维数组表示三子棋棋盘。 public class Exercise6_26 { public static void main(String[] args) { // Each row in points represents a point double[][] points = {{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1}, {2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, {-1.5, 4, 2}, {5.5, 4, -0.5}}; // p1 and p2 are the indices in the points array int p1 = 0, p2 = 1; // Initial two points double shortestDistances = distance( points[p1][0], points[p1][1], points[p1][2], points[p2][0], points[p2][1], points[p2][2]); // Initialize shortestDistances // Compute distance for every two points for (int i = 0; i < points.length; i++) { for (int j = i + 1; j < points.length; j++) { double distance = distance( points[i][0], points[i][1], points[i][2], points[j][0], points[j][1], points[j][2]); if (shortestDistances > distance) { p1 = i; // Update p1 p2 = j; // Update p2 shortestDistances = distance; // Update shortestDistances } } } // Display result System.out.println("The closest two points are " + "(" + points[p1][0] + ", " + points[p1][1] + ", " + points[p1][2] + ") and (" + points[p2][0] + ", " + points[p2][1] + ", " + points[p2][2] + ")"); } /** Compute the distance between two points */ public static double distance( double x1, double y1, double z1, double x2, double y2, double z2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); } } 103 (西洋跳棋盘)编写程序,在8x8 的棋盘中随机地填入0 和 1,打印棋盘,并找出全是0 (或 1)的行、列或对角线。使用二维数组表示棋盘。 104 (玩三子棋游戏)在三子棋游戏中,两个游戏者在3x3 网格中轮流作标记,一个人用X, 另一 个人用0。如果一个游戏者在网格的水平、垂直或对角线方向上作了三个连续的标 记,游戏就以这 个游戏者得胜而告终。当网格的所有单元格都填满了标记而没有一个游 戏者获胜,就出现了平局 (没有优胜者)。编写一个玩三子棋游戏的程序,如下所示: 程序提示第一个游戏者输入 X 标记,接着提示第二个游戏者输入0 标记。每输入一 个标记后,程序刷新棋盘并显示游戏的状态(获胜、平局还是未结束)。为了输入标记, 通过输入对话框提示用户输入标记的行和列坐标。 // Exercise6_24.java public class Exercise6_28 { public static void main(String[] args) { int[][] board = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = (int)(Math.random() * 2); System.out.print(board[i][j]); } System.out.println(); } // Check rows for (int i = 0; i < 3; i++) if (board[i][0] == board[i][1] && board[i][0] == board[i][2]) System.out.println("All " + board[i][0] + "'s on row " + i); // Check columns for (int j = 0; j < 3; j++) if (board[0][j] == board[1][j] && board[0][j] == board[2][j]) System.out.println("All " + board[0][j] + "'s on column " + j); // Check major diagonal if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) System.out.println("All " + board[0][0] + "'s on major diagonal"); // Check subdiagonal if (board[0][2] == board[1][1] && board[0][2] == board[2][0]) System.out.println("All " + board[0][2] + "'s on subdiagonal"); } } 105 编写方法计算两个整数n1,n2的最小公倍数(提示:最小公倍数=n1*n2/gcd(n1,n2)), 并用120和150来测试方法。 106 编写一个名为 Rectangle 的类表示矩形,他有两个双精度浮点型私有属性:width、 height,一个静态字符串型类属性color,一个默认长和宽都为1的的构造函数,一个指 定长和宽的构造函数,公有的求面积和周长的方法。 public class Exercise7_1 { public static void main(String[] args) { Rectangle r1 = new Rectangle(4,40); r1.setColor("red"); Rectangle r2 = new Rectangle(3.5,35.9); r2.setColor("red"); System.out.println(r1.toString()); System.out.println(r2.toString()); } } class Rectangle{ private double width = 1; private double height = 1; private String color = "white"; public Rectangle(){ this(1.0,1.0); } public Rectangle(double width,double height){ this.width = width; this.height = height; color = "white"; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getArea(){ return width*height; } public double getPerimeter(){ return (width + height)*2; } public String toString(){ String output = ""; output = "矩形的属性:\n\t宽 " + width + "\n\t高:" + height +"\n\t颜色:" + color + "\n\t面积:" + getArea() + "\n\t周长:" + getPerimeter(); return output; } } 107 利用System.currentTimeMillis()方法测试一个计算100阶双精度浮点矩阵乘法花费的 执行时间。 108 编写一个二维的平面点类Mypoint,有双精度型x,y属性,一个距离方法头如下: double distance(MyPoint secendPoint) 另外一个静态距离方法头如下: double distance(MyPoint p1,MyPoint p2) 109 编写一个方法判断一个字符串是否为回文串。 110编写一个方法判断一个字符串是否为回文串,忽略大小写。 111 编写一个方法判断一个字符串是否为另一个串的子串。 112使用下列方法头编写—个方法,求指定字符在字符串中出现的次数: public static int count(String str, char a) import javax.swing.JOptionPane; public class Exercise8_4 { public static void main(String[] args) { String str = JOptionPane.showInputDialog("输入一个字符串"); char ch = JOptionPane.showInputDialog("输入要统计的字符:").trim().toCharArray()[0]; int count = count(str,ch); JOptionPane.showMessageDialog(null,"字符 " + ch + " 在字符串 " + str + " 中出现 " + count + " 次"); } private static int count(String s, char a) { int count = 0; for(int i =0; i < s.length();i++) if(a == s.charAt(i)) count ++; return count; } } 113使用下列方法头编写一个方法,统计每个数字在字符串中出现的次数。 public static int[] count(String s) 编写main方法,显示对字符串"SSN is 343 32 4545 and ID is 434 34 4323"的统 计结果。 import javax.swing.JOptionPane; public class Exercise8_5 { public static void main(String[] args) { String str = JOptionPane.showInputDialog("输入一个字符串,统计数字出现的次数:"); String output = "统计结果:0 1 2 3 4 5 6 7 8 9 \n出现次数:"; int[] num = new int[10]; num = count(str); for(int i = 0; i < num.length; i++) output += num[i] + " "; JOptionPane.showMessageDialog(null,output); } private static int[] count(String str) { int[] count = new int[10]; int num = 0; for(int i = 0; i < str.length();i++) if(Character.isDigit(str.charAt(i))){ num = Integer.parseInt(String.valueOf(str.charAt(i))); count[num]++; } return count; } } 114使用下列方法头编写一个方法,统计字母在字符串中出现的个数。 public static int countLetters(String s) 编写main方法调用 countLetters("Java in 2008")并显示结果。 import javax.swing.JOptionPane; public class Exercise8_6 { public static void main(String[] args) { String s = JOptionPane.showInputDialog("输入一个字符串,统计字母出现的次数"); int count = countLetters(s); JOptionPane.showMessageDialog(null,"字符串 " + s + "\n字母出现的次数为:" + count); } private static int countLetters(String s) { int count = 0; for(int i = 0; i < s.length();i++) if(Character.isLetter(s.charAt(i))) count++; return count; } } 115 编写一个方法将十六进制的字符串转换为十进制整数。方法头如下: public static int parseHex(String hexString) 使用十六进制串 ABC 和 10A来检测。 import javax.swing.JOptionPane; public class Exercise8_7 { public static void main(String[] args) { String numString = JOptionPane.showInputDialog("输入一个十六进制数,转化成十进制数"); JOptionPane.showMessageDialog(null, numString + "转化成十进制数为:" + parseHex(numString)); } public static int parseHex(String hexString){ int num = 0; for(int i = 0; i < hexString.length();i++) if(Character.isDigit(hexString.charAt(i))) num = num*16 + (int)(hexString.charAt(i) - '0'); else if(Character.isLowerCase(hexString.charAt(i))) num = num*16 + (int)(hexString.charAt(i) - 'W'); else num = num*16 + (int)(hexString.charAt(i) - '7'); return num; } } 116 编写一个方法将二进制的字符串转换为十进制整数,方法头如下: public static int parseBinary(String binaryString) 使用二进制串 11111111 测试该方法。 import javax.swing.JOptionPane; public class Exercise8_8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String binaryString = JOptionPane.showInputDialog("输入一个2进制数转化成16进制数"); JOptionPane.showMessageDialog(null,binaryString + "转化成16进制为:" + parseBinary(binaryString)); } private static String parseBinary(String binaryString) { // TODO Auto-generated method stub int num; String output = ""; for(int i = 0;i < binaryString.length();i += 4){ num = 0; String binary = binaryString.substring(i, i+4); for(int j = 1; j <= 4;j++){ num += (int)(binary.charAt(j - 1) - '0') * Math.pow(2, binary.length() - j); } if(num >= 10) output += (char)(num + 55); else output += num + ""; } return output; } } 117 编写一个方法,将十进制数转换成十六进制字符串,方法头如下: public static String convertDecimalToHex(int value) 使用十进制数298和9123测试该方法。 import javax.swing.JOptionPane; public class Exercise8_9 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int value = Integer.parseInt(JOptionPane.showInputDialog("十进制转化成十六进制")); JOptionPane.showMessageDialog(null,value + "转化为十六进制数:" + conertDecimalToHex(value)); } private static String conertDecimalToHex(int value) { // TODO Auto-generated method stub StringBuilder output = new StringBuilder(); int num = 0; while(value > 0){ num = value % 16; if(num >= 10) output.append((char)(num + '7')); else output.append(num); value /= 16; } return output.reverse().toString(); } } 118 使用下述方法头编写一个方法,返回排好序的字符串: public static String sort(String s) 例如:sort(“acb”)返回abc; 119 编写一个方法,将十进制数转换为二进制数的字符串,方法头如下: public static String convertDecimalToBinary(int value) 使用十进制数298和9123测试该方法。 import javax.swing.JOptionPane; public class Exercise8_10 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num = Integer.parseInt(JOptionPane.showInputDialog("十进制转换成二进制")); JOptionPane.showMessageDialog(null,num + "转化成二进制数:" + conertDecimalToBinary(num)); } private static String conertDecimalToBinary(int num) { // TODO Auto-generated method stub StringBuffer strBuf = new StringBuffer(); while(num > 0){ strBuf.append(num % 2); num /= 2; } return strBuf.reverse().toString(); } } 120 (变位词)编写一个方法检测两个单词是否互为变位词。如果在不计顺序的情况下两个 单词包含完全相同的字母,则这两个词互为变位词。例如"silent" and "listen"互为变 位词。方法头如下所示: public static boolean isAnagram(String s1, String s2) 调用 isAnagram("silent", "listen"), isAnagram("garden", "ranged"), 和 isAnagram("split", "lisp")来测试。 public class Exercise8_12 { public static void main(String args[]) { // Prompt the user to enter a string java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter the first string: "); String first = input.nextLine(); System.out.print("Enter the second string: "); String second = input.nextLine(); System.out.println( first + " and " + second + " is " + (isAnagram(first, second) ? "anagram." : "not anagram.")); } public static boolean isAnagram(String s1, String s2) { String newS1 = Exercise8_11.sort(s1); String newS2 = Exercise8_11.sort(s2); if (newS1.length() != newS2.length()) return false; for (int i = 0; i < newS1.length(); i++) { if (newS1.charAt(i) != newS2.charAt(i)) return false; } return true; } } 121编写程序,使用空格和标点符号作为定界符,从字符串中提取单词。字符串从输人 对话框中读入。 122编写程序,从输入对话框中读入一个字符串,该串是由空 格分割的双精度值组成的,显示这些值的和。 import javax.swing.JOptionPane; public class Exercise8_17{ public static void main(String[] args){ String s = JOptionPane.showInputDialog("输入一个字符处提取单词"); String[] str = s.split("\\s"); System.out.println("提取的单词为:"); for(int i = 0; i < str.length;i++) System.out.println("\t" + str[i]); } } 123使用Scanner类从键盘读入空格分割的数,显示它们的和与平均值。 124 从程序命令行参数读入一个串,判断该串是否互为回文。 125 编写程序,传给main方法一个字符串,显示该字符串中大写字母的个数。 126 编写一个抽象的二维几何体类GeometricObject,其中定义周长和面积方法,和形体颜 色属性,再编写三角形类其继承GeometricObject类,有三个边长属性,编写矩形类继承 GeometricObject类,有长和宽属性。 127编写一个抽象的二维几何体接口Geometricinterface,其中定义周长和面积方法,再编 写三角形类其实现Geometricinterface接口,有三个边长属性,编写矩形类继承 Geometricinterface接口,有长和宽属性。 128 编写一个抽象的二维几何体类 GeometricObject,其中定义周长和面积方法,和形体颜 色属性,并实现Comparable接口(规定比较是比较面积大小),再编写三角形类其继承 GeometricObject类,有三个边长属性,编写矩形类继承GeometricObject类,有长和宽 属性。用max方法编写测试程序比较一个三角形和一个矩形的大小。 129编写一个抽象的二维几何体接口Geometricinterface,其中定义周长和面积方法,并实 现 Comparable 接口(规定比较是比较面积大小),再编写三角形类其实现 Geometricinterface接口,有三个边长属性,编写矩形类继承Geometricinterface接口, 有长和宽属性。 130 编写一个抽象的二维几何体类 GeometricObject,其中定义周长和面积方法,和形体颜 色属性,并实现Comparable接口(规定比较是比较面积大小),再编写三角形类其继承 GeometricObject类,有三个边长属性,编写矩形类继承GeometricObject类,有长和宽 属性。创建大小为10的矩形数组,求最大的矩形面积。 131 实现一个有理数Rational类,并用Rational类计算 1/2+2/3+„+99/100。 132 编写程序打印1到1000的整数中,满足x2+y=z 的解。 133 编写方法计算从1+2+3+„+n 的和。 134 编写方法计算从1+2+3+„+n 的和。 135 编写程序向ArrayList中添加10个随机整数,然后遍历ArrayList打印这些整数。 136 用泛型的方式向ArrayList中添加10个随机整数,然后遍历ArrayList打印这些整数。 137 编写程序向Vector中添加10个随机整数,然后遍历Vector打印这些整数。 138 用泛型的方式向Vector中添加10个随机整数,然后遍历Vector打印这些整数。 139 编写程序向HashMap中添加字符串为key和值的10个条目,然后遍历打印其内容。
本文档为【JAVA考试题库】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_222924
暂无简介~
格式:doc
大小:235KB
软件:Word
页数:97
分类:企业经营
上传时间:2017-09-02
浏览量:176