【问题标题】:Can someone help me with the attack method in my fighter class有人可以帮我在我的战斗机课上使用攻击方法吗
【发布时间】:2014-03-21 23:26:25
【问题描述】:
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) 中使用参数 f 所以你的 Fighter 会和自己打架

标签: java class methods instance overloading


【解决方案1】:

您的问题似乎在这里:

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;
}

【讨论】:

  • 所以你的意思是重载的方法基本上是调用另一个战斗机f而不是这个?
  • @user3363245 您的代码中没有发生重载。重载一个方法意味着有多个方法具有相同的名称,但形式参数列表不同。这里不相关。我的意思是,在您的原始版本中,所有访问的字段都属于this 实例。如果您没有限定字段或方法,则暗示 this。我通过指定应该使用其他战斗机实例的字段来修复它。
  • 好吧,有道理,你知道为什么攻击方法是减去两倍的力量而不是正常的力量值。即使我只是输入 System.out.print("hello") 它也会打印出 hello hello
  • 在每一轮中,双方战斗机都会攻击。这是你看到的吗?
  • 是的,两名战士都在进攻,但是战士的生命值不会被对方战士的力量减少,而是战士的生命值会减少两倍的对方战士的力量,我应该只除以 2 还是存在“正确”这样做的方法
猜你喜欢
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多