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

提交评论