简单的口袋妖怪战斗

时间:2015-11-21 09:35:07

标签: java

美好的一天。我在我的代码上遇到了敌人HP的问题,因为它没有减少到0,而是只是继续添加我所选择的每次攻击所处理的伤害并显示这样的事情(皮卡丘使用迅雷!敌人生活: - 120)。我是Java新手,我无法识别问题。如果有人可以帮助我,请谢谢!

import java.io.*;

public class Pokemon extends Formulas
{
public static void main (String [] args) throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    Formulas f = new Formulas();

    String opt, ans;
    int EnemyHP = 100;

    do
    {
        System.out.println("What will Pikachu do? \nA) Thundershock \nB) Quick Attack \nC) Thundershock \nD) Thunder");
        opt = br.readLine();

            switch(opt)
                {
                case "a": case "A":
                    f.setThundershock();
                    System.out.println("Pikachu uses Thundershock! Enemy Life:"+f.getThundershock());
                    break;
                case "b": case "B":
                    f.setQuickAttack();
                    System.out.println("Pikachu uses Quick Attack! Enemy Life:"+f.getQuickAttack());
                    break;
                case "c": case "C":
                    f.setThunderbolt();
                    System.out.println("Pikachu uses Thunderbolt! Enemy Life:"+f.getThunderbolt());
                    break;
                case "d": case "D":
                    f.setThunder();
                    System.out.println("Pikachu uses Thunder! Enemy Life:"+f.getThunder());
                    break;
                default:
                    System.out.println("Invalid input.");
                }
    }
    while (EnemyHP != 0);
}   
}

这是Formulas类:

class Formulas extends SkillValues{

private int Thundershock, QuickAttack, Thunderbolt, Thunder, Damage, EnemyHP;

SkillValues s = new SkillValues();

public void setThundershock()
{
    this.Damage = s.valueThundershock();
    EnemyHP -= Damage;
}
public void setQuickAttack()
{
    this.Damage = s.valueQuickAttack();
    EnemyHP -= Damage;
}
public void setThunderbolt()
{
    this.Damage = s.valueThunderbolt();
    EnemyHP -= Damage;
}
public void setThunder()
{
    this.Damage = s.valueThunder();
    EnemyHP -= Damage;  
}


public int getThundershock()
{
    return EnemyHP;
}
public int getQuickAttack()
{
    return EnemyHP;
}
public int getThunderbolt()
{
    return EnemyHP;
}
public int getThunder()
{
    return EnemyHP;
}
}

最后是SkillsValue:

public class SkillValues 
{

public int valueThundershock()
{
    return 15;
}
public int valueQuickAttack()
{
    return 10;
}
public int valueThunderbolt()
{
    return 15;
}
public int valueThunder()
{
    return 40;
}
}

1 个答案:

答案 0 :(得分:1)

您有两个EnemyHP个变量。一个是main方法的本地方法,您永远不会更新,因此您的循环永远不会结束,而Formulas类中的另一个方法会更新。

你的循环应该从f得到当前值。

更改

while (EnemyHP != 0)

while (f.getThunder () >= 0) // you have multiple methods that return the EnemyHP member
                             // but it would make more sense to have a getEnemyHP method

您还应初始化EnemyHP课程的Formulas成员。否则默认为0。

您可以将其传递给构造函数:

Formulas f = new Formulas(100);

...

class Formulas extends SkillValues{
    ...
    public Formulas (int EnemyHP) {
        this.EnemyHP = EnemyHP;
    }

    ...
}