課題2 課題1は少し置いといて課題2に挑戦
攻撃力 ー 防御力の計算はできたので、
次に、
・モンスタークラス、勇者クラスは生物クラスを継承する
に挑戦します。
ひとまず一旦保存
Main
public class Main {
public static void main(String[] args) {
//勇者を作成
String name;
int hp;
int power;
int damage;
int defence;
Hero h = new Hero();
h.name = "マッサル";
h.hp = 100;
h.power = 20;
System.out.println("勇者" + h.name + "が第一世界に召喚された。HP" + h.hp);
Goblin g1 = new Goblin();
g1.name = "ゴブリン";
g1.hp = 50;
g1.defence = 10;
g1.suffix = 'A';
System.out.println("ゴブリン" + g1.suffix + "が現れた!!");
h.damage = h.power - g1.defence;
h.sleep(10);
h.attack(g1);
SuperHero sh = new SuperHero();
sh.run();
}
public static void main(String[] args) {
//勇者を作成
String name;
int hp;
int power;
int damage;
int defence;
Hero h = new Hero();
h.name = "マッサル";
h.hp = 100;
h.power = 20;
System.out.println("勇者" + h.name + "が第一世界に召喚された。HP" + h.hp);
Goblin g1 = new Goblin();
g1.name = "ゴブリン";
g1.hp = 50;
g1.defence = 10;
g1.suffix = 'A';
System.out.println("ゴブリン" + g1.suffix + "が現れた!!");
h.damage = h.power - g1.defence;
h.sleep(10);
h.attack(g1);
SuperHero sh = new SuperHero();
sh.run();
}
}
Hero
public class Hero {
String name;
int hp;
int power;
int damage;
int defence;
int recover;
void sleep(int hour){
recover = 10 * hour;
this.hp += recover;
System.out.println(this.name + "は、" + hour + "時間眠って" + recover + "回復した。");
System.out.println(this.name + "の現在のHPは" + this.hp + "です。");
}
void attack(Goblin g1){
System.out.println(this.name + "は、" + g1.name + "に" + damage + "ダメージを与えた");
g1.hp -= damage;
System.out.println(g1.name + "の現在のHPは" + g1.hp + "です。");
}
String name;
int hp;
int power;
int damage;
int defence;
int recover;
void sleep(int hour){
recover = 10 * hour;
this.hp += recover;
System.out.println(this.name + "は、" + hour + "時間眠って" + recover + "回復した。");
System.out.println(this.name + "の現在のHPは" + this.hp + "です。");
}
void attack(Goblin g1){
System.out.println(this.name + "は、" + g1.name + "に" + damage + "ダメージを与えた");
g1.hp -= damage;
System.out.println(g1.name + "の現在のHPは" + g1.hp + "です。");
}
}
Goblin
public class Goblin{
char suffix;
void die(){
if(hp<0){
System.out.println("ゴブリンは動かなくなった。");
}
}
char suffix;
void die(){
if(hp<0){
System.out.println("ゴブリンは動かなくなった。");
}
}
}