java练习代码Word文档格式.docx
《java练习代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《java练习代码Word文档格式.docx(21页珍藏版)》请在冰点文库上搜索。
System.out.println("
);
x=x+1;
}
}
Dog.java
publicclassDog
Stringname;
//变量有两种:
基本数据类型和引用数据类型;
变量声明时必须要有类型和名称
//创建dog对象
Dogdog1=newDog();
dog1.bark();
dog1.name="
mark"
//创建dog数组
Dog[]mydogs=newDog[3];
//数组也属于对象,数组的引用既是对象的引用
mydogs[0]=newDog();
mydogs[1]=newDog();
mydogs[2]=dog1;
//通过数组引用存取dog
mydogs[0].name="
tina"
mydogs[1].name="
peter"
//dog2的名字?
System.out.println("
whatisthedog2'
sname?
System.out.println(mydogs[2].name);
//对dog逐个执行bark()方法
mydogs.length)
mydogs[x].bark();
publicvoidbark(){
System.out.println(name+"
wangwang!
!
DogTestDrive.java
publicclassDogTestDrive
publicstaticvoidmain(String[]args)
Dogd=newDog();
//建立一个dog对象
d.size=40;
d.bark();
classDog
intsize;
Stringbreed;
voidbark(){
youareafooldog!
DooBee.java
publicclassDooBee
intx=1;
3){
System.out.print("
Doo"
Bee"
x++;
if(x==3){
Do"
DrumKitTestDrive.java
classDrumKit
booleantopHat=true;
booleansnare=true;
voidplayTopHat()
dingdingda-ding"
voidplaySnare()
bangbangba-bang"
classDrumKitTestDrive
DrumKitd=newDrumKit();
d.snare=false;
d.playSnare();
d.playTopHat();
if(d.snare==true)
d.playSnare();
EchoTestDrive.java
publicclassEchoTestDrive
Echoe1=newEcho();
Echoe2=newEcho();
4)
e1.hello();
e1.count=e1.count+1;
if(x==3)
{
e2.count=e2.count+1;
}
if(x>
0)
e2.count=e2.count+e1.count;
System.out.println(e2.count);
classEcho
intcount=0;
voidhello()
heloooo...."
GameLaucher.java
classGuessGame
Playerp1;
//创建3个实例变量player对象
Playerp2;
Playerp3;
publicvoidstartGame()//创建startGame的方法
p1=newPlayer();
p2=newPlayer();
p3=newPlayer();
intguessp1=0;
//用三个变量声明是否数字被猜中
intguessp2=0;
intguessp3=0;
booleanp1isright=false;
//声明三个变量来保存猜测的数字
booleanp2isright=false;
booleanp3isright=false;
inttargetnumber=(int)(Math.random()*10);
Iamthinkingofanumberis:
...."
while(true)
numbertogussis"
+targetnumber);
p1.guess();
//调用player的guess方法
p2.guess();
p3.guess();
guessp1=p1.number;
//取出猜的数字并且罗列出来
playeronegussed"
+guessp1);
guessp2=p2.number;
playertwogussed"
+guessp2);
guessp3=p3.number;
playerthreegussed"
+guessp3);
if(guessp1==targetnumber)//判断所猜的数字是否和目标数字相等
p1isright=true;
if(guessp2==targetnumber)
p2isright=true;
if(guessp3==targetnumber)
p3isright=true;
if(p1isright||p2isright||p3isright)
System.out.println("
wehaveawinner!
playeronegotitright?
+p1isright);
playertwogotitright?
+p2isright);
playerthreegotitright?
+p3isright);
Gameisover!
break;
//游戏结束终止循环
}else{
thegamewewillcomeon!
classPlayer
intnumber=0;
//要被猜的数字
publicvoidguess()
number=(int)(Math.random()*10);
//利用随机函数产生随机数
Iamguessingthenumberis"
+number);
//主方法
publicclassGameLaucher
GuessGamegame=newGuessGame();
//创建GussGame的对象game
game.startGame();
//game对象调用startGame的方法,开始执行游戏
HelloWorld.java
publicclassHelloWorld
publicstaticvoidmain(String[]args){
HelloWorld"
Hobbits.java
publicclassHobbits
Hobbits[]h=newHobbits[3];
intz=-1;
while(z<
2)
z=z+1;
h[z]=newHobbits();
h[z].name="
bilbo"
if(z==1)
h[z].name="
if(z==2)
System.out.println(h[z].name+"
isagoodhobbitsname!
IfTest.java
publicclassIfTest
intx=3;
xmustbe3"
thisrunsnomatterwhat!
IfTest2.java
publicclassIfTest2
intx=2;
}else{
xisnot3"
JieCheng.java
publicclassJieCheng
longresult=0;
longf=1;
for(inti=1;
i<
10;
i++)
f=f*i;
result+=f;
1到10的阶乘之和为:
+result);
Loopy.java
publicclassLoopy
beforetheloop"
intheloop"
valueofxis"
+x);
x=x+1;
thisisaftertheloop"
OddSum.java
publicclassOddSum
100;
i+=2)
result+=i;
1~100之内的奇数和为:
PhraseOMatic.java
publicclassPhraseOMatic
String[]first={"
I"
"
love"
you"
};
String[]second={"
China"
String[]third={"
English"
//计算每组有多少个名词术语
intoneLength=first.length;
inttwoLength=second.length;
intthreeLength=third.length;
//产生随机数字
intrand1=(int)(Math.random()*oneLength);
intrand2=(int)(Math.random()*twoLength);
intrand3=(int)(Math.random()*threeLength);
//组合出专家术语
Stringphrase=first[rand1]+"
+second[rand2]+"
+third[rand3];
//输出
whatweneedisa"
+phrase);
Shufflel.java
classShufflel
intx=3;
while(x>
2)System.out.print("
a"
x=x-1;
-"
if(x==2)System.out.print("
bc"
if(x==1)System.out.print("
d"
Sum.java
publicclassSum
1到99的和是:
Test.java
publicclassTest
intnum=0,i=1;
while(i<
100){
if(i%3==0){
i="
+i);
num++;
if(num==5){
i++;
TestArrays.java
publicclassTestArrays
String[]islands=newString[4];
int[]index=newint[4];
inty=0;
islands[0]="
Bermuda"
islands[1]="
Fiji"
islands[2]="
Azores"
islands[3]="
Cozumel"
index[0]=1;
index[1]=3;
index[2]=0;
index[3]=2;
intref;
while(y<
ref=index[y];
islands="
System.out.println(islands[ref]);
y=y+1;
TestBreak.java
publicclassTestBreak
intstop=4;
if(i==stop)break;
System.out.println(i);
TestConvert.java
publicclassTestConvert
byteb1=69;
byteb2=87;
byteb3=(byte)(b1+b2);
System.out.println(b3);
TestParameter.java
publicclassTestParameter
Dogmydog=newDog();
mydog.bark(3);
private:
intnumsofbark;
Dog()
numsoftbark=0;
Dog(inta)
numsofbark=a;
publicvoidbark(inta)
numsofbark=a;
while(numsofbark>
wangwang!
numsofbark--;
~Dog()
numsofbark=0;
TestNum.java
publicclassTestNum
for(inti=101;
200;
booleanflag=true;
for(intj=2;
j<
i;
j++)
if(i%j==0)
{
flag=false;
}
if(!
flag){continue;
TestVar.java
publicclassTestVar
intj=9;
inti=0;
System.out.println(i);
//System.out.println(j);
//成员变量和局部变量