Files
question-java/Question13-Extends/src/main/java/Character.java
2026-10-30 17:49:49 +09:00

54 lines
1.3 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

public class Character {
private String name;
private int hp;
private int power;
private int defense;
public Character(int hp, int power, int defense) {
this.hp = hp;
this.power = power;
this.defense = defense;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int attack() {
return this.power;
}
public boolean damage(int enemyAttack) {
int damage = enemyAttack - this.defense; //ダメージ量の計算
if(damage <= 0) {
damage = 1; //計算結果が0以下ならダメージ量1にする
}
this.hp -= damage; //hpからダメージ量引いて残りをhpにする
boolean dead= false; //戦闘不能かどうかを真偽地を使って判断
if(this.hp <= 0) { //hpが0以下なら
this.hp = 0; //hpを0に補正
dead = true;
}
System.out.println(this.name + "は" + damage + "のダメージを受けた");
System.out.println(this.name + "の残りの体力は" + this.hp + "。");
if(dead) { //戦闘不能の時
System.out.println(this.name + "はちからつきた。");
}
return dead;
}
public String Parameters() { //パラメーターを文字列で返す
return "名前:" + this.name + "\n" + "体力:" + this.hp + "\n" +
"力" + this.power + "\n" + "耐久" + this.defense;
}
}