继承、setter 和 getter(游戏 Class 输出)
Inheritance, setters and getters (Game Class Output)
我应该如何先降低它的护甲然后再降低生命值?我不想打印护甲的负数,我想打印如果护甲为0,健康应该是下一个减少的。
为了测试中的实例Class,我使用巫师并向骑士施放法术3次火球。因为骑士应该先减护甲,护甲到0后生命值才减。
输出:
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now 6 armor left, and 80 health left.
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 80 health left.
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 56 health left.
正如您在第二个打印中看到的那样,盔甲得到 -18,我希望打印 0 和 62 剩余生命值。因为法师造成 24 点伤害
enter image description here
这是Parent class:
public class Human {
String name;
int strength;
int stealth;
int intelligence;
int health;
public Human() {
name = "Human";
strength=3;
stealth=3;
intelligence=3;
health=100;
}
public int getStrength()
{
return strength;
}
public int getStealth()
{
return stealth;
}
public int getIntelligence()
{
return intelligence;
}
public int getHealth()
{
return health;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void setStrength(int strength)
{
this.strength = strength;
}
public void setStealth(int stealth)
{
this.stealth = stealth;
}
public void setIntelligence(int intelligence)
{
this.intelligence = intelligence;
}
public void setHealth(int health)
{
this.health = health;
}
public void attack(Human human)
{
int damagetaken = this.getStrength();
human.health = human.health - this.getStrength();
System.out.println(this.getName()+ " attack " + human.getName() + " and will deal " +damagetaken +" damage.");
System.out.println(human.getName()+ " has now "+human.getHealth()+" health left");
System.out.println();
}
}
Child Class 骑士:
public class Knight extends Human
{
int armor=30;
public Knight() {
this.name="Knight";
this.health=80;
}
public void Reinforce(){
this.armor=this.armor+5;
System.out.println(this.getName()+ " use a 'Reinforce' and will increase its armor to 5 ");
System.out.println(this.getName()+ " has now "+ this.armor +" armor left.");
System.out.println();
}
public void HolyStrike(Human human)
{
int damagetaken = (int)(human.health-10-(.20*this.armor));
human.health= human.getHealth() - damagetaken;
System.out.println(this.getName()+ " use a 'Holy Strike' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
System.out.println();
}
}
Child Class 向导:
public class Wizard extends Human{
public Wizard()
{
this.name="Wizard";
this.health=50;
this.intelligence=8;
}
public void heal(Human human)
{
int healtake = this.getIntelligence();
human.health=human.health+this.intelligence;
System.out.println(this.getName()+" use a 'Heal' to " + human.getName() + " he/she will receive " + healtake + " additional health to his/her HP");
System.out.println(human.getName()+" has now "+ human.getHealth()+" health left.");
System.out.println();
}
public void fireball(Human human)
{
int damagetaken = this.intelligence*3;
human.health= human.health-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
System.out.println();
}
public void fireball(Knight human)
{
if (human.armor > 1)
{
int damagetaken = this.intelligence*3;
human.armor= human.armor-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
System.out.println();
}
else
{
int damagetaken = this.intelligence*3;
human.health= human.health-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
System.out.println();
}
}
}
测试Class:
public class HumanTest {
public static void main(String Args[]){
Human h1 = new Human();
Wizard w1= new Wizard();
Ninja n1 = new Ninja();
Samurai s1 = new Samurai();
Knight k1 = new Knight();
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
}
}
你能帮我解决这个问题吗?
源码可以问我
您正在检查 armor
是否大于 1 (human.armor > 1
)。您必须尽可能减少护甲,然后 health
.
public void fireball(Knight knight) { //I've renamed the variable as knight
int damageToTake = this.intelligence * 3;
if (knight.armor >= damageToTake) {
knight.armor -= damageToTake;
} else {
int remaining = damageToTake - knight.armor;
knight.armor = 0;
// reduce remaining from health
knight.health -= remaining;
}
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ knight.getName());
System.out.println(knight.getName()+" has now " +knight.armor+" armor left, and "+knight.getHealth()+" health left.");
System.out.println();
}
我想当 health
不够时,您也必须处理。我会把它留给你。
您应该添加额外的检查:
- 如果护甲小于0
- 然后生命+=护甲
- 然后将护甲设置为0。
示例护甲 = -10,健康 = 30,然后健康 = 20,护甲 = 0。其他算法也是可能的。
作为附加说明,具有对骑士造成伤害的方法和对人类造成伤害的方法是糟糕的设计。如果你有更多 class 个带盔甲的,你会为每个人制作额外的方法吗?
例如,最好让所有人都有盔甲,但默认值为 0,并将正确的盔甲分配给扩展 classes。
或者更好的是有一个处理护甲和健康的接口,你的 classes 应该实现它,并且伤害处理方法应该接受它。
或者最好有一个方法来接受每个 class 造成的伤害。像这样 class 本身将根据不同的修饰符处理剩余的生命值,你的情况下是盔甲,但你可以添加更多。你真的不应该在向导中处理骑士的盔甲和生命值 class。
我应该如何先降低它的护甲然后再降低生命值?我不想打印护甲的负数,我想打印如果护甲为0,健康应该是下一个减少的。
为了测试中的实例Class,我使用巫师并向骑士施放法术3次火球。因为骑士应该先减护甲,护甲到0后生命值才减。
输出:
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now 6 armor left, and 80 health left.
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 80 health left.
Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 56 health left.
正如您在第二个打印中看到的那样,盔甲得到 -18,我希望打印 0 和 62 剩余生命值。因为法师造成 24 点伤害
enter image description here
这是Parent class:
public class Human {
String name;
int strength;
int stealth;
int intelligence;
int health;
public Human() {
name = "Human";
strength=3;
stealth=3;
intelligence=3;
health=100;
}
public int getStrength()
{
return strength;
}
public int getStealth()
{
return stealth;
}
public int getIntelligence()
{
return intelligence;
}
public int getHealth()
{
return health;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void setStrength(int strength)
{
this.strength = strength;
}
public void setStealth(int stealth)
{
this.stealth = stealth;
}
public void setIntelligence(int intelligence)
{
this.intelligence = intelligence;
}
public void setHealth(int health)
{
this.health = health;
}
public void attack(Human human)
{
int damagetaken = this.getStrength();
human.health = human.health - this.getStrength();
System.out.println(this.getName()+ " attack " + human.getName() + " and will deal " +damagetaken +" damage.");
System.out.println(human.getName()+ " has now "+human.getHealth()+" health left");
System.out.println();
}
}
Child Class 骑士:
public class Knight extends Human
{
int armor=30;
public Knight() {
this.name="Knight";
this.health=80;
}
public void Reinforce(){
this.armor=this.armor+5;
System.out.println(this.getName()+ " use a 'Reinforce' and will increase its armor to 5 ");
System.out.println(this.getName()+ " has now "+ this.armor +" armor left.");
System.out.println();
}
public void HolyStrike(Human human)
{
int damagetaken = (int)(human.health-10-(.20*this.armor));
human.health= human.getHealth() - damagetaken;
System.out.println(this.getName()+ " use a 'Holy Strike' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
System.out.println();
}
}
Child Class 向导:
public class Wizard extends Human{
public Wizard()
{
this.name="Wizard";
this.health=50;
this.intelligence=8;
}
public void heal(Human human)
{
int healtake = this.getIntelligence();
human.health=human.health+this.intelligence;
System.out.println(this.getName()+" use a 'Heal' to " + human.getName() + " he/she will receive " + healtake + " additional health to his/her HP");
System.out.println(human.getName()+" has now "+ human.getHealth()+" health left.");
System.out.println();
}
public void fireball(Human human)
{
int damagetaken = this.intelligence*3;
human.health= human.health-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
System.out.println();
}
public void fireball(Knight human)
{
if (human.armor > 1)
{
int damagetaken = this.intelligence*3;
human.armor= human.armor-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
System.out.println();
}
else
{
int damagetaken = this.intelligence*3;
human.health= human.health-(this.intelligence*3);
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
System.out.println();
}
}
}
测试Class:
public class HumanTest {
public static void main(String Args[]){
Human h1 = new Human();
Wizard w1= new Wizard();
Ninja n1 = new Ninja();
Samurai s1 = new Samurai();
Knight k1 = new Knight();
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
w1.fireball(k1);
}
}
你能帮我解决这个问题吗?
源码可以问我
您正在检查 armor
是否大于 1 (human.armor > 1
)。您必须尽可能减少护甲,然后 health
.
public void fireball(Knight knight) { //I've renamed the variable as knight
int damageToTake = this.intelligence * 3;
if (knight.armor >= damageToTake) {
knight.armor -= damageToTake;
} else {
int remaining = damageToTake - knight.armor;
knight.armor = 0;
// reduce remaining from health
knight.health -= remaining;
}
System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ knight.getName());
System.out.println(knight.getName()+" has now " +knight.armor+" armor left, and "+knight.getHealth()+" health left.");
System.out.println();
}
我想当 health
不够时,您也必须处理。我会把它留给你。
您应该添加额外的检查:
- 如果护甲小于0
- 然后生命+=护甲
- 然后将护甲设置为0。
示例护甲 = -10,健康 = 30,然后健康 = 20,护甲 = 0。其他算法也是可能的。
作为附加说明,具有对骑士造成伤害的方法和对人类造成伤害的方法是糟糕的设计。如果你有更多 class 个带盔甲的,你会为每个人制作额外的方法吗?
例如,最好让所有人都有盔甲,但默认值为 0,并将正确的盔甲分配给扩展 classes。
或者更好的是有一个处理护甲和健康的接口,你的 classes 应该实现它,并且伤害处理方法应该接受它。
或者最好有一个方法来接受每个 class 造成的伤害。像这样 class 本身将根据不同的修饰符处理剩余的生命值,你的情况下是盔甲,但你可以添加更多。你真的不应该在向导中处理骑士的盔甲和生命值 class。