第三次Java出题
王者峡谷之,放开那个貂蝉,让我来!
吕布和赵云同时喜欢上了貂蝉,他们为了争取追求貂蝉的机会,召集了各自的小伙伴,决定在王者峡谷进行比拼。
一.定义王者荣耀人物类Character:
1.私有属性:名字name、初始血量full、当前血量blood、防御值defend、武器名字armsname、攻击值fire
2.构造方法、set、get方法【请自动生成,且构造函数含有全部参数】
3.重写equals方法,名字相同即为相同
二.定义战队类Team
1.私有属性:名字name、List<Character>类型Lcs来存储队员【指向ArrayList】
方法:
- )构造方法以及name的set、get方法【请自动生成,且构造函数只含有name参数】
- )提前准备的参战人员adds()并输出“name正在准备”,并且调用add();
以及中途加入的参战人员addCharacter(),并输出“name中途加入战斗”,并且调用add();
3. )百里玄策来寻找哥哥,判断队伍中参战人员是否有百里守约contains(),若百里守约参战并未死亡,输出“name正在参战,他是参战人员”;若百里守约参战且死亡,输出“name参战过,已死亡”;若未参战,输出“name未参战”
4. )剩余人数int getSize()调用size();
5. )攻击void whoWin(Team t1,Team t2) 【两组每次从开头各派一个队员,没死亡继续下一局】 【后者生命减少的值为前者攻击力,前者生命减少的值为后者攻击力与自身防御之差】,并输出“a.name攻击b.name且a.name受到b.name的反击”,并判断二者是否死亡,若死亡输出“name Game Over!”,并调用remove()删除该成员
6. )(类的方法)void whoWin(Team t1,Team t2)用剩余人数判断哪个队伍获胜并输出“name赢得貂蝉”
吕布之地表最强战队:
百里守约:full和blood:500, defend 200,武器:绝影神枪,fire:700;
吕布:full和blood:600, defend 100,武器:方天画戟, fire:650;
中途参战:李白:full和blood:400, defend 300,武器:大河之剑, fire:800;
赵云之潇洒战队:
鲁班:full和blood:400, defend 100,武器:机关双炮, fire:700,;
赵云:full和blood:700, defend 200,武器:龙胆枪,fire:700;
中途参战:花木兰:full和blood:500, defend 22,武器:传说之刃
, fire:800;
主方法已经给出:
public static void main(String[] args) {
Team t1=new Team("吕布之地表最强战队");
System.out.println(t1.getName());
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character baili=new Character("百里守约",500,500,200,"绝影神枪",700);
t1.contains(baili);
t1.adds(baili);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character lvbu=new Character("吕布",600,600,100,"方天画戟",650);
t1.adds(lvbu);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
t1.contains(baili);
System.out.println();
Team t2=new Team("赵云之潇洒战队");
System.out.println(t2.getName());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Character luban=new Character("鲁班",400,400,100,"机关双炮",700);
t2.adds(luban);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Character zhaoyun=new Character("赵云",700,700,200,"龙胆枪",700);
t2.adds(zhaoyun);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
System.out.println("\n开始战斗:\n");
t1.teamFight(t2);
System.out.println("打完一波之后");
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
t1.contains(baili);
Character libai=new Character("李白",400,400,300,"大河之剑",800);
t1.addCharacter(libai);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character huamulan=new Character("花木兰",500,500,22,"传说之刃",800);
t2.addCharacter(huamulan);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
System.out.println("新的战斗又开始了");
t1.teamFight(t2);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Team.whoWin(t1, t2);
}
正确输出:
吕布之地表最强战队
吕布之地表最强战队现有人数:0
百里守约未参战
百里守约正在准备
吕布之地表最强战队现有人数:1
吕布正在准备
吕布之地表最强战队现有人数:2
百里守约正在参战,他是参战人员
赵云之潇洒战队
赵云之潇洒战队现有人数:0
鲁班正在准备
赵云之潇洒战队现有人数:1
赵云正在准备
赵云之潇洒战队现有人数:2
开始战斗:
百里守约攻击鲁班且百里守约受到鲁班的反击
鲁班 Game Over!
百里守约 Game Over!
吕布攻击赵云且吕布受到赵云的反击
吕布 Game Over!
打完一波之后
吕布之地表最强战队现有人数:0
赵云之潇洒战队现有人数:1
百里守约参战过,已死亡
李白中途加入战斗
吕布之地表最强战队现有人数:1
花木兰中途加入战斗
赵云之潇洒战队现有人数:2
新的战斗又开始了
李白攻击赵云且李白受到赵云的反击
赵云 Game Over!
李白 Game Over!
吕布之地表最强战队现有人数:0
赵云之潇洒战队现有人数:1
赵云之潇洒战队赢得貂蝉
全部代码:
package DiaoChan;
public class Character {
private String name;
private int full;
private int blood;
private int defend;
private String armsname;
private int fire;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFull() {
return full;
}
public void setFull(int full) {
this.full = full;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getDefend() {
return defend;
}
public void setDefend(int defend) {
this.defend = defend;
}
public String getArmsname() {
return armsname;
}
public void setArmsname(String armsname) {
this.armsname = armsname;
}
public int getFire() {
return fire;
}
public void setFire(int fire) {
this.fire = fire;
}
public Character(String name, int full, int blood, int defend, String armsname, int fire) {
super();
this.name = name;
this.full = full;
this.blood = blood;
this.defend = defend;
this.armsname = armsname;
this.fire = fire;
}
//***********************
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Character other = (Character) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package DiaoChan;
import java.util.ArrayList;
import java.util.List;
public class Team {
private String name;
private List<Character> lcs=new ArrayList<Character>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Team(String name) {
super();
this.name = name;
}
public void addCharacter(Character a) {
lcs.add(a);
System.out.println(a.getName()+"中途加入战斗");
}
public void adds(Character a) {
lcs.add(a);
System.out.println(a.getName()+"正在准备");
}
public void contains(Character a) {
if(!lcs.contains(a)&&a.getBlood()==a.getFull()) {//战斗后就不满血了
System.out.println(a.getName()+"未参战");
return;
}
//否则
if(a.getBlood()>0) {
System.out.println(a.getName()+"正在参战,他是参战人员");
}
else if(a.getBlood()==0) {
System.out.println(a.getName()+"参战过,已死亡");
}
}
public int getSize() {
return lcs.size();
}
public void teamFight(Team t) {
for(int i=0,j=0;i<t.lcs.size()&&j<lcs.size();) {
t.lcs.get(i).setBlood(t.lcs.get(i).getBlood()-(lcs.get(j).getFire()));//-t.lcs.get(0).getDefend()
System.out.print(lcs.get(j).getName()+"攻击"+t.lcs.get(i).getName());
lcs.get(j).setBlood(lcs.get(j).getBlood()-(t.lcs.get(i).getFire()-lcs.get(0).getDefend()));//
System.out.println("且"+lcs.get(j).getName()+"受到"+t.lcs.get(i).getName()+"的反击");
if(t.lcs.get(i).getBlood()<=0) {
System.out.println(t.lcs.get(i).getName()+" Game Over!");
t.lcs.remove(t.lcs.get(i));
}
if(lcs.get(0).getBlood()<=0) {
System.out.println(lcs.get(j).getName()+" Game Over!");
lcs.remove(lcs.get(j));
}
}
}
public static void whoWin(Team t1,Team t2) {
if(t1.getSize()>t2.getSize()) {
System.out.print(t1.getName()+"赢得貂蝉");
}
else if(t1.getSize()<t2.getSize()){
System.out.print(t2.getName()+"赢得貂蝉");
}
else {
System.out.println("平局");
}
}
}
package DiaoChan;
public class Test {
public static void main(String[] args) {
Team t1=new Team("吕布之地表最强战队");
System.out.println(t1.getName());
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character baili=new Character("百里守约",500,500,200,"绝影神枪",700);
t1.contains(baili);
t1.adds(baili);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character lvbu=new Character("吕布",600,600,100,"方天画戟",650);
t1.adds(lvbu);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
t1.contains(baili);
System.out.println();
Team t2=new Team("赵云之潇洒战队");
System.out.println(t2.getName());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Character luban=new Character("鲁班",400,400,100,"机关双炮",700);
t2.adds(luban);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Character zhaoyun=new Character("赵云",700,700,200,"龙胆枪",700);
t2.adds(zhaoyun);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
System.out.println("\n开始战斗:\n");
t1.teamFight(t2);
System.out.println("打完一波之后");
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
t1.contains(baili);
Character libai=new Character("李白",400,400,300,"大河之剑",800);
t1.addCharacter(libai);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
Character huamulan=new Character("花木兰",500,500,22,"传说之刃",800);
t2.addCharacter(huamulan);
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
System.out.println("新的战斗又开始了");
t1.teamFight(t2);
System.out.println(t1.getName()+"现有人数:"+t1.getSize());
System.out.println(t2.getName()+"现有人数:"+t2.getSize());
Team.whoWin(t1, t2);
}
}