欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOCX文档下载  

    java练习代码.docx

    • 资源ID:11695869       资源大小:36.60KB        全文页数:28页
    • 资源格式: DOCX        下载积分:6
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要6
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    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

    注意事项

    本文(java练习代码.docx)为本站会员(大张伟)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    经营许可证编号:宁ICP备18001539号-1

    三一文库
    收起
    展开