java中編程實(shí)現(xiàn)如下的骰子游戲:丟下兩個骰子,若分值的總值為7點(diǎn),則“贏”;否則“輸”。
public class Test {
public static void main(String[] args){
DieGame dieGame = new DieGame();
if (dieGame.play()) {
System.out.println(你贏了!);
} else {
System.out.println(你輸了!);
}
}
}
class Die {
private int faceValue;
public int getFaceValue() {
return faceValue;
}
public void setFaceValue(int faceValue) {
this.faceValue = faceValue;
}
public void roll() {
this.faceValue = (int) (Math.random() * 6 + 1);
}
}
class DieGame {
private Die die1 = new Die();
private Die die2 = new Die();
public boolean play() {
die1.roll();
System.out.println(第一次點(diǎn)數(shù): + die1.getFaceValue());
die2.roll();
System.out.println(第二次點(diǎn)數(shù): + die2.getFaceValue());
if (die1.getFaceValue() + die2.getFaceValue() == 7) {
return true;
} else {
return false;
}
}
}
java編寫兩玩家對打程序,需要生命值,攻擊和防御,生命為0則死亡。
public?class?Player?{
????????//?模擬交戰(zhàn)
public?static?void?Engage(){
System.out.println(戰(zhàn)斗開始!);
new?Player(悟空,200,50,10).Attack(new?Player(八戒,300,40,6));
System.out.println(戰(zhàn)斗結(jié)束!);
}
//?名字
private?String?name;
//?生命值
private?int?hp;
//?傷害
private?int?damage;
//?護(hù)甲
private?int?armor;
public?Player(String?name,int?hp,int?damage,int?armor){
this.name?=?name;
this.hp?=?hp;
this.damage?=?damage;
this.armor?=?armor;
}
public?String?GetName(){
return?this.name;
}
//?進(jìn)攻
public?void?Attack(Player?target){
System.out.println(String.format(%1$s正在攻擊%2$s!,this.GetName(),target.GetName()));
target.HurtFrom(this,?this.damage);
}
//?反擊
public?void?CounterAttack(Player?enemy){
System.out.println(String.format(%1$s正在反擊%2$s!,this.GetName(),enemy.GetName()));
this.Attack(enemy);
}
//?計(jì)算傷害
public?void?HurtFrom(Player?enemy,int?damage){
int?realDamage?=?damage?-?this.armor;
this.hp?=?this.hp?-?realDamage;
System.out.println(String.format(%1$s受到%2$s的攻擊,傷害%3$s,真實(shí)傷害%4$s,當(dāng)前血量%5$s!,this.GetName(),enemy.GetName(),damage,realDamage,this.hp));
if(this.hp<50)
System.out.println(String.format(哈哈,%1$s快死了!,?this.GetName()));
if(this.hp?<?0){
?System.out.println(String.format(%1$s已死亡!,?this.GetName()));
?return;
}
this.CounterAttack(enemy);
}
}
public?class?Application?{
public?static?void?main(String[]?args)?{
//?模擬交戰(zhàn)
Player.Engage();
}
}
//?戰(zhàn)斗記錄
/*
戰(zhàn)斗開始!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量256!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量170!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量212!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量140!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量168!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量110!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量124!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量80!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量80!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量50!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量36!
哈哈,八戒快死了!
八戒正在反擊悟空!
八戒正在攻擊悟空!
悟空受到八戒的攻擊,傷害40,真實(shí)傷害30,當(dāng)前血量20!
哈哈,悟空快死了!
悟空正在反擊八戒!
悟空正在攻擊八戒!
八戒受到悟空的攻擊,傷害50,真實(shí)傷害44,當(dāng)前血量-8!
哈哈,八戒快死了!
八戒已死亡!
戰(zhàn)斗結(jié)束!
*/
