java練習代碼_第1頁
java練習代碼_第2頁
java練習代碼_第3頁
java練習代碼_第4頁
java練習代碼_第5頁
已閱讀5頁,還剩23頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、東北石油大學計算機與信息技術學院李勇勇java練習代碼bookstestdrive.javaclass books(string title;string author;) public class bookstestdrive(public static void main(string args)(books mybooks = new books3;mybooks0 = new books();倉建 books 的 對象mybooksl = new books();mybooks=new books();int x = 0;my books 0. title = nenglishn;my

2、 books 1. title = math;my books 2. title = chinese;mybooks0.author = mark;mybooksl.author = tina”;mybooks2eauthor = peter;while(x3)|system.out.print(mybooksx.title +by + mybooksx.author);system.out.println(n );x = x + 1;)dog.javapublic class dogstring name;/變量有兩種:基本數據類型和引用數據類型;變量聲明時必須要有類型和名稱public s

3、tatic void main(string args)創建dog對象dog dogl = new dog(); dogl.bark(); = mark;創建dog數組dog mydogs = new dog3;數組也屬于對象,數組的引用既是對象的引用mydogs0 = new dog(); mydogsl = new dog();mydogs2 = dogl;通過數組引用存取dog = ntinan; = peter;/dog2的名字?sy stem.out.println(1 * what is the dog2fs

4、name?);system.out.println();對dog逐個執行bark()方法int x = 0;while(xmydogs.length)(mydogsx.bark();x = x + 1;public void bark()system.out.println(name + mwang wang!n); ) ) dogtestdrive.java public class dogtestdrivepublic static void main(string args)dog d = new dog();建立一個 dog 對象desize = 40;deba

5、rk();)class dog(int size;string breed;string name;void bark()system.out.println(hyou are a fool dog!) )doobee.javapublic class doobee(public static void main(string args)(int x=l;while(x 3)sy stem.out.print(f f doo);sy stem.out.print(h bee n);x+;)if(x=3)sy stem.outprint (n do n);)drumkittestdrive.j

6、ava class drumkit (boolean tophat = true;boolean snare = true;void playtophat() (system.out.println(1 fding ding da-ding);)void playsnare()(system.out.println(hbang bang ba-bangn);)class drumkittestdrive(public static void main(string args)(drumkit d = new drumkit();d.snare = false;d.playsnare();d.p

7、laytophat();if(d.snare = true)(d.playsnare();)echotestdrive.j avapublic class echotestdrive(public static void main(string args)(echo el = new echo();echo e2 = new echo();int x = 0;while(x0)(e2.count = e2.count + el.count;)x = x + 1;)system.out.println(e2.count);)class echoint count = 0;void hello()

8、(sy stem.out.println(1 f heloooo.n);)gamelauchenj avaclass guessgameplayer pl;創建3個實例變量player對象player p2;player p3;public void startgame()創建 startgame 的方法(pl = new player();p2 = new player();p3 = new player();int guesspl=o;用三個變量聲明是否數字被猜中int guessp2=0;int guessp3=0;boolean plisright = false;聲明三個變量來保存猜

9、測的數字boolean p2isright = false;boolean p3isright = false;inttargetnumber=(int)(math.random()*10);system.out.println(1 fi am thinking of a number is:while(true)(system.out.println(1 fnumber to guss is” +targetnumber);plguess();調用 player 的 guess 方法p2.guess();p3.guess();10東北石油大學計算機與信息技術學院李勇勇guesspl = pl

10、.mimber; 取出猜的數字并 且羅列出來system.out.println(1 fplayer one gussedh + guesspl);guessp2 = p2.number;sy stem.out.println(1 * player two gussedn + guessp2);guessp3 = p3.number;sy stem.out.println(1 f playerthreegussedn + guessp3);if(guesspl=targetnumber)/lj 斷所猜的 數字是否和目標數字相等(plisright = true;)if(guessp2=targ

11、etnumber) (p2isright = true;if(guessp3=targetnumber)( p3isright = true;)if(p lisright i ip2isrightl ip3isright)(sy stem.out.println(t1 wehave awinner!h);system.out.println(t fplayer one got it right?” + p lisright);system.out.println(t1 player two got it right?” + p2isright);system.out.println(mplay

12、er three got it right?” + p3isright);system.out.println(11 game is over!);break;/游戲結束終止循環 else system.out.println(f fthe game we will come on!);)東北石油大學計算機與信息技術學院李勇勇)class player(int number = 0;要被猜的數字public void guess()(number = (int)(mathrandom()*10);利用 隨機函數產生隨機數sy stem.out.println(f 11 am guessing

13、the number is + number);)/主方法public class gamelaucher(public static void main(string args)guessgame game = new guessgame();倉!j 建gussgame的對象gamegame.startgame() ;/game 對象調用 startgame的方法,開始執行游戲)helloworld.j avapublic class helloworld(public static void main(string args)system.out.println(h helloworld

14、);)hobbits.javapublic class hobbits(string name;public static void main(string args)(hobbits h = new hobbits3;int z = -1;while(z2)(z = z + 1;hz = new hobbits(); = bilbo;if(z=l)( = mark;)if(z=2)( = tina;)sy stem.out.println(hz .name + + is a good hobbits name!);)iftest.javapubli

15、c class iftest15東北石油大學計算機與信息技術學院李勇勇public static void main(string args)(int x=3;if(x=3)system.out.println(1 fx must be 3n);)system.out.println(1 fthis runs no matter what!”); ) iftest2.java public class iftest2 (public static void main(string args)(int x=2;if(x=3)system.out.println(1 fx must be 3n);

16、 else system.outprintln(mx is not 3);)東北石油大學計算機與信息技術學院李勇勇system.out.println(1 fthis runs no matter what!”);)jiecheng.javapublic class jiecheng(public static void main(string args)long result = 0;long f = 1;for(int i=l;i10;i+)result += f;system.out.println(111 至1 10 的階乘之和為:+result);)loopy.javapublic

17、class loopy(public static void main(string args)(int x=l;system.out.println(hbefore the loop);while(x4)(system.out.println(11 in the loop);system.out.println(hvalue of x is n + x); x=x+l;)sy stem.out.println(1 * this is after the loop);)oddsum.javapublic class oddsum(public static void main(string a

18、rgs)long result = 0;for(int i=l;i0)(if(x2) system.out.print(h a n);x=x-l;sy stem.out.print(n -h);if(x=2) sy stem. out. print。b cn);x=x-l;sy stem.out.print(n -h);if(x=1) system.out.print(hdn); x=x-l;)sum.javapublic class sum(public static void main(string args)(long result = 0;for(int i=l;i100;i+)res

19、ult += i;)system.out.println(h 1 到 99 的和 是:+result);) ) test.java public class test (public static void main(string args)(int num = 0,i = 1;while(i100)if(i%3=0) sy stem.out.println(n i=n+i); num+;)if(num = 5) break;i+;)testarrays.javapublic class testarrays(public static void main(string args)(string islands = new st ring 4;int index = new int4;int y = 0;islands0 = bermuda”;islandsl = fiji;islands2 = azores;islands3 = coz

溫馨提示

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

評論

0/150

提交評論