import java.util.Scanner;

public class Assignment5 {
    public static void main(String[] arg){
     Fighter myFighter, enemyFighter;
     Scanner console = new Scanner(System.in);
     int num1, num2, num3;
     String str, another;
     System.out.println ("***  Fighter Game ***");

     do {
             System.out.println("Create your fighter (Type three integers + name):     ");
             num1 = console.nextInt();   num2 = console.nextInt();   num3     = console.nextInt();
             str = console.next();
             if (num1 + num2 + num3 == 10) {
                    myFighter = new Fighter (num1, num2, num3, str);
                   enemyFighter = new Fighter( );
                   enemyFighter.setName ("Enemy");
                   System.out.print( myFighter.getName()+"    ["+myFighter.getPower()+","+myFighter.getSpeed()+","+myFighter.getHeal()+"] ");
                   System.out.print( enemyFighter.getName()+"    ["+enemyFighter.getPower()+","+enemyFighter.getSpeed()+","+enemyFighter.getHeal()+"] ");
                   System.out.println();

                 int fights = 0;
                 boolean gameOver= false;
                 while (fights < 10  &&  !gameOver){
                     System.out.print("Fight[" + fights + "]: ");
                     myFighter.attack (enemyFighter);
                     myFighter.heal ();
                     enemyFighter.attack(myFighter);
                     enemyFighter.heal();
                     myFighter.printInfo();
                     enemyFighter.printInfo();
                     if (enemyFighter.isDead() ||     myFighter.isDead()) gameOver = true;
                     fights ++;
                     System.out.println();
                 }
                 if(myFighter.getHealth() > enemyFighter.getHealth())     System.out.println(" You Win");
                 else System.out.println("You Lost");
                System.out.println();
                System.out.print("Play another fight (y/n)? ");
                another = console.next();
            }
            else {
                System.out.println("Invalid Inputs. The total of three     numbers should be 10.");
                another = console.next();
            }
        } while (another.equalsIgnoreCase("y"));
}
}







public class Fighter
{
public Fighter()
{

}


private int power = (int)(1 + Math.random()*5);
private int speed = (int)(1 + Math.random()*5);
private int heal = 10 - (power + speed);
private int health = 50;
private String name;

public Fighter (int num1, int num2, int num3, String str)
{
this.power = num1;
this.speed = num2;
this.heal = num3;
this.name = str;
}

public String getName()
{
    return name;
}

public int getPower()
{
    return power;
}

public int getSpeed()
{
    return speed;
}

public int getHeal()
{
    return heal;
}

public int getHealth()
{
    return health;
}

public void setName(String newName)
{
    name = newName;
}

public void setPower(int newPower)
{
    power = newPower;
}

public void setSpeed(int newSpeed)
{
    speed = newSpeed;
}
public void setHeal(int newHeal)
{
    heal = newHeal;
}

public void setHealth(int newHealth)
{
    health = newHealth;
}

public boolean isDead()
{
    if(health <= 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

public void heal()
{
    int random =(int)(1+ Math.random()*10);

    if (random <= 3 && health <= 49)
    {
        health++;
    }
    else
    {

    }
}

public void printInfo()
{
    System.out.print(name + "[" + health + "]       ");
}

public void attack(Fighter f)
{
    this.health = this.health - power;
    health = health - this.power;
}
}


第一组代码是我分配的程序,第二组代码是战斗机类,是我正在从事的工作。攻击方法应该从另一架战斗机的生命值中减去一个战斗机的力量,但我能使他们做的就是从其自身的战斗力中减去其自身力量的两倍。我如何设置它以使一名战斗机的力量从另一架战斗机的生命值中减去,为什么过载的方法会使我所做的一切加倍?

我是否不完全了解重载方法的工作原理?他们会将所有内容加倍吗?还是我写错了什么?我是否正确地使用它来尝试获得新的健康价值?

最佳答案

您的问题看起来像在这里:

public void attack(Fighter f)
{
  this.health = this.health - power;
  health = health - this.power;
}


这应该是

public void attack(Fighter f)
{
  this.health = this.health - f.power;
  f.health = f.health - this.power;
}

10-05 18:59