JAVA機(jī)試題庫(kù)_第1頁(yè)
JAVA機(jī)試題庫(kù)_第2頁(yè)
JAVA機(jī)試題庫(kù)_第3頁(yè)
JAVA機(jī)試題庫(kù)_第4頁(yè)
JAVA機(jī)試題庫(kù)_第5頁(yè)
已閱讀5頁(yè),還剩81頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Java 機(jī)考試題 一、第一類題 1. 編寫程序,從標(biāo)準(zhǔn)輸入讀入華氏度,將其轉(zhuǎn)換為攝氏度,并在標(biāo)準(zhǔn)輸出打印轉(zhuǎn)換結(jié)果。 轉(zhuǎn) 換公式為:攝氏度 = (5/9)*(華氏度-32)。 import javax.swing.JOptionPane; public class Exercise2_1public static void main(String args) String str = JOptionPane.showInputDialog(null,"請(qǐng)輸入華氏溫度:"); double temp = Double.parseDouble(str); double c =

2、(5.0/9)*(temp - 32); String output = "將其轉(zhuǎn)化為攝氏溫度為:" + c; JOptionPane.showMessageDialog(null,output);2. 編寫程序讀入圓柱體的半徑和高,計(jì)算圓柱的體積,并打印圓柱體的體積。 import javax.swing.JOptionPane;public class Exercise2_2 public static void main(String args) double radius, height; double area, volume; radius = Double.p

3、arseDouble(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 + ", 高

4、度為:" + height + "的圓柱體積是:" + volume); 3. 編寫程序讀入球的半徑,計(jì)算球的體積和表面積,并顯示結(jié)果。 package eayang;import javax.swing.JOptionPane;public class Test1 /* * param args */public static void main(String args) / TODO 自動(dòng)生成方法存根double r;double area,volume;r=Double.parseDouble(JOptionPane.showInputDialog(&quo

5、t;輸入半徑:");area=4*3.14*r*r;volume=4*3.14*r*r/3.0;JOptionPane.showMessageDialog(null, "此圓的面積為:"+area+" 此圓的體積:"+volume);4. 從命令行讀入一些參數(shù),打印參數(shù)個(gè)數(shù)和參數(shù)列表。 public class Test2 public static void main(String arg) for(int i=0;i<arg.length;i+) System.out.println(argi); 5. 編寫程序讀入英尺數(shù),轉(zhuǎn)換為米數(shù)

6、并顯示結(jié)果。一英尺等于 0.305 米。 import javax.swing.JOptionPane;public class Exercise2_3 public static void main(String args) String foot = JOptionPane.showInputDialog(null,"輸入英尺數(shù):"); double m = 0.305*Double.parseDouble(foot); String output = foot + " 英尺是 " + m +" 米" JOptionPane.sh

7、owMessageDialog(null,output); 6. 編寫程序?qū)蹀D(zhuǎn)換為千克。程序提示用戶輸入磅數(shù),轉(zhuǎn)換成千克并顯示結(jié)果。 一磅等于 0.454 千克。 import javax.swing.JOptionPane;public class Exercise2_4 public static void main(String args) String pond = JOptionPane.showInputDialog(null,"請(qǐng)輸入英鎊數(shù):"); String output = pond + " 英鎊是 " + Double.parse

8、Double(pond)*0.454 + "千克" JOptionPane.showMessageDialog(null,output); 7. 編寫一個(gè)程序,讀入費(fèi)用與提成率,計(jì)算提成與總費(fèi)用,例如:如果使用者鍵入 10 作為費(fèi)用,15%作為提成率,計(jì)算結(jié)果顯示 1.5 作為提成費(fèi),11.5 作為總費(fèi)用。 import javax.swing.JOptionPane;public class Exercise2_5 public static void main(String args) Double pay = Double.parseDouble(JOptionPan

9、e.showInputDialog(null,"請(qǐng)輸入費(fèi)用:"); Double tax = Double.parseDouble(JOptionPane.showInputDialog(null,"請(qǐng)輸入提成:"); String output = "提成費(fèi)為:" + pay*tax +"n總費(fèi)用是:" + (pay+pay*tax); 8. (求一個(gè)整數(shù)各位的和)編寫程序讀入 0 到 1000 之間的一個(gè)整數(shù),并將其各位數(shù)字加起來(lái)。例如整數(shù) 932,各位數(shù)字之和為 14。 / Exercise2_6.java:

10、 Summarize all digits in an integer < 1000import javax.swing.JOptionPane;public class Exercise2_6 / Main method public static void main(String args) / Read a number String numberString = JOptionPane.showInputDialog(null, "Enter an integer between 0 and 1000:", "Exercise2_6", J

11、OptionPane.QUESTION_MESSAGE); int number = Integer.parseInt(numberString); / Find all digits in number int lastDigit = number % 10; int remainingNumber = number / 10; int secondLastDigit = remainingNumber % 10; remainingNumber = remainingNumber / 10; int thirdLastDigit = remainingNumber % 10; / Obta

12、in the sum of all digits int sum = lastDigit + secondLastDigit + thirdLastDigit; / Display results System.out.println("The sum of all digits in " + number + " is " + sum); System.exit(0); 9. (將大寫字母轉(zhuǎn)換為小寫字母)編寫一個(gè)方法將大寫字母轉(zhuǎn)換為小寫字母。 public class Exercise2_7 public static void main(String

13、 args) char uppercase = 'E' int offset = (int)'a' - (int)'A' char lowercase = (char)(int)uppercase + offset); System.out.println("字符 " + uppercase + " 轉(zhuǎn)化為小寫字母為:" + lowercase); 10. (從 ASCII 求出對(duì)應(yīng)的字符)編寫程序讀入一個(gè) ASCII 碼(從 1 到 128 的整數(shù))并顯示它表示的字符。 例如,如果用戶輸入 97,程序顯示

14、字母 a。 import java.util.Scanner;public class Exercise2_8 public static void main(String args) Scanner input = new Scanner(System.in); int num = input.nextInt(); if(num<0 | num>127) System.out.println("輸入有誤!程序終止運(yùn)行"); System.exit(0); System.out.println("ASCII碼為:"+ num +"

15、的字符是: "+(char)num); 11. (判斷三角形編寫程序輸入三角形的三個(gè)邊,判斷輸入是否有效。 mport javax.swing.JOptionPane;public class Exercise3_1 public static void main(String args) double edge1 = Double.parseDouble(JOptionPane.showInputDialog(null,"輸入第一條邊長(zhǎng):"); double edge2 = Double.parseDouble(JOptionPane.showInputDial

16、og(null,"輸入第二條邊長(zhǎng):"); double edge3 = Double.parseDouble(JOptionPane.showInputDialog(null,"輸入第三條邊長(zhǎng):"); boolean isTriangle = isTriangle(edge1,edge2,edge3); String output = "Can deges " + edge1 + " , " + edge2 + " and " + edge3 + " from a triangle?

17、"+ isTriangle; JOptionPane.showMessageDialog(null,output); private static boolean isTriangle(double edge1,double edge2,double edge3) double temp; if(edge1<edge2) temp = edge1; edge1 = edge2; edge2 = temp; if(edge1<edge3) temp = edge1; edge1 = edge3; edge3 = temp; if(edge2<edge3) temp =

18、 edge2; edge2 = edge3; edge3 = temp; if(edge1<(edge2+edge3) return true; else return false; 12. (判斷一個(gè)數(shù)是否為偶數(shù))編寫程序讀入一個(gè)整數(shù)并判斷其是否為偶數(shù)。 import javax.swing.JOptionPane;public class Exercise3_2 public static void main(String args) int num = Integer.parseInt(JOptionPane.showInputDialog(null,"輸入一個(gè)整數(shù)&qu

19、ot;); String output = "Is " + num + " an even number? " +isEven(num); JOptionPane.showMessageDialog(null,output); private static boolean isEven(int num) if(num % 2 =0) return true; else return false; 13. 編寫程序輸入一個(gè)整數(shù),判斷其是否能被 5 和 6 整除,是否能被 5 或 6 整除,是否能被 5或 6 整除但不能被 5 和 6 整除。 import

20、javax.swing.JOptionPane;public class Exercise3_3 public static void main(String args) int num = Integer.parseInt(JOptionPane.showInputDialog(null,"輸入一個(gè)整數(shù)"); String output = "Is " + num + " divisible by 5 and 6 ? "+ isDivisibleBoth(num) + "nIs " + num + "

21、divisible by 5 or 6 ? "+ isDivisibleOr(num) + "nIs " + num + " divisible by 5 or 6 , but not both ? "+ isDivisible(num); JOptionPane.showMessageDialog(null,output); private static boolean isDivisible(int num) if(num%5=0 num%6=0) return true; else return false; private static

22、 boolean isDivisibleBoth(int num) if(num%5=0 && num%6=0) return true; else return false; private static boolean isDivisibleOr(int num) if(num%5=0 | num%6=0) return true; else return false; 14. 編寫程序,讀入資金額(現(xiàn)值)、年利率和年數(shù),顯示終值(將來(lái)的資金額),計(jì)算公 式如下: 終值 = 現(xiàn)值 * (1+年利率)年數(shù) import javax.swing.JOptionPane;publ

23、ic class Ex_14 public static void main(String args) double cash,rate,totalAll; int year; cash=Double.valueOf(JOptionPane.showInputDialog(null,"請(qǐng)輸入本金(現(xiàn)值):"); rate=Double.valueOf(JOptionPane.showInputDialog(null,"請(qǐng)輸入年利率(小數(shù)):"); year=Integer.valueOf(JOptionPane.showInputDialog(null,

24、"請(qǐng)輸入存款年數(shù):"); totalAll=cash*(1+rate)*year; JOptionPane.showMessageDialog(null, "連本帶息的終值為: "+totalAll); 15 (三個(gè)整數(shù)排序)編寫程序?qū)θ齻€(gè)整數(shù)排序。整數(shù)由輸入對(duì)話框讀入,并分別存入變量 num1、 num2 和 num3,對(duì)它們進(jìn)行排序,使得 num1<=num2<=num3。 import javax.swing.JOptionPane;public class Exercise3_8public static void main(Stri

25、ng args)int num1,num2,num3,temp;num1 = Integer.parseInt(JOptionPane.showInputDialog(null,"輸入第一個(gè)數(shù):");num2 = Integer.parseInt(JOptionPane.showInputDialog(null,"輸入第二個(gè)數(shù):");num3 = Integer.parseInt(JOptionPane.showInputDialog(null,"輸入第三個(gè)數(shù):");String output = num1 + " , &q

26、uot; + num2 + " , " + num3;if(num1>num2)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 +=" 排序的結(jié)果是:n" +num1 + " , " + num2 + " , " + num3;JOptionPane.showMess

27、ageDialog(null,output);16 (計(jì)算三角形的周長(zhǎng))編寫程序,讀入三角形的三邊,如果輸入有效,計(jì)算它的周長(zhǎng);否 則,顯示 輸入無(wú)效。如果任意兩邊的和大于第三邊,輸入有效。 import javax.swing.JOptionPane;public class Exercise3_9public static void main(String args)double side1,side2,side3;boolean isTriangle = false;side1 = Double.parseDouble(JOptionPane.showInputDialog(null,&

28、quot;輸入第一條邊長(zhǎng):");side2 = Double.parseDouble(JOptionPane.showInputDialog(null,"輸入第二條邊長(zhǎng):");side3 = Double.parseDouble(JOptionPane.showInputDialog(null,"輸入第三條邊長(zhǎng):");isTriangle = (side1 + side2 > side3)&&(side1 + side3 > side2)&&(side2 + side3 >side1);if(i

29、sTriangle)JOptionPane.showMessageDialog(null,"三角形的周長(zhǎng)為:" + (side1+side2+side3);elseJOptionPane.showMessageDialog(null,"輸入的數(shù)據(jù),不能組成三角形");17 (查找當(dāng)月的天數(shù))編寫程序,提示用戶輸入年和月,而后顯示該月的天數(shù)。例如,如果用戶 輸入 2000 年 2 月時(shí),應(yīng)該顯示 2000 年 2 月有 29 天。如果用戶輸入 2005 年 3 月時(shí),應(yīng)該顯示 2005 年 3 月有 31 天。 import javax.swing.JOp

30、tionPane;public class Exercise3_11public 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)|(yea

31、r% 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 (統(tǒng)計(jì)正數(shù)和負(fù)數(shù)的個(gè)數(shù)并計(jì)算這些數(shù)的平均數(shù)

32、)編寫程序,讀入個(gè)數(shù)不確定的整數(shù),求出讀人的 正數(shù)和負(fù)數(shù)個(gè)數(shù),并計(jì)算它們的總和及平均值,0 不參與計(jì)數(shù)。當(dāng)輸入為 0 時(shí), 程序結(jié)束。將平均值作為一個(gè)浮點(diǎn)數(shù)來(lái)顯示。(例如,如果輸入 1、2 和 0,平均值應(yīng)當(dāng) 為 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.parse

33、Int(JOptionPane.showInputDialog("輸入整數(shù)求平均數(shù),以 0 為結(jié)束標(biāo)志");if(num = 0)break;sum += num;output += num + " , "if(num > 0)count1 +;else count2 +;output += " 的平均數(shù)為:" + (double)sum/(count1 + count2) +"n正數(shù)的個(gè)數(shù)為:" + count1 + "n負(fù)數(shù)的個(gè)數(shù)為:" + count2;JOptionPane.sho

34、wMessageDialog(null,output);19 (千克轉(zhuǎn)換成磅)編一個(gè)顯示下列表格的程序(注意,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 stubSystem.out.println("t千克t磅");for(int i = 1;i < 200; i += 2)System.out

35、.println("t" + i + "t" + (int)(i*2.2*10)/10.0);20 (英里轉(zhuǎn)換成千米)編一個(gè)顯示下列表格的程序(注意,1 英里為 1.609 千米): 英里 千米 1 1.609 2 3.218 . 9 14.481 10 16.09 package eayangpublic class Test44 /* * param args */public static void main(String args) / TODO Auto-generated method stubSystem.out.println("

36、;t英里t千米");for(int i=1;i<=10;i+)System.out.println("t"+i+"t"+(i*1.609);21 (千克與磅互換編寫一個(gè)程序,并排顯示下列兩個(gè)表格(注意,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

37、main(String args) / TODO Auto-generated method stubSystem.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 (英里與千米互換

38、)編寫一個(gè)程序,并排顯示下列兩個(gè)表格(注意,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 stubSystem.out.println("t英里t千米t千米t英里");for(i

39、nt 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(計(jì)算將來(lái)的學(xué)費(fèi))假設(shè)今年某一大學(xué)的學(xué)費(fèi)為$10000,學(xué)費(fèi)的年增長(zhǎng)率為 5%。使用循環(huán)語(yǔ)句編寫程序,計(jì)算 10 年內(nèi)的學(xué)費(fèi)。 package eayang;public class Test47 /* * param args */public static void

40、 main(String args) / TODO Auto-generated method stubint 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 年內(nèi)的學(xué)費(fèi)為:"+sum);24 (查找最高分)編寫程序,提示用戶輸入學(xué)生的數(shù)量及每個(gè)學(xué)生的名字和得分,而后顯示最高分的學(xué)生。 import javax.swing.JOptionPane;public class Exercise4_8 /*

41、* param args */public static void main(String args) / TODO Auto-generated method stubint num = Integer.parseInt(JOptionPane.showInputDialog("輸入學(xué)生數(shù)量:");String name = ""double score = 0;String output = "最高分為:n"for(int i = 0;i < num;i+)String oneName = JOptionPane.showI

42、nputDialog("輸入姓名:");double scoreOfOne = Double.parseDouble(JOptionPane.showInputDialog("輸入分?jǐn)?shù):");if(scoreOfOne > score)score = scoreOfOne;name = oneName;else if(score = scoreOfOne)output += "姓名:" + oneName + " 分?jǐn)?shù): " + scoreOfOne + "n"output += &quo

43、t;姓名:" + name + " 分?jǐn)?shù): " + score + "n"JOptionPane.showMessageDialog(null, output);25 (查找最低分)編寫程序,提示用戶輸入學(xué)生的數(shù)量及每個(gè)學(xué)生的名字和得分,而后顯示最低分的學(xué)生。 package soft;import javax.swing.JOptionPane;public class Test3/* * param args */public static void main(String args) / TODO Auto-generated metho

44、d stubint num = Integer.parseInt(JOptionPane.showInputDialog("輸入學(xué)生數(shù)量:");String name = ""double score = 0;String output = "最低分為:n"for(int i = 0;i < num;i+)String oneName = JOptionPane.showInputDialog("輸入姓名:");double scoreOfOne = Double.parseDouble(JOptionPan

45、e.showInputDialog("輸入分?jǐn)?shù):");if(scoreOfOne <score)score = scoreOfOne;name = oneName;else if(score = scoreOfOne)output += "姓名:" + oneName + " 分?jǐn)?shù): " + scoreOfOne + "n"output += "姓名:" + name + " 分?jǐn)?shù): " + score + "n"JOptionPane.showMe

46、ssageDialog(null, output);26 編寫一個(gè)程序,產(chǎn)生 10 個(gè) 0 到 100 以內(nèi)的隨機(jī)浮點(diǎn)數(shù),計(jì)算是個(gè)數(shù)的平均值,顯示平均值,分別顯示大于和小于平均值的數(shù)的個(gè)數(shù)。 public class Ex_26 public static void main(String args) double average,sum=0,randomNum; double array=new double10; int greater=0,less=0; for(int i=0;i<=9;i+) randomNum=Math.random()*100; arrayi=randomN

47、um; sum+=randomNum; average=sum/10; for(int i=0;i<=9;i+) if(arrayi>average)greater+; if(arrayi<average)less+; System.out.println("平均值為: "+average); System.out.println("大于平均值的個(gè)數(shù)為: "+greater); System.out.println("小于平均值的個(gè)數(shù)為: "+less); 27 (查找兩個(gè)最高分)編寫程序,提示用戶輸人學(xué)生的數(shù)量及每

48、個(gè)學(xué)生的名字和得分,而后顯示獲得最高分的學(xué)生和第二高分的學(xué)生。 import javax.swing.JOptionPane;public class Exercise4_8 /* * param args */public static void main(String args) / TODO Auto-generated method stubint num = Integer.parseInt(JOptionPane.showInputDialog("輸入學(xué)生數(shù)量:");String name = ""double score = 0;Strin

49、g output = "最高分為:n"for(int i = 0;i < num;i+)String oneName = JOptionPane.showInputDialog("輸入姓名:");double scoreOfOne = Double.parseDouble(JOptionPane.showInputDialog("輸入分?jǐn)?shù):");if(scoreOfOne > score)score = scoreOfOne;name = oneName;else if(score = scoreOfOne)output

50、+= "姓名:" + oneName + " 分?jǐn)?shù): " + scoreOfOne + "n"output += "姓名:" + name + " 分?jǐn)?shù): " + score + "n"JOptionPane.showMessageDialog(null, output);28 (查找能被 5 和 6 整除的數(shù)編寫程序,顯示從 100 到 1000 之間所有能被 5 和 6 都整除的數(shù),每行顯示 10 個(gè)。 public class Exercise4_10 /* * param args */public static void main(String args) / TODO Auto-generated method stubint n = 0;System.out.println("輸出1

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論