java练习代码.docx
东北石油大学计算机与信息技术学院李勇勇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 books 1. title = "math"my books 2. title = Chinese;mybooks0.author = "Mark"mybooksl.author = "Tina”;mybooks2eauthor = "Peter"while(x<3)|System.out.print(mybooksx.title +"by" + mybooksx.author);System.out.println(n );x = x + 1;)Dog.javapublic class DogString name;/变量有两种:基本数据类型和引用数据类型;变量声明时必须要有类型和名称public static void main(String args)创建dog对象Dog dogl = new Dog(); dogl.bark(); dogl.name = mark;创建dog数组Dog mydogs = new Dog3;数组也属于对象,数组的引用既是对象的引用mydogs0 = new Dog(); mydogsl = new Dog();mydogs2 = dogl;通过数组引用存取dog mydogs0.name = ntinan; mydogsl.name = peter;/dog2的名字?Sy stem.out.println(1 * what is the dog2fs name?);System.out.println(mydogs2.name);对dog逐个执行bark()方法int x = 0;while(x<mydogs.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;debark();)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 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.playTopHat();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(x<4)(el.hello();el.count = el.count + 1;if(x = 3)(e2.count = e2.count + 1;)if(x>0)(e2.count = e2.count + el.count;)X = X + 1;)System.out.println(e2.count);)class Echoint count = 0;void hello()(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;声明三个变量来保存猜测的数字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.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=targetnumber) (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(Mplayer 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 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 ");)Hobbits.javapublic class Hobbits(String name;public static void main(String args)(Hobbits h = new Hobbits3;int z = -1;while(z<2)(Z = Z + 1;hz = new Hobbits();hz.name = "bilbo"if(z=l)( hz.name = Mark;)if(z=2)(hz.name = "Tina")Sy stem.out.println(hz .name + + "is a good hobbits name!);)IfTest.javapublic 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); 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;i<10;i+)result += f;System.out.println(111 至1 10 的阶乘之和为:+result);)Loopy.javapublic class Loopy(public static void main(String args)(int x=l;System.out.println(Hbefore the loop);while(x<4)(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 args)long result = 0;for(int i=l;i<100;i+=2)(result += i;)System.out.println(111 -100 之内 的奇数和 为:+result);)PhraseOMatic.java public class PhraseOMatic (public static void main(String args)(String fibrst=rJlove" Jyou;String second= T J love "J China;String third=nIn;,loven;,Englishn;计算每组有多少个名词术语int oneLength=first.length;int twoLength=second.length;int threeLength=third.length;产生随机数字intrandl=(int)(Math.random()*oneLength);intrand2=(int)(Math.random()H:twoLength); intrand3=(mt)(Math.random()H:threeLength);组合出专家术语String phrase = firstrandl+ +secondrand2+ + thirdrand3;/输出Sy stem.out.println(1 * what we need is a + phrase);)ShuffleLjavaclass Shufflel(public static void main(String args)int x = 3;while(x>0)(if(x>2) 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;i<100;i+)result += 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(i<100)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 = Cozumel;index0 = 1;indexl = 3;index2 = 0;index3 = 2;int ref;while(y<4)ref = index y;Sy stem.out.print(f 1 islands = n);System.out.println(islandsref); y = y +1;)TestBreak.javapublic class TestBreak(public static void main(String args) (int stop = 4;for(int i=l;i<10;i+)(if(i = stop) break;System.out.println(i);24东北石油大学计算机与信息技术学院李勇勇TestC onvert.j avapublic class TestConvert(public static void main(String args)(byte bl=69;byte b2=87;byte b3 = (byte)(bl+b2);Sy stem.out.println(b3);)TestParameter.j avapublic class TestParameter(public static void main(String args)(Dog mydog = new Dog();mydog.bark(3);)public class Dogprivate:int numsofbark;Dog() ( numsoftbark=0;)Dog(int a) ( numsofbark=a;) public void bark(int a) (numsofbark=a;while(numsofbark>0)(Sy stem.out.println(1! wangwang!); numsofbark;)Dog()numsofbark=0;)TestNum.javapublic class TestNum(public static void main(String args)(for(int i=101;i<200;i+)(boolean flag = true;for(int j=2;j<i;j+)(if(i%j=O)(flag=false;break;)if(!flag) continue;)TestVar.javapublic class TestVarintj=9;public static void main(String args)int i=0;System.out.println(i);/System.out.printlnQ);) 成员变量和局部变量28