package Subject.test5;
import java.util.Random;
public class Role {
private String name;
private int blood;
public Role() {
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public void attack(Role role)
{
Random r=new Random();
int hurt=r.nextInt(20)+1;
int remindBlood=role.getBlood()-hurt;
remindBlood=remindBlood>0? remindBlood:0;
role.setBlood(remindBlood);
System.out.println(this.getName()+"举起拳头,打了"+role.getName()+"一下,"+"造成了"+hurt+"点伤害,"+role.getName()+"还剩下了"+role.getBlood()+"点血");
}
}
package Subject.test5;
public class RoleTest {
public static void main(String[] args) {
Role role1=new Role("乔峰",100);
Role role2=new Role("鸠摩智",100);
while(true)
{
role1.attack(role2);
if(role2.getBlood()==0)
{ System.out.println(role2.getName()+"已经被打死");
break;
}
}
while(true)
{
role2.attack(role1);
if(role1.getBlood()==0) {
System.out.println(role1.getName() + "已经被打死");
break;
}
}
}
}