Java基础编程题(含答案)

上传人:非****1 文档编号:57266935 上传时间:2022-02-23 格式:DOCX 页数:49 大小:68.52KB
返回 下载 相关 举报
Java基础编程题(含答案)_第1页
第1页 / 共49页
Java基础编程题(含答案)_第2页
第2页 / 共49页
Java基础编程题(含答案)_第3页
第3页 / 共49页
点击查看更多>>
资源描述
精选文库50道JAVA基础编程练习题【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月乂生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?程序分析: 兔子的规律为数列1, 1,2,3, 5,8, 13,21.-P ublic class Progl(p ublic static void mam(Stnng2 args) int n = 10;System, out. printin(第个月 兔子总数为“+fun(n);p rivate static int fun(int n)if (n=l n=2)return 1;elsereturn fun(n-l)+fun(n-2);【程序2】题目:判断101-2。之间有多少个素数,并输出所有索数。程序分析:判断素数的方法:用一个数分别去除2到sqrt (这个数),如果能被整除,则表明此数不是 素数,反之是素数。P ublic class Prog2(p ublic static void main(String口 args)int m = 1;int n = 1000;int count = 0;/统计素数个数 for (int i=m; in;i+) if(is Prime(i)count;System, out. print (i+);if (count%100) System, out. printlnO;System, out. printInO;System. out. printing,在和之间共有+count+个素数);判断素数P rivate static boolean isP nme(int n)boolean flag = true;if(n=l)flag = false;else(for (int i=2; iv=Math. sqrt (n) ; if (n%i)=:on=l) flag = false;break;elseflag = true;return flag;【程序3】题目:打印出所有的-水仙花数,所谓-水仙花数是指一个三位数,其各位数字立方和等于该数本身。 例如:153是一个水仙花数,因为153=1的三次方+ 5的三次方+ 3的三次方。程序分析:利用for循环控制1。0999个数,每个数分解出个位,十位,百位。P ublic class Prog3(p ublic static void main(Stringargs) for(int i=100;ik,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一 步。如果n不能被k整除,则用k+1作为k的值,重复执行第一步。P ublic class Prog4p ublic static void main(Stringtl args)int n = 13;deco mp ose(n);p rivate static void deco mp ose(int n) System, out . p rint(n+=);for(int i=2;i=9。分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。程序分析:(ab)?a:b这是条件运算符的基本例子。P ublic class Prog5(p ublic static void mam(Stnng args) int n = -1;try(n = Integer, parselnt(args0);catch(ArraylndexOutOfBoundsExce ption e)System, out, print In (请输入成绩):return;grade(n);成绩等级计算p rivate static void grade (int n) if (n100 II n=90)?分,属于 A 等:(Q60)?分,属于 B 等:分,属于 C 等)System.out. pnntln(n+str);【程序6】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 程序分析:利用辗除法。P ublic class Prog6 p ublic static void mam(String args) int m, n;try(m = Integer, parselnt(args0J);n = Integer, parselnt(args1J);catch(ArraylndexOutOfBoundsExce ption e) System, out. println(/输入有 误”);return;maximin(m, n);求最大公约数和最小公倍数P rivate static void max_min(int m. int n) int temp = 1;int yshu = 1; int bshu = m*n;if (nm)temp = n;n = m; m = te mp;while(m!=0) te mp = n%m;n = m; m = te mp;yshu = n;bshu /= n;System. out. pnntln(m+和的最大公约数为力7加);System. out. pnntln(m+和+n+”的最小公倍数为+bshu);【程序7】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。程序分析:利用while语句,条件为输入的字符不为,3.import java. util. Scanner;p ublic class Prog7_l(p ublic static void mam (String args) System, out . p rint C请输入一a串字符:);Scanner scan = new Scanner(System, in);String str = scan. nextLine () ;/ scan. close ();将一行字符转化为字符串count(str);统计输入的字符数P rivate static void count(Stnng str)String El = *ule00-u9fa5*;/汉字String E2 =String E3 = 0-9”;String E4 =空格int countChinese = 0;int countLetter = 0;int countNumber = 0;int counts pace = 0;int countOther = 0;chart array,Char = str. toCharArray() ;/将字符串转化为字符数组String array_String = new Stringarray_Char. length ;/ for(int汉字只能作为字符串处理i=0; iarray_Char. length; i-H-)array_Stnngi2 = String. valueOf (array_Char li);遍历字符串数组中的兀素for (String s:array_String) if(s. matches (El)countChinese;else if(s. matches(E2)countLette 叶+;else if(s. matches(E3)countNumbe 叶+;else if(s. matches(E4)counts pace+; elsecount0ther+;System, out. printingSystem, out. printingSystem, out. printingSystem, out.System, out.printingprinting输入的汉字个数:输入的字母个数:输入的数字个数:输入的空格个数:*-countChinese);-rcountLetter);“-countNumber);“-counts pace);输入的其它字符个数:+countSpace);import java. util. *;p ublic class Prog7 2(P ublic static void mam(StnngI args) System. out. printing请输入一行字符:;Scanner scan = new Scanner(System, in);String str = scan. nextLineO ;scan, close ();count(str);统计输入的字符p rivate static void count(String str)List list = new ArrayList();char array_Char = str. toCharArray();for(char c:array_Char)list, add (String. valueOf (c) ;/将字符作为字符串添加到list表中Collections. sort (list);/HE序for(String s:list) mt begin = list. indexOf (s);int end = list.lastlndexOf(s);索引结束统计字符数if (list. get (end) =s)System, out. printing字符+S+有+(end-begin+l)+个);【程序8】题目:求s=a+aa+aaa+aaaa+aaa的值,其中a是一个数字。例如2+22+222+2222+22222 (此时共有5个数相加),几个数相加有键盘控制。程序分析:关键是计算出每一项的值。import java. util. Scanner;p ublic class Prog8p ublic static void mam(String2 args) 以空格作为分隔符System. out. print (M求 s=a,aa+aaa+aaaa-.的值,请输入 a 的值:);Scanner scan = new Scanner (System, in). useDelimiter CWs* ) ;/int a = scan, nextlnt ();int n = scan, nextlnt ();scan, close() ;/关闭扫描器System, out. pnntln(ex pressed(2, 5)+add(2, 5);求和表达式p rivate static String exp resseddnt a, int n) StnngBuffer sb = new StringBufferO;StringBuffer subSB = new StringBuffer ();for(intsubSB = subSB. a pp end (a);sb = sb. a pp end(subSB);if(in)sb = sb. a pp end(+);sb. appe nd(=);return sb. toStringO ;/求和p rivate static long add(int a,int n)long sum = 0;long subSUM = 0;for(intsubSUM = subSUM*10+a;sum = sum+subSUM;return sum;【程序9】题目:一个数如果恰好等于它的因子之和,这个数就称为完数。例如6=1 +2+ 3.编程找出10。0以内的所有完数。P ublic class Prog9(p ublic static void mam (String 2 args) int n = 10000;compNu mber(n);/求完数p rivate static void compNu mber(int n) int count = 0;System, out . p rintIn(n+* 以内的完数:);for(int i=l;ivn+l;i+)int sum = 0;for(int j=l;jvi/2+1;j+)if(i%j)=O)sum += j;if(sum=i)System, out. print;if (count+)%5=0)System, out. printlnO;【程序10第10次落地时,题目:一球从I。米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在共经过多少米?第1。次反弹多高? import java. util. Scanner;p ublic class ProglO(p ublic static void mam (String 2 args) System, out. print C请输入小球落地时的高度和求解的次数:;Scanner scan = new Scanner (System, in). useDelimiter (*s);int h = scan.nextlnt 0;int n = scan.nextlnt 0;scan, close ();distance (h, n);小球从h高度落下,经n次反弹后经过的距离和反弹的高度P rivate static void distance(int h, int n)double length = 0;for (int i=0;in;i+) length += h; h /=2. 0 ;System, out. printin (*经过第+n+次反弹后,小球共经过+length+米,+第+建次反弹高度为+h+米);精选文库【程序11题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。P ublic class P rogll p ublic static void main(Stnng args)( int count = 0;int n = 0;for(int i=l;i5;i+)for(int j=l;jv5;j+)if(j=i)continue;for(int k=l;k1000000)profit = p rofit_sub-1000000;profit_sub = 1000000;prize y p rofit*0. 01;if(p rofit600000)profit = profit_sub-600000;p rofit_sub = 600000; prize+= p rofit*0. 015;if(p rofit400000)(profit = p rofit_sub-400000;P rofit.sub = 400000;prize += p rofit*0. 03;if(p rofit200000)profit = profit_sub-200000;profit_sub = 200000;p rize += p rize*0.05;if(p rofit100000)(profit = p rofit_sub-100000;profit_sub = 100000;prize += p rofit*0. 075; p rize += p rofit_sub*0. 1;return p nze;【程序13168 乂是一个完全平方数,请问该数是多少?题目:一个整数,它加上1。0后是一个完全平方数,再加上程序分析:在1。万以内判断,先将该数加上】。0后再开方.,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。P ublic class Progl3p ublic static void main(Stnng args)int n=0;for(int i=0;i100001;i) if (isCo mp Sqrt(i+100) & isCo mp Sqrt(i+268)break;System, out. printing 所求的数是:+n);/判断完全平方数p rivate static boolean isCo mp Sqrt(int n)boolean isCo mp = false;for(int i=l;iy则将工与y的值进行交换,程序分析:我们想办法把最小的数放到然后再用工与z进行比较,如果。z则将工与z的值进行交换,这样能使工最小。import java. util. Scanner;p ublic class Progl5p ublic static void main(String args) Scanner scan = new Scanner(System, in). useDelimiter(*D);System. out . p rint(* 请输入三个数:;int x = scan, nextlnt ();int y = scan, nextlnt ();int z = scan, nextlnt ();scan. closeO ;System. out. println(z,排序结果:+sort (x, y, z);比较两个数的大小p rivate static String sort(int x,int y,int z)String s = null;if (xy)int t = x;x = y;y = t;if(xz)int t = x;2 = t;if (yz)int t =乙 z = y;y = t;s = x+”+z;return s;【程序16题目输出9*9 口诀。程序分析:分行与列考虑,共9行9列,i控制行,j控制列。P ublic class Progl6(p ublic static void main(Stnng口 args)for (int; jH)System, out. printlnO;【程序17题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,乂多吃了一个第二天早上又将剩下的桃子吃掉一半,乂多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第1。天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。 程序分析:采取逆向思维的方法,从后往前推断。 P ublic class Progl7 p ublic static void main(String args) int m = 1; for(int i=10;i0;i) m = 2*m + 2; System. out. printing小猴子共摘了桃子”);【程序18题目两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和* z比,请编程序找出三队赛手的名单。import java. util. ArrayList; p ublic class Progl8String a,b,c;/ 甲队成员p ublic static void main(String args)String racer = (x, y, z;/乙队成员ArrayList arrayList = new ArrayList (); for (int i=0;i3;i+) for(int j=0;j3;j+) for (int k=0;k3;k+)P rogl8 p roglS = new P roglS (raceri,racer 1 j,racerk);if (! progl8.a.equals (p roglS.b) & !p roglS.a.equals (p rogl8.c)& !p rogl8. b. equals( progl8.c) &!p rogl8. a. equalsCx*) & !p roglS. c. equalsCx) & !p roglS. c. equals(z*) arrayList. add( progl8);for(Object obj:arrayList)System, out. pnntln(obj);构造方法private P rogl8(String a,String b,String c) this, a = a;this, b = b ;this, c = c;public String toStringO return a的对手是的对手是+b+ +c的对手是+c;【程序19题目打印出如下图案(菱形)* *程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。P ublic class Progl9p ublic static void main(String口 args) int n = 5;p rintStar(n);/打印星星p rivate static void p rintStar(int n)/打印上半部分精选文库for(int i=0;ivn;i+)for (int j=0; j2*n; j+) if (j=n-i & j=n+i)System, out. pnntlnO;/打印下半部分 for (int i=l;in;i+) System, out. print();for(int j=0;j2*n-i;j+)if(j=i & j2*n-i-l)System, out. print(*);System, out. pnntlnO;【程序20题目:有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13求出这个数列的前20项之和。程序分析:请抓住分子与分母的变化规律。P ublic class Prog20p ublic static void mam (String args) double nl = 1;double n2 = 1;double fraction = nl/n2;double Sn = 0;for(int i=0;i20;i+)double tl = nl;double t2 = n2;nl = tl+t2;fraction = nl/n2;Sn +: fraction;System, out. pnnt(Sn);【程序21】 题目:求1+2!+3!+20!的和 程序分析:此程序只是把累加变成了累乘。 P ublic class Prog21p ublic static void mam (String args) long sum = 0;for(int i=0;i20;i+)sum += factorial(il);System, out. pnntIn(sum);阶乘p rivate static long factorial(int n) int mult = 1;for(int i=l;i99999) System, out. print In C输入的不是 5 位数!”);return;else for(int i=0;i5;i+) ai = n%10; n /= 10;if (a0=a4 & al=atS) System. out. println(m+是一个回文数”);29elseSystem, out. pnntIn(m+“不是回文数);【程序26题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或 import java. io. *;p ublic class Prog26(p ublic static void main(String匚 args)String str = new String();BufferedReader bufln = new BufferedReader(new Inpu tStreamReader(System, in);System, out . p rint C请输入星期的英文单伺前两至四个字母):trystr = bufln. readLine 0;catch(lOExce ption e)e. p rintStackTrace ();finallytry(bufln. close ();catch(lOExce ption e) e. p rintStackTrace0;week(str);p rivate static void week(String str) int n = -1;if (str. trimO. equalslgnoreCase(31o*) str.equalsignor eCase (Mon*)str. tnm(). equalslgnoreCase CMond)n = 1;if (str. trimO. equalslgnoreCase(Tu)str. trimO. equalslgnoreCase Clue*)str. trimO. equalslgnoreCase(z*Tues*)n = 2;if (str. trimO. equalslgnoreCase (* We*)str. trimO. equalslgnoreCase (Wed*)“);判断第二个字母。if语句判断第二个字母。str. tnmO. equalslgnoreCase (z,Wedn)n = 3;if (str. tnm(). equalslgnoreCase CTh*)str. trim(). equalslgnoreCase(zxThu)str. trimO. equalslgnoreCase (Thur*)n = 4;if (str. tnm(). equalslgnoreCase CFr*)str. tnm(). equalslgnoreCaseCFn)str. trimO. equalslgnoreCase(Frid)n = 5;if (str. trimO. equalslgnoreCase C,Sa*)str. trimO. equalslgnoreCase(Sat)str. trimO. equalslgnoreCase(Satu*)n = 2;if (str. trimO. equalslgnoreCase CSu*) str. trimO. equalslgnoreCase (7 Sun*)str. trimO. equalslgnoreCase(Sund)n = 0;switch(n)case 1:System, out.printing星期一”);break;case 2:System, out.printing星期二)break;case 3:System, out.printing星期三”);break;case 4:System, out.printing星期四”);break;case 5:System, out.printing星期五)break;case 6:System, out.printing星期六)break;case 0:System, out.printing星期日)break;精选文库default:System, out. printing输入有误!);break;)【程序27题目:求1。之内的素数P ublic class Prog27 p ublic static void mam(Stnng args) int n = 100;System, out . p rint (n*以内的素数:;for(int i=2;in+l;i+)if(is Prime(i)System, out. print;/求素数p rivate static boolean isP rime(int n) boolean flag = true;for (mt i=2; iMath. sqrt (n) +1; i+)if(n%i=0)flag = false;break;return flag;【程序28题目:对10个数进行排序程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。P ublic class Prog28 p ublic static void mam (String L args) int a = new int 31, 42,21, 50,12, 60,81, 74, 101, 93);for (int i=0;iaj+l) int temp = aj;aj = aj+l;aj+l = temp;for(int 1=0;ia, length;i)System, out. print;【程序29题目:求一个3*3矩阵对角线元素之和程序分析:利用双重for循环控制输入二维数组,再将累加后输出。P ublic class Prog29(p ublic static void mam(String args) int a = new int 100, 2, 3, 4, 5, 6, 17, 8,9);matrSum(a);p rivate static void maTrSum(intj a) int suml = 0;mt sum2 = 0;for(int i=0;ia. length;1)for(int j=0;length;if(i=j) suml -= aij;if (j=a. lengthil) sum2 += aij;System, out .p rintlnC*矩阵对角线之和分别是:+suml+和sum?);【程序30题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。插入后此元素之后的数,程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,依次后移一个位置。 import java. util. Scanner; p ublic class Prog30p ublic static void main(String口 args)int A = new int 0, 8, 7, 5, 9, 1, 2, 4, 3, 12;int B = sort (A);print(B);System, out. printlnO ;System. out. print(请输入10个数的数组:);Scanner scan = new Scanner(System, in);int a = scan, nextlnt ();scan. closeO ;int C = insert(a,B);print(C);选择排序 p rivate static int sort (int A) mt B = new int A. length;for (int i=0;iAlj) int te mp = min;min = Aj;= temp;Bi = mm;BA. length-l = AA. lengthl;return B;/扪印 p rivate static void p rint (int A) for (int i=0;iO;i-)if(aA i) Bi+1 = a;for(int j=0;j=i;j+)Bj = Aj;for (int k=i+2;kvB. length;k+)Bk = Ak-1;break;return B;【程序31题目:将一个数组逆序输出。程序分析:用第一个与最后一个交换。P ublic class Prog31p ublic static void main(StringL args)int A = new mt 1, 2, 3, 4, 5, 6, 7, 8, 9,;print(A);System, out. printlnO;int B = reverse(A);print(B);p rivate static
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 营销创新


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!