游戏暴击(随机数和if判断)

编程入门 行业动态 更新时间:2024-09-26 00:35:07

游戏暴击(<a href=https://www.elefans.com/category/jswz/34/1766016.html style=随机数和if判断)"/>

游戏暴击(随机数和if判断)

Player
package co.hp.wanjialei;import java.util.Random;/*
* 玩家类
* 属性:名字,类型,生命值,防御力,攻击力
* 方法:自我介绍,pk
* */
public class Player {//封装;把属性设为private,提供公共的get和set方法间接访问,提高安全性private String name;private String type;private int hp;private int fy;private int gjl;/** 描述自己的信息* */public void say(){System.out.println("我是"+name+"一个"+type+"生命值"+hp+"防御力"+fy+"攻击力"+gjl);}/** pk* 我方开启的战斗* @param P 敌对玩家* */public void pk(Player p){//定义一个标记,0我方攻击1敌方攻击int flag = 0;//回合制,直到一方死while (true){//显示当前战斗人员信息this.say();p.say();Random a = new Random();int bj = a.nextInt(100);if(flag==0){if(bj<=50){int harm = this.gjl*2-p.fy;//伤害值System.out.println("我方回合,暴击了"+this.gjl*2);System.out.println(p.name+"掉血"+harm);p.hp-=harm;//生命值-伤害值}else{int harm = this.gjl-p.fy;//伤害值System.out.println(p.name+"掉血"+harm);p.hp-=harm;//生命值-伤害值}flag =1;}else {if (bj<=50){int harm = p.gjl*2-this.fy;//伤害值,掉血量System.out.println("敌方回合,暴击了"+this.gjl*2);System.out.println(this.name+"掉血"+harm);this.hp-=harm;}else{int harm = p.gjl-this.fy;//伤害值,掉血量System.out.println(this.name+"掉血"+harm);this.hp-=hp;}flag = 0;}Random b = new Random();int dl = b.nextInt(10);//有血量<=0,战斗结束if(this.hp<=0){System.out.println(p.name+"战胜"+this.name);if (dl == 0){System.out.println("无掉落");}if (dl == 1){System.out.println("皮肤碎片*2");}if (dl == 2){System.out.println("英雄碎片*2");}if (dl == 3){System.out.println("钻石*300");}if (dl == 4){System.out.println("铭文碎片*200");}if (dl == 5){System.out.println("铭文碎片*400");}if (dl == 6){System.out.println("铭文碎片*250");}if (dl == 7){System.out.println("铭文碎片*300");}if (dl == 8){System.out.println("铭文碎片*350");}if (dl == 9){System.out.println("荣耀积分*60");}if (dl == 10){System.out.println("荣耀积分*40");}break;//结束}if(p.hp<=0) {System.out.println(this.name+"战胜"+p.name);if (dl == 0){System.out.println("无掉落");}if (dl == 1){System.out.println("皮肤碎片*2");}if (dl == 2){System.out.println("英雄碎片*2");}if (dl == 3){System.out.println("钻石*300");}if (dl == 4){System.out.println("铭文碎片*200");}if (dl == 5){System.out.println("铭文碎片*400");}if (dl == 6){System.out.println("铭文碎片*250");}if (dl == 7){System.out.println("铭文碎片*300");}if (dl == 8){System.out.println("铭文碎片*350");}if (dl == 9){System.out.println("荣耀积分*60");}if (dl == 10){System.out.println("荣耀积分*40");}break;//结束}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}//构造器(建议如果定义了有参的构造器,一定定义一个无参构造器)public Player(String name, String type, int hp, int fy, int gjl) {this.name = name;this.type = type;this.hp = hp;this.fy = fy;this.gjl = gjl;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public int getFy() {return fy;}public void setFy(int fy) {this.fy = fy;}public int getGjl() {return gjl;}public void setGjl(int gjl) {this.gjl = gjl;}
}
PlayerTest
package co.hp.wanjialei;/*** 测试玩家类* 船舰玩家对象PK*/
public class PlayerTest {public static void main(String[] args) {Player p1= new Player("1号", "ll", 60000, 1600, 2200);Player p2= new Player("2号", "hh", 60000, 1460, 2500);p1.pk(p2);}
}

更多推荐

游戏暴击(随机数和if判断)

本文发布于:2023-06-23 13:32:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/849917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!