java基礎50道練習題及答案_第1頁
java基礎50道練習題及答案_第2頁
java基礎50道練習題及答案_第3頁
java基礎50道練習題及答案_第4頁
java基礎50道練習題及答案_第5頁
已閱讀5頁,還剩35頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、沃克IT教育JAVA基礎編程練習題整理:Lemon【程序1】題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子對數為多少? 程序分析: 兔子的規(guī)律為數列1,1,2,3,5,8,13,21. public class Prog1public static void main(String args)int n = 10;System.out.println("第"+n+"個月兔子總數為"+fun(n);private static int fun(int n)if(n=1

2、 | n=2) return 1;else return fun(n-1)+fun(n-2);【程序2】題目:判斷101-200之間有多少個素數,并輸出所有素數。程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除,則表明此數不是素數,反之是素數。public class Prog2public static void main(String args)int m = 1;int n = 1000;int count = 0;/統(tǒng)計素數個數for(int i=m;i<n;i+)if(isPrime(i)count+;System.out.print(i+&quo

3、t; ");if(count%10=0)System.out.println();System.out.println();System.out.println("在"+m+"和"+n+"之間共有"+count+"個素數");/判斷素數private static boolean isPrime(int n)boolean flag = true;if(n=1) flag = false;elsefor(int i=2;i<=Math.sqrt(n);i+)if(n%i)=0 | n=1)flag

4、= false;break; else flag = true; return flag;【程序3】題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等于該數本身。例如:153是一個"水仙花數",因為153=1的三次方5的三次方3的三次方。 程序分析:利用for循環(huán)控制100-999個數,每個數分解出個位,十位,百位。public class Prog3public static void main(String args)for(int i=100;i<1000;i+)if(isLotus(i)

5、System.out.print(i+" ");System.out.println();/判斷水仙花數private static boolean isLotus(int lotus)int m = 0;int n = lotus;int sum = 0;m = n/100;n -= m*100;sum = m*m*m;m = n/10;n -= m*10;sum += m*m*m + n*n*n;if(sum=lotus)return true;elsereturn false;【程序4】題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。程序分析

6、:對n進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成:(1)如果這個質數恰等于n,則說明分解質因數的過程已經結束,打印出即可。(2)如果n<>k,但n能被k整除,則應打印出k的值,并用n除以k的商,作為新的正整數n,重復執(zhí)行第一步。(3)如果n不能被k整除,則用k+1作為k的值,重復執(zhí)行第一步。public class Prog4public static void main(String args)int n = 13;decompose(n);private static void decompose(int n)System.out.print(n+"

7、=");for(int i=2;i<n+1;i+)while(n%i=0 && n!=i)n/=i;System.out.print(i+"*");if(n=i)System.out.println(i);break;【程序5】題目:利用條件運算符的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。程序分析:(a>b)?a:b這是條件運算符的基本例子。 public class Prog5public static void main(String args)int n = -1;

8、tryn = Integer.parseInt(args0);catch(ArrayIndexOutOfBoundsException e)System.out.println("請輸入成績");return;grade(n);/成績等級計算private static void grade(int n)if(n>100 | n<0) System.out.println("輸入無效");else String str = (n>=90)?"分,屬于A等":(n>60)?"分,屬于B等":&

9、quot;分,屬于C等"); System.out.println(n+str);【程序6】題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。程序分析:利用輾除法。public class Prog6public static void main(String args)int m,n;trym = Integer.parseInt(args0);n = Integer.parseInt(args1);catch(ArrayIndexOutOfBoundsException e)System.out.println("輸入有誤");return;max_mi

10、n(m,n);/求最大公約數和最小公倍數private static void max_min(int m, int n)int temp = 1;int yshu = 1;int bshu = m*n;if(n<m)temp = n;n = m;m = temp;while(m!=0)temp = n%m;n = m;m = temp;yshu = n;bshu /= n;System.out.println(m+"和"+n+"的最大公約數為"+yshu);System.out.println(m+"和"+n+"的最

11、小公倍數為"+bshu);【程序7】題目:輸入一行字符,分別統(tǒng)計出其中英文字母、空格、數字和其它字符的個數。程序分析:利用while語句,條件為輸入的字符不為'n'.import java.util.Scanner;public class Prog7_1public static void main(String args)System.out.print("請輸入一串字符:");Scanner scan = new Scanner(System.in);String str = scan.nextLine();/將一行字符轉化為字符串scan.

12、close();count(str);/統(tǒng)計輸入的字符數private static void count(String str)String E1 = "u4e00-u9fa5"/漢字String E2 = "a-zA-Z"String E3 = "0-9"String E4 = "s"/空格int countChinese = 0;int countLetter = 0;int countNumber = 0;int countSpace = 0;int countOther = 0;char array_Ch

13、ar = str.toCharArray();/將字符串轉化為字符數組String array_String = new Stringarray_Char.length;/漢字只能作為字符串處理for(int i=0;i<array_Char.length;i+) array_Stringi = String.valueOf(array_Chari);/遍歷字符串數組中的元素for(String s:array_String)if(s.matches(E1) countChinese+;else if(s.matches(E2) countLetter+;else if(s.matche

14、s(E3) countNumber+;else if(s.matches(E4) countSpace+;else countOther+;System.out.println("輸入的漢字個數:"+countChinese);System.out.println("輸入的字母個數:"+countLetter);System.out.println("輸入的數字個數:"+countNumber);System.out.println("輸入的空格個數:"+countSpace);System.out.println

15、("輸入的其它字符個數:"+countSpace);import java.util.*;public class Prog7_2public static void main(String args) System.out.println("請輸入一行字符:"); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); scan.close(); count(str);/統(tǒng)計輸入的字符private static void count(String str)List<

16、;String> list = new ArrayList<String>();char array_Char = str.toCharArray();for(char c:array_Char) list.add(String.valueOf(c);/將字符作為字符串添加到list表中Collections.sort(list);/排序for(String s:list)int begin = list.indexOf(s);int end = list.lastIndexOf(s);/索引結束統(tǒng)計字符數if(list.get(end)=s) System.out.prin

17、tln("字符"+s+"有"+(end-begin+1)+"個");【程序8】題目:求s=a+aa+aaa+aaaa+aa.a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。 程序分析:關鍵是計算出每一項的值。import java.util.Scanner;public class Prog8public static void main(String args)System.out.print("求s=a+aa+aaa+aaaa+.的值,請輸入a的值:&qu

18、ot;);Scanner scan = new Scanner(System.in).useDelimiter("s*");/以空格作為分隔符int a = scan.nextInt();int n = scan.nextInt();scan.close();/關閉掃描器System.out.println(expressed(2,5)+add(2,5); /求和表達式private static String expressed(int a,int n)StringBuffer sb = new StringBuffer();StringBuffer subSB = ne

19、w StringBuffer();for(int i=1;i<n+1;i+) subSB = subSB.append(a); sb = sb.append(subSB); if(i<n) sb = sb.append("+");sb.append("=");return sb.toString();/求和private static long add(int a,int n)long sum = 0;long subSUM = 0;for(int i=1;i<n+1;i+)subSUM = subSUM*10+a;sum = sum+

20、subSUM;return sum;【程序9】題目:一個數如果恰好等于它的因子之和,這個數就稱為"完數"。例如6=123.編程找出1000以內的所有完數。public class Prog9public static void main(String args)int n = 10000;compNumber(n);/求完數private static void compNumber(int n)int count = 0;System.out.println(n+"以內的完數:");for(int i=1;i<n+1;i+)int sum = 0

21、;for(int j=1;j<i/2+1;j+)if(i%j)=0)sum += j;if(sum=i) System.out.print(i+" "); if(count+)%5=0) System.out.println(); 【程序10】題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?import java.util.Scanner;public class Prog10public static void main(String args)System.out.print(&quo

22、t;請輸入小球落地時的高度和求解的次數:");Scanner scan = new Scanner(System.in).useDelimiter("s");int h = scan.nextInt();int n = scan.nextInt();scan.close();distance(h,n);/小球從h高度落下,經n次反彈后經過的距離和反彈的高度private static void distance(int h,int n)double length = 0;for(int i=0;i<n;i+)length += h;h /=2.0 ;Syst

23、em.out.println("經過第"+n+"次反彈后,小球共經過"+length+"米,"+"第"+n+"次反彈高度為"+h+"米");【程序11】題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?程序分析:可填在百位、十位、個位的數字都是1、2、3、4。組成所有的排列后再去 掉不滿足條件的排列。public class Prog11public static void main(String args)int count = 0;int

24、n = 0;for(int i=1;i<5;i+)for(int j=1;j<5;j+)if(j=i) continue;for(int k=1;k<5;k+)if(k!=i && k!=j)n = i*100+j*10+k; System.out.print(n+" "); if(+count)%5=0) System.out.println();System.out.println();System.out.println("符合條件的數共:"+count+"個");【程序12】題目:企業(yè)發(fā)放的獎

25、金根據利潤提成。利潤(I)低于或等于10萬元時,獎金可提10%;利潤高于10萬元,低于20萬元時,低于10萬元的部分按10%提成,高于10萬元的部分,可可提成7.5%;20萬到40萬之間時,高于20萬元的部分,可提成5%;40萬到60萬之間時高于40萬元的部分,可提成3%;60萬到100萬之間時,高于60萬元的部分,可提成1.5%,高于100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發(fā)放獎金總數? 程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。import java.io.*;public class Prog12public static void

26、 main(String args)System.out.print("請輸入當前利潤:");long profit = Long.parseLong(key_Input();System.out.println("應發(fā)獎金:"+bonus(profit);/接受從鍵盤輸入的內容private static String key_Input()String str = null;BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in);trystr = bufIn

27、.readLine();catch(IOException e)e.printStackTrace();finallytrybufIn.close();catch(IOException e)e.printStackTrace();return str;/計算獎金private static long bonus(long profit)long prize = 0;long profit_sub = profit;if(profit>1000000)profit = profit_sub-1000000;profit_sub = 1000000;prize += profit*0.01

28、;if(profit>600000)profit = profit_sub-600000;profit_sub = 600000;prize += profit*0.015; if(profit>400000)profit = profit_sub-400000;profit_sub = 400000;prize += profit*0.03;if(profit>200000)profit = profit_sub-200000;profit_sub = 200000;prize += prize*0.05;if(profit>100000)profit = profi

29、t_sub-100000;profit_sub = 100000;prize += profit*0.075;prize += profit_sub*0.1;return prize;【程序13】題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?程序分析:在10萬以內判斷,先將該數加上100后再開方,再將該數加上268后再開方,如果開方后的結果滿足如下條件,即是結果。public class Prog13public static void main(String args)int n=0;for(int i=0;i<100001;i+)if

30、(isCompSqrt(i+100) && isCompSqrt(i+268)n = i;break;System.out.println("所求的數是:"+n);/判斷完全平方數private static boolean isCompSqrt(int n)boolean isComp = false;for(int i=1;i<Math.sqrt(n)+1;i+)if(n=Math.pow(i,2)isComp = true;break;return isComp;【程序14】題目:輸入某年某月某日,判斷這一天是這一年的第幾天?程序分析:以3月5日

31、為例,應該先把前兩個月的加起來,然后再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大于3時需考慮多加一天。import java.util.Scanner;public class Prog14public static void main(String args)Scanner scan = new Scanner(System.in).useDelimiter("D");/匹配非數字System.out.print("請輸入當前日期(年-月-日):");int year = scan.nextInt();int month = scan.next

32、Int();int date = scan.nextInt();scan.close();System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");/判斷天數private static int analysis(int year, int month, int date)int n = 0;int month_date = new int 0,31,28,31,30,31,30,31,31,30,31,30;if(year%400)=0 | (year%4)

33、=0)&&(year%100)!=0) month_date2 = 29;for(int i=0;i<month;i+) n += month_datei;return n+date;【程序15】題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。程序分析:我們想辦法把最小的數放到x上,先將x與y進行比較,如果x>y則將x與y的值進行交換,然后再用x與z進行比較,如果x>z則將x與z的值進行交換,這樣能使x最小。import java.util.Scanner;public class Prog15public static void main(Strin

34、g args)Scanner scan = new Scanner(System.in).useDelimiter("D");System.out.print("請輸入三個數:");int x = scan.nextInt();int y = scan.nextInt();int z = scan.nextInt();scan.close();System.out.println("排序結果:"+sort(x,y,z);/比較兩個數的大小private static String sort(int x,int y,int z)Stri

35、ng s = null;if(x>y)int t = x;x = y;y = t;if(x>z)int t = x;x = z;z = t;if(y>z)int t = z;z = y;y = t;s = x+" "+y+" "+z;return s;【程序16】題目:輸出9*9口訣。程序分析:分行與列考慮,共9行9列,i控制行,j控制列。public class Prog16public static void main(String args)for(int i=1;i<10;i+)for(int j=1;j<i+1;j

36、+)System.out.print(j+"*"+i+"="+(j*i)+" ");System.out.println();【程序17】題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。程序分析:采取逆向思維的方法,從后往前推斷。public class Prog17public static void main(String args)int m

37、= 1; for(int i=10;i>0;i-) m = 2*m + 2; System.out.println("小猴子共摘了"+m+"桃子");【程序18】題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。 import java.util.ArrayList;public class Prog18String a,b,c;/甲隊成員public static void main(String args

38、)String racer = "x","y","z"/乙隊成員ArrayList<Prog18> arrayList = new ArrayList<Prog18>();for(int i=0;i<3;i+) for(int j=0;j<3;j+) for(int k=0;k<3;k+) Prog18 prog18 = new Prog18(raceri,racerj,racerk); if(!prog18.a.equals(prog18.b) && !prog18.a.eq

39、uals(prog18.c) && !prog18.b.equals(prog18.c) && !prog18.a.equals("x") && !prog18.c.equals("x") && !prog18.c.equals("z") arrayList.add(prog18); for(Object obj:arrayList) System.out.println(obj);/構造方法private Prog18(String a,String b,String

40、c)this.a = a;this.b = b ;this.c = c;public String toString()return "a的對手是"+a+" "+"b的對手是"+b+" "+"c的對手是"+c;【程序19】題目:打印出如下圖案(菱形)    *   *  * *  *   *    * 程序分析:先把圖形分成兩部分來看待,前四行一個規(guī)律,后三行一個規(guī)律,利用雙重 for循環(huán),第一層

41、控制行,第二層控制列。public class Prog19public static void main(String args)int n = 5;printStar(n);/打印星星private static void printStar(int n)/打印上半部分for(int i=0;i<n;i+)for(int j=0;j<2*n;j+) if(j<n-i) System.out.print(" "); if(j>=n-i && j<=n+i) System.out.print("*"); Sy

42、stem.out.println();/打印下半部分for(int i=1;i<n;i+)System.out.print(" ");for(int j=0;j<2*n-i;j+)if(j<i) System.out.print(" "); if(j>=i && j<2*n-i-1) System.out.print("*");System.out.println();【程序20】題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13.求出這個數列的前20項之和。程序分析

43、:請抓住分子與分母的變化規(guī)律。public class Prog20public static void main(String args)double n1 = 1;double n2 = 1;double fraction = n1/n2;double Sn = 0;for(int i=0;i<20;i+) double t1 = n1; double t2 = n2; n1 = t1+t2; n2 = t1; fraction = n1/n2; Sn += fraction; System.out.print(Sn);【程序21】題目:求1+2!+3!+.+20!的和 程序分析:此

44、程序只是把累加變成了累乘。public class Prog21public static void main(String args)long sum = 0;for(int i=0;i<20;i+) sum += factorial(i+1);System.out.println(sum);/階乘private static long factorial(int n)int mult = 1;for(int i=1;i<n+1;i+) mult *= i;return mult;【程序22】題目:利用遞歸方法求5!。程序分析:遞歸公式:fn=fn_1*4!public clas

45、s Prog22public static void main(String args)System.out.println(fact(10);/遞歸求階乘private static long fact(int n)if(n=1) return 1;else return fact(n-1)*n;【程序23】題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后問第一個人,他說是10歲。請問第五個人多大? 程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲

46、數,需知道第四人的歲數,依次類推,推到第一人(10歲),再往回推。public class Prog23public static void main(String args)System.out.println(getAge(5,2);/求第m位同志的年齡private static int getAge(int m,int n)if(m=1) return 10;else return getAge(m-1,n)+n;【程序24】題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。public class Prog24public static void mai

47、n(String args)int n = Integer.parseInt(args0); int i = 0;int a = new int5;doai = n%10; n /= 10; +i;while(n!=0);System.out.print("這是一個"+i+"位數,從個位起,各位數字依次為:");for(int j=0;j<i;j+) System.out.print(aj+" ");【程序25】題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。import java.i

48、o.*;public class Prog25public static void main(String args)int n = 0;System.out.print("請輸入一個5位數:");BufferedReader bufin = new BufferedReader(new InputStreamReader(System.in);try n = Integer.parseInt(bufin.readLine();catch(IOException e)e.printStackTrace();finallytry bufin.close();catch(IOE

49、xception e)e.printStackTrace();palin(n);private static void palin(int n)int m = n;int a = new int5;if(n<10000 | n>99999)System.out.println("輸入的不是5位數!");return;else for(int i=0;i<5;i+) ai = n%10; n /= 10; if(a0=a4 && a1=a3) System.out.println(m+"是一個回文數"); else Sys

50、tem.out.println(m+"不是回文數"); 【程序26】題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續(xù) 判斷第二個字母。程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。import java.io.*;public class Prog26public static void main(String args)String str = new String(); BufferedReader bufIn = new BufferedReader(new InputStreamReader(S

51、ystem.in); System.out.print("請輸入星期的英文單詞前兩至四個字母):"); try str = bufIn.readLine(); catch(IOException e) e.printStackTrace(); finally try bufIn.close(); catch(IOException e) e.printStackTrace(); week(str);private static void week(String str)int n = -1;if(str.trim().equalsIgnoreCase("Mo&qu

52、ot;) | str.trim().equalsIgnoreCase("Mon") | str.trim().equalsIgnoreCase("Mond") n = 1;if(str.trim().equalsIgnoreCase("Tu") | str.trim().equalsIgnoreCase("Tue") | str.trim().equalsIgnoreCase("Tues") n = 2; if(str.trim().equalsIgnoreCase("We"

53、) | str.trim().equalsIgnoreCase("Wed") | str.trim().equalsIgnoreCase("Wedn") n = 3;if(str.trim().equalsIgnoreCase("Th") | str.trim().equalsIgnoreCase("Thu") | str.trim().equalsIgnoreCase("Thur") n = 4; if(str.trim().equalsIgnoreCase("Fr") | str.tr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論