

下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Java 練習題、運行程序寫結果1.當在命令提示符后輸入 ”ava SumT est 4 5 ”之后,下列程序的輸出結果為:public class SumTestpublic static void main( Stri ng args)int a, b, sum;a=ln teger.parse In t(argsO);b=ln teger.parse In t(args1);sum=a+b;System.out.pri ntln(a + + + b + = + sum);2.當在命令提示符后輸入”ava PrintStar 8 ”之后,下列程序的輸出結果為:public class Pr
2、in tStar public static void main(String args) int n = In teger.parse In t(args0);int i;for( i=1; i=n; i+) System.out.pri nt(*);System.out.pri ntl n();3.當在命令提示符后輸入 java CalculateTest 5.5 之后,下列程序的輸出結果為:public class CalculateTestpublic static void main(String args) float x,y;x = Float.parseFloat(args0)
3、; if(x1)y = x;else if(1=x&x10)y = 2*x-1;2elsey = 3*x-11;System.out.pri ntl n( y);4.當在命令提示符后輸入” java Factorial 3”之后,下列程序的輸出結果為:public class Factorial3 public static void main( Stri ng args) int num = In teger.parse In t(args0);int result = 1;if(nu m1)System.out.pri ntl n(” 應輸入大于 1 的整數!); elseint
4、i;for( i=1; i=num; i+)result = result*i;System.out.pri ntln (結果是:+result);5._下歹 y 程序的輸出結果為:_public class Xpublic static void main( Stri ng args)String names=new String5;for(i nt x=0;xargsen gth;x+)n amesx=argsx;System.out.pri ntln(n ames2);6._下歹 u 程序的輸出結果為:public class Testpublic static void main(St
5、ring args) System.out.pri nt ( 3.5+1/2+56%10+);System.out.pri nt ( 3.5+1.0/2+56%10+);int a = 4%3*7+1;3System.out.pri ntln( a );7. 下歹 y 程序的輸出結果為:_public class A1 public static void main(String args) int a= new int 5;a0=1;a1=2;a2=3;a3=4;a4=5;System.out.print(a+0+ = +a0+);System.out.pri nt(a+1+ = +a1+)
6、;System.out.pri ntl n( a+2+ = +a2+);System.out.pri nt(a+3+ = +a3+);System.out.pri nt(a+4+ = +a4);8. 下歹 y 程序的輸出結果為: _public class array1D_1 public static void main(String args) int i;int a;a = new in t5;for(i=0;i=O;i_)System.out.print(“ a “ +i + ”= t + i +”,9. 運行結果是:_public class Test1 Stri ng str =
7、new Stri ng(Hi !);char ch = L, i, k, e ;public static void main(String args) Test1 ex = new Test1();System. out .print(ex.str + );System. out .print(ex.ch);10._運行結果是:public class Test3 4public static void main( Stri ng args) int i;for (i = 3; i = 20; i +) if (i % 2) = 0)System. out .print(i + );11._
8、 運行結果是:public class Test4 public static void main(String args) int i,sum=0;for (i = 3; i = 20; i +)if (i % 2) = 0) sum=sum+i;System. out.print(i=+i+,sum=+sum);12._ 運行結果是:public class Test5 public static void main(String args) int i,sum=0;for (i = 1; i = 100; i +) if (i % 2) = 1) sum=sum+i;System.out
9、.pri ntln( sum);13.運行結果是class Test6 public static void main( Stri ng args) int i;5for(i=0;i10;i+)System.out.pri nt(i+);if (i%2!=0) continue;System.out.pri ntln(” );14、運行結果是: public class Test14 public static void main(String args) int n = 1, m, j, i;for (i = 3; i = 30; i += 2) m = (int) Math.sqrt(do
10、uble) i);for (j = 2; j = m + 1) System.out.pri nt(i + );if (n % 5 = 0)System.out.pri nt(n);n+;15. 下歹 y 程序的輸出結果為: _public class Testi public static void main(String args) int y, x=1, total=0;while(x=10) y=x*x;System.out.pri nt(y+);total+=y;+x;System.out.pri nt(total is +total);616. 下歹 y 程序的輸出結果為: _pu
11、blic class WhileExamplepublic static void main(String args)String copyFromMe=Copy every letter un til you encoun ter u.;Stri ngBufer copyToMe=new Stri ngBuffer();/ 創(chuàng)建一個空的串變量int i=0;char c=copyFromMe.charAt(i);while(c!=u) copyToMe.appe nd(c);c=copyFromMe.charAt(+i); System.out.pri ntl n( copyToMe);17
12、. 下歹 y 程序的輸出結果為: _public class BreakExamplepublic static void main(String args)in t array=32,87,3,589,12,1076,2000,8,622;int search = 1076, i=0, j=0;boolea n foun dIt = false;for (i=0; i3; i+) for (j=0; j3; j+)if (arrayij = search) foun dIt = true; break; / 跳出內層循環(huán)if(foundlt) break; / 跳出外層循環(huán)if(fou nd
13、lt)System.out.println(Found + search + at i = + i +, j = + j); elseSystem.out.println(search + is not in the array);18. 下歹 y 程序的輸出結果為: _public class SumDemo2 public static void main( Stri ng args) int sum = 0, i = 0;doif(i%2=0)sum = sum+i;i+;while(i=20);System.out.pri ntl n(其最終和是:+sum);719. 下歹 u 程序的
14、輸出結果為:_public class AddTest public static void main(String args) int nu m1, nu m2;num1 = 10;num2 = 20;System.out.pri ntln(n um1 + + + num2 + = + add( num1, num2); static int add(i nt op1,i nt op2)retur n op1+op2;20._ 下歹 y 程序的輸出結果為: _ class gen eralFunctionpublic static int add(int x,int y) return x+
15、y; 靜態(tài)方法public class StaticF un Testpublic static void main(String args)int c=ge neral Function. add(7, 8);System.out.pri nt(7 + 8 = + c+“,“);gen eralF unction fun=new generalF un cti on();int d= fun.add(5, 8);System.out.pri ntln(5 + 8 = + d);21. 下歹 y 程序的輸出結果為 _class Boxint len gth,width,height;publi
16、c void set In fo(i nt l,i nt w,i nt h)len gth = l;width = w;height = h;public int volu mn()retur n len gth*width*height;public int area()return (le ngth*width + len gth*height + width*height) * 2;8public Stri ng toStri ng()return Le ngth: + len gth + width: + width + height: + height+ volum n: + vol
17、u mn() + area: + area();public class BoxTest public static void main(String args) Box b = new Box();b.setl nfo(5,2,4);System.out.pri ntl n( b.toStri ng();22. 下歹 y 程序的輸出結果為: _public class ExampleString str=new Strin g(good);charch=a,b,c;public static void main(String args)Example ex=new Example。;ex.c
18、ha nge(ex.str,ex.ch);9System.out.pri nt(ex.str+ and ”);System.out.pri nt(ex.ch);public void change(String str,char ch)str=test ok;ch0=g:23. 下歹 y 程序的輸出結果為: _arrayParam1.java以數組為參數的方法調用public class arrayParam1public static void mai n(Stri ng args )int a = 8, 3, 7, 88, 9, 23;LeastNumb MinNu mber= new L
19、eastNumb();MinNu mber.least(a);class LeastNumbpublic void least(i nt array )int temp = array0;for(i nt i=0;iarrayi) temp = arrayi;System.out.println(” 最小的數為:+ temp);24. 運行結果是:(B6)(p50 例 4.11)import java.awt.*; import java.applet.*; class MemberVar static int sn = 30; final int fn; final int fk = 40;
20、 MemberVar() 10fn = + sn;public class TestIO exte nds Applet public void pain t(Graphics g) MemberVar obj1 = new MemberVar();MemberVar obj2 = new MemberVar();g.drawString(obj1.fn= + obj1.fn, 20, 30);g.drawStri ng(obj1.fk= + obj1.fk, 20, 50);g.drawStri ng(obj2.fn= + obj2.fn, 20, 70);g.drawStri ng(obj
21、2.fk= + obj2.fk, 20, 90);25. 下歹 y 程序的輸出結果為: _class Astatic int y=3;void showy( )System.out.pri ntln (y=+y); class testApublic static void main(String aaa) A a1= new A();A.y+=1;a1.y+;a1.showy();26. 運行結果是:_public class Test9 static boolea n foo(char c) System.out.pri nt(c);return true;public static vo
22、id main( String argv ) int i =0;for ( foo(B); foo(A)&(i2); foo(C)i+ ;11foo(D);27._運行結果是:import java.io.*;public class Test2public staticvoid main( Stri ng args) Sub sb = new Sub();System.out.pri ntln( sb.method( );class Fatherint a=3 , b=2 ;int method() return a-b; class Subexte ndsFatherint met
23、hod2( ) return a+b; 28._下歹 y 程序的輸出結果為 _class Aclassvoid go()System.out.pri ntl n( Aclass);public class Bclass exte nds Aclassvoid go()System.out.pri ntl n( Bclass);public static void main(String args) Aclass a=new Aclass(); Aclass a1= newBclass();12a.go();a1.go();29. 下歹 u 程序的輸出結果為:_class Ball privat
24、e double r;public void setR(double x)r = x;public double getR()return r;class Billiards exte nds Ballprivate String color;public void setCol(Stri ng col)color = col;public void show()System.out.println(” 臺球的顏色是+color+,半徑是+getR();public class Demopublic static void main(String args)Ball b1 = new Ball
25、();b1.setR(10);System.out.println(” 球的半徑是+b1.getR();Billiards b2 = new Billiards();b2.setR(5);b2.setCol(藍色);b2.show();30. 運行結果是:public class Test12 exte nds TTpublic static void main(String args) 13Test12 t = new Test12 (Tom);public Test12(Stri ng s)super(s);System.out.pri ntln (How do you do?);publ
26、ic Test12()this(I am Tom);class TTpublic TT()System.out.pri ntln (What a pleasure!);public TT(Stri ng s)this();System.out.println(I am +s);31.運行結果是: public class Test13 exte nds TTpublic static void main(String args) Test13 t = new Test13 ();public Test13(Stri ng s)super(s);System.out.pri ntln( How
27、do you do?);public Test13()this(I am Tom);class TT14public TT()System.out.pri ntln (What a pleasure!);public TT(Stri ng s)this();System.out.println(I am +s);32. 下列程序的輸出結果為 :_in terface IShapepublic double area();class square impleme nts IShapedouble len gth ;public square(double l)len gth = l;public
28、 double area()retur n len gth*le ngth;public class Test public static void main(String args) square s = new square(5);System.out.pri ntl n( s.area();33. 運行結果是:_class Test11class Dog private String name;private int age;public int step;Dog(Stri ng s, i nt a) n ame = s;age = a;step = 0;public void run
29、(Dog fast) 15fast.step+;public static void main(String args) Test11 a = new Test11();Dog d = a. new Dog(Kate, 5);d.step = 35;d.ru n(d);System. out .println( + d.step);34._運行結果是:public class Test15 private static int count;private String name;public class Stude nt private int count;private String nam
30、e;public void Output(i nt count) coun t+;this.co un t+;Test15.co un t+;Test15.this.co un t+;System.out.pri ntl n(count + + this.co unt + +Test15.cou nt + + Test15.this.cou nt);public Student aStu() return new Stude nt();public static void main(String args) Test15 g3 = new Test15();g3.cou nt = 10;Tes
31、t9.Stude nt s1 = g3.aStu();16s1.0utput(5);35. 下歹 y 程序的輸出結果為: _public class Q1public static void main(String args)trythrow new MyExceptio n();catch(Excepti on e)System.out.println(It s caught! ” );Fi nallySystem.out.pri ntl n(It s fin ally caught! ” );class MyExcepti on exte nds Excepti on36. 運行結果是:p
32、ublic class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();catch(Excepti on e)System.out.pri ntln (exceptio nOOO);fin allySystem.out.pri ntln (fi nally111);public static void main(String args)tryaMethod();catch(Excepti on e)17System.out.pri ntl n(exceptio n);System.o
33、ut.pri ntln (fi ni shed);37. 運行結果是:public class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();fin allySystem.out.pri ntln (fi nally111);public static void main(String args)tryaMethod();catch(Excepti on e)System.out.pri ntl n(excepti on);System.out.pri ntln (fi ni sh
34、ed);38. 運行結果是:public class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();18catch(Excepti on e)System.out.pri ntln (exceptio nOOO);public static void main(String args)tryaMethod();catch(Excepti on e)System.out.pri ntl n(exceptio n); System.out.pri ntln (fi ni shed);39._運行結果是:public class Test1 public static String output =;public static void foo(i nt i)tryif(i=1)throw new Exception(); output += 1
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學習動力激發(fā)輔導考核試卷
- 舊貨零售店鋪選址與商圈分析考核試卷
- 拉丁語基礎與古羅馬文化考核試卷
- 智能材料設計與制造考核試卷
- 小學生經典誦讀愛國課件
- 智能餐飲顧客服務系統(tǒng)考核試卷
- ehs之家安全培訓課件
- 施工安全合同范本
- 城管部門采購合同范本
- 貨物拉運合同范本
- 《瘋狂動物城》全本臺詞中英文對照
- 建筑施工安全管理及揚塵治理檢查投標方案(技術方案)
- 六年級毛筆書法教案(下冊)
- 秘魯農村公路
- 五年級下冊勞動全冊教案人教版貴州人民出版社
- 吉利質量協(xié)議
- 空調系統(tǒng)的應急預案
- 2023玻纖增強聚氨酯門窗工程技術規(guī)程
- 急性化膿性中耳炎課件
- 食堂食品安全隱患排查報告
- 汽車維修廠車輛進出廠登記制度
評論
0/150
提交評論