public class Demo03Class {
private String name;
private String sex;
private String type;
private int life;
private int defense;
private int attack;
private double baoji;
public double getBaoji() {
return baoji;
}
public void setBaoji(double baoji) {
this.baoji = baoji;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public void say(){
System.out.println("我叫"+name+"是一个"+type+"生命值高达"+life+"防御"+defense+"攻击力"+attack+"性别为"+sex);
}
public void pk(Demo03Class p){
int flag = 1;//默认我方先进攻
//回合制pk,直到一方死亡
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//每次都显示剩余的生命值
this.say();
p.say();
//定义一个标记,0表示我方进攻,1表示敌方进攻
//创造一个随机数
Random rd = new Random();
//赋值给变量a 并且设置随机数为0-100
int a = rd.nextInt(100);
if(flag==0){
//获取暴击率
double b = this.getBaoji();
//我方进攻: (我方攻击力-敌方防御力)
int harm = this.attack-p.defense;//得到伤害
//如果随机的数小于获取的暴击率那么暴击
if (a <= b*100){
double y = harm*1.5;
p.setLife(p.life-(int) y);//敌人掉血
System.out.println(p.name+"掉血" + y+"暴击!!!!");
}else {
p.setLife(p.life-harm);//敌人掉血
System.out.println(p.name+"掉血" + harm);
}
flag = 1 ; //改变进攻方
}else{
//获取敌人的暴击率
double b = p.getBaoji();
//敌方进攻: (敌方攻击力-我方防御力)
int harm = p.attack-this.defense;//得到伤害
if (a <= b*100){
double y = harm*1.5;
this.setLife(this.life-(int) y);//主方掉血
System.out.println(this.name+"掉血" + y+"暴击!!!!");
}else {
this.setLife(this.life-harm);//敌人掉血
System.out.println(this.name+"掉血" + harm);
}
flag = 0 ; //改变进攻方
}
//判别血量
if(this.life<=0){
System.out.println(this.name+"被KO了");
//如果随机数大于等于50那么获得装备
if (a>=50){
System.out.println("恭喜获得|倚天屠龙剑|!!");
}else {
System.out.println("没有掉落装备");
}
break;//有人倒下,停止战斗
}
if(p.life<=0){
System.out.println(p.name+"被KO了");
if (a>=50){
System.out.println("恭喜获得|倚天屠龙剑|!!");
}else {
System.out.println("没有掉落装备");
}
break;//有人倒下,停止战斗
}
}
}
设置对战玩家参数并运行
public static void main(String[] args) {
Demo03Class play = new Demo03Class();
play.setName("自己");
play.setSex("男");
play.setLife(800);
play.setType("战士");
play.setDefense(60);
play.setAttack(70);
play.setBaoji(0.2);
Demo03Class play2 = new Demo03Class();
play2.setName("玩家");
play2.setSex("女");
play2.setLife(523);
play2.setType("刺客");
play2.setDefense(34);
play2.setAttack(120);
play.setBaoji(0.5);
play.pk(play2);
}
&spm=1001.2101.3001.5002&articleId=125682715&d=1&t=3&u=afd0b06695ca413396c83e4a756976d0)
2922

被折叠的 条评论
为什么被折叠?



