我试图在Java中操作对象。为了更好地说明,我有一个称为Creature的超类和两个名为(Dragon和Warrior)的子类。我为龙和战士彼此战斗创建了两个对象。如何设置对一个对象造成伤害的攻击方法,并使用该数字将其从第二个对象的生命值中减去。
请注意,战士和龙都是子类。
public class Creature {
private int health = 50;
private int attack;
private int level = 1;
private String name;
private Creature random;
private Creature random2;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
if(health >= 3000)
{
health = 3000;
}
if(health <= 0)
{
health = 0;
}
return health;
}
public void setHealth(int health) {
this.health = health < 0 ? 0 : health;
}
public int getAttack() {
Random generator = new Random();
attack = generator.nextInt(10) * level + 1;
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getLevel() {
if(level >= 60)
{
level = 60;
}
return level;
}
public void setLevel(int level){
this.level = level;
}
public boolean isAlive() {
if(getHealth() <= 0)
{
return false;
}
return true;
}
public String getWelcome()
{
String welcome = "Hello and welcome to Dragonslayer!";
return welcome;
}
public String getStab()
{
String stab = "You choose to stab the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
return stab;
}
public String getSlash()
{
String slash = "You choose to slash the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
return slash;
}
public String getCut()
{
String cut = "You choose to cut the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
return cut;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("You come across the dragon and you have two options. Do you run or fight? ");
Creature kirsta = new Dragon();
kirsta.setHealth(50);
Creature brenton = new Warrior();
brenton.setHealth(50);
while(in.hasNextLine())
{
switch(in.nextLine())
{
case "run":
System.out.println("I am so sorry, you could not outrun the dragon, you have been killed!");
break;
case "fight":
while(kirsta.isAlive() && brenton.isAlive())
{
System.out.println("Do you want to stab, slash, or cut the dragon? ");
switch(in.nextLine())
{
case "stab":
System.out.println("You choose to stab the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
break;
case "slash":
System.out.println("You choose to slash the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
break;
case "cut":
System.out.println("You choose to cut the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
break;
default:
System.out.println("I am sorry that is not valid, try again. ");
}
}
break;
default:
System.out.println("I am sorry that is not a valid choice. ");
break;
}//end of switch
if(brenton.isAlive() == false && kirsta.isAlive() == false)
{
System.out.println("It is a horrid day today, as both you and the dragon have fallen.");
}
else if(brenton.isAlive() == true && kirsta.isAlive() == false)
{
System.out.println("Congratulations you have killed the dragon, and survived!");
}
else if(brenton.isAlive() == false && kirsta.isAlive() == true)
{
System.out.println("You have sadly fallen to the dragon, better luck next time.");
}
else
{
System.out.println("SOMETHING WENT WRONG!!!");
}
break;
}//end of while loop in.hasNextLine().
}//end of main
}//end of class
假设您的attack
方法具有与此类似的形式
public void attack(Creature otherCreature){
otherCreature.decreaseHitPointsBy(this.attackValue);
}
用法看起来像
warrior.attack(dragon);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句