我想操縱Java中的對象。爲了更好地解釋,我有一個叫做Creature的超類和兩個叫做Dragon and Warrior的子類。我爲每個龍和戰士創造了兩個對象來互相對抗。我該如何設置一個攻擊方法,該方法對一個對象造成傷害,並使用該數字將其從第二個對象的健康狀況中減去。操作OOP中的Java對象
請注意,戰士和龍都是亞類。
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
這個問題太開放了。請顯示一些代碼,以獲得更快,更有幫助的回覆。 – andrewdleach
目前爲止你有什麼代碼?什麼部分給你帶來困難? – khelwood
嗯......這一切[看起來有點熟悉](http://stackoverflow.com/a/32342794/522444).... –