java练习代码_第1页
java练习代码_第2页
java练习代码_第3页
java练习代码_第4页
java练习代码_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

东北石油大学计算机与信息技术学院 李勇勇 1 BooksTestDrive java class Books String title String author public class BooksTestDrive public static void main String args Books mybooks new Books 3 mybooks 0 new Books 创建 books 的对象 mybooks 1 new Books mybooks 2 new Books int x 0 mybooks 0 title english mybooks 1 title math mybooks 2 title chinese mybooks 0 author Mark mybooks 1 author Tina mybooks 2 author Peter while x 3 System out print mybooks x title by mybooks x author System out println x x 1 Dog java public class Dog String name 变量有两种 基本数据类型和引用数据类型 变量声明时必须要有类型 和名称 public static void main String args 创建 dog 对象 Dog dog1 new Dog dog1 bark 东北石油大学计算机与信息技术学院 李勇勇 2 dog1 name mark 创建 dog 数组 Dog mydogs new Dog 3 数组也属于对象 数组的引用既是对象的引用 mydogs 0 new Dog mydogs 1 new Dog mydogs 2 dog1 通过数组引用存取 dog mydogs 0 name tina mydogs 1 name peter dog2 的名字 System out println what is the dog2 s name System out println mydogs 2 name 对 dog 逐个执行 bark 方法 int x 0 while x mydogs length mydogs x bark x x 1 public void bark System out println name wang wang DogTestDrive java public class DogTestDrive public static void main String args Dog d new Dog 建立一个 dog 对象 d size 40 d bark class Dog int size 东北石油大学计算机与信息技术学院 李勇勇 3 String breed String name void bark System out println you are a fool dog DooBee java public class DooBee public static void main String args int x 1 while x 3 System out print Doo System out print Bee x if x 3 System out print Do DrumKitTestDrive java class DrumKit boolean topHat true boolean snare true void playTopHat System out println ding ding da ding void playSnare System out println bang bang ba bang class DrumKitTestDrive public static void main String args 东北石油大学计算机与信息技术学院 李勇勇 4 DrumKit d new DrumKit d snare false d playSnare d playTopHat if d snare true d playSnare EchoTestDrive java public class EchoTestDrive public static void main String args Echo e1 new Echo Echo e2 new Echo int x 0 while x0 e2 count e2 count e1 count x x 1 System out println e2 count class Echo int count 0 void hello System out println heloooo 东北石油大学计算机与信息技术学院 李勇勇 5 GameLaucher java class GuessGame Player p1 创建 3 个实例变量 player 对象 Player p2 Player p3 public void startGame 创建 startGame 的方法 p1 new Player p2 new Player p3 new Player int guessp1 0 用三个变量声明是否数字被猜中 int guessp2 0 int guessp3 0 boolean p1isright false 声明三个变量来保存猜测的数字 boolean p2isright false boolean p3isright false int targetnumber int Math random 10 System out println I am thinking of a number is while true System out println number to guss is targetnumber p1 guess 调用 player 的 guess 方法 p2 guess p3 guess guessp1 p1 number 取出猜的数字并且罗列出来 System out println player one gussed guessp1 guessp2 p2 number System out println player two gussed guessp2 guessp3 p3 number System out println player three gussed guessp3 if guessp1 targetnumber 判断所猜的数字是否和目标数字相等 p1isright true 东北石油大学计算机与信息技术学院 李勇勇 6 if guessp2 targetnumber p2isright true if guessp3 targetnumber p3isright true if p1isright p2isright p3isright System out println we have a winner System out println player one got it right p1isright System out println player two got it right p2isright System out println player three got it right p3isright System out println Game is over break 游戏结束 终止循环 else System out println the game we will come on class Player int number 0 要被猜的数字 public void guess number int Math random 10 利用随机函数产生随机数 System out println I am guessing the number is number 主方法 public class GameLaucher public static void main String args GuessGame game new GuessGame 创建 GussGame 的对象 game 东北石油大学计算机与信息技术学院 李勇勇 7 game startGame game 对象调用 startGame 的方法 开始执行游戏 HelloWorld java public class HelloWorld public static void main String args System out println HelloWorld Hobbits java public class Hobbits String name public static void main String args Hobbits h new Hobbits 3 int z 1 while z 2 z z 1 h z new Hobbits h z name bilbo if z 1 h z name Mark if z 2 h z name Tina System out println h z name is a good hobbits name IfTest java public class IfTest public static void main String args int x 3 东北石油大学计算机与信息技术学院 李勇勇 8 if x 3 System out println x must be 3 System out println this runs no matter what IfTest2 java public class IfTest2 public static void main String args int x 2 if x 3 System out println x must be 3 else System out println x is not 3 System out println this runs no matter what JieCheng java public class JieCheng public static void main String args long result 0 long f 1 for int i 1 i 10 i f f i result f System out println 1 到 10 的阶乘之和为 result Loopy java public class Loopy public static void main String args int x 1 System out println before the loop while x 4 东北石油大学计算机与信息技术学院 李勇勇 9 System out println in the loop System out println value of x is x x x 1 System out println this is after the loop OddSum java public class OddSum public static void main String args long result 0 for int i 1 i0 if x 2 System out print a x x 1 System out print if x 2 System out print b c x x 1 System out print if x 1 System out print d x x 1 Sum java public class Sum public static void main String args long result 0 for int i 1 i 100 i result i System out println 1 到 99 的和是 result Test java public class Test public static void main String args int num 0 i 1 while i 100 东北石油大学计算机与信息技术学院 李勇勇 11 if i 3 0 System out println i i num if num 5 break i TestArrays java public class TestArrays public static void main String args String islands new String 4 int index new int 4 int y 0 islands 0 Bermuda islands 1 Fiji islands 2 Azores islands 3 Cozumel index 0 1 ind

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论