简体   繁体   English

我的if和else陈述无法正常运作

[英]My if and else statements are not working properly

this is the code: 这是代码:

public class Champion
{
    private String  championName;           // champion name

    private String  championSummonersName;  // Champion summoner's name

    int             maxHitPoints1;          // Champion's max hit points

    int             currentHitPoints1;      // Champion's current hit points

    int             maxHitPoints2;          // summoner's max hit points

    int             currentHitPoints2;      // summoner's current hit points

    double          globalbuff  = 1.1;

    Champion(int MHP1, int CHP1, int MHP2, int CHP2)
    {
        maxHitPoints1 = 100;
        currentHitPoints1 = 75;
        maxHitPoints2 = 125;
        currentHitPoints2 = 110;
    }

    public Champion(String name, String summoners)
    {
        championName = name;
        championSummonersName = summoners;
    }

    public void setchampionName(String name)
    {
        championName = name; // store the champion name
    } // end method setchampionName

    // method to set the summoners name
    public void setchampionSummonersName(String summoners)
    {
        championSummonersName = summoners; // store the summoners name
    }

    // method to retrieve the champion name
    public String getchampionName()
    {
        return championName;
    } // end method getchampionName

    // method to retrieve the summoners name
    public String getchampionSummonersName()
    {
        return championSummonersName;
    }

    public void setmaxHitPoints1(int MHP1)
    {
        if (getMHP1() < 0)
        {
            System.out.println(1);
        }
        else
            maxHitPoints1 = MHP1;

    }

    public int getMHP1()
    {
        return maxHitPoints1;

    }

    public void setmaxHitPoints2(int MHP2)
    {
        if (getMHP2() < 0)
        {
            System.out.println(1);
        }
        else
            maxHitPoints2 = MHP2;

    }

    public int getMHP2()
    {
        return maxHitPoints2;

    }

    public void setcurrentHitPoints1(int CHP1)
    {
        if (CHP1 <= 0)
        {
            currentHitPoints1 = 0;
            System.out.println("Champion is dead");
        }
        else
            currentHitPoints1 = CHP1;

    }

    public int getCHP1()
    {
        return this.currentHitPoints1;

    }

    public void setcurrentHitPoints2(int CHP2)
    {
        if (CHP2 <= 0)
        {
            currentHitPoints2 = 0;
            System.out.println("summoner is dead");
        }
        else
            currentHitPoints2 = CHP2;

    }

    public int getCHP2()
    {
        return currentHitPoints2;
    }

    public void displayMessage()
    {
        // this statement calls getCharacterName to get the
        // name of the course this DnDCharacterSheet represents

        System.out.printf("Champion is %s!\n", getchampionName());
        System.out.printf("Cain's max hit points are: " + getMHP1());
        System.out.printf("\nCain's current hit points are: " + getCHP1());
        /* write code to output the character's player name */
        System.out.printf("\nSummoner is %s!", getchampionSummonersName());
        System.out.printf("\nAble's max hit points are: " + getMHP2());
        System.out.printf("\nAble's current hit points are: " + getCHP2());
        System.out.printf("\n\nGlobal buff by 10 percent to max and current hit points\n");
        System.out.printf("\nCain's buff\nmax hit points: " + globalbuff * getMHP1());
        System.out.printf("\ncurrent hit points: " + globalbuff * getCHP1());
        System.out.printf("\nAble's buff\nmax hit points: " + globalbuff * getMHP2());
        System.out.printf("\ncurrent hit points! " + globalbuff * getCHP2());
        System.out.printf("\n\nRKO OUTTA NOWHERE!!!! CAUSING 500 DAMAGE TO EVERYTHING\n");

        System.out.printf("\nCain after RKO\nmax hit points: " + globalbuff * getMHP1());
        System.out.printf("\n");
        System.out.printf("current hit points: ");
        System.out.println(getCHP1() - 500);
        System.out.printf("\n\n");
        System.out.printf("Able after RKO\nmax hit points: " + globalbuff * getMHP2());
        System.out.printf("\n");
        System.out.printf("current hit points: ");
        System.out.println(getCHP2() - 500);

    }
}

So when the hit points drop below 0 I want it to simply say "0" but instead it outputs a negative number either though I have a my if statements. 因此,当命中点降至0以下时,我希望它只说“ 0”,但是尽管我有一个if语句,但它还是输出一个负数。 It does compile correctly it just does not output correctly and I'm curious as to why? 它可以正确编译,只是不能正确输出,我很好奇为什么?

First you should definitely check out some programming tutorials for object orientated programming. 首先,您绝对应该查看一些面向对象编程的编程教程。 You create a Champion class but never use it. 您创建了冠军类,但从未使用过。 you create a bunch of methods which you never use and most just return a field that can be accessed directly in the class. 您创建了一堆从未使用过的方法,而大多数方法只是返回一个可以直接在类中访问的字段。

Now to your problem: You never use setcurrentHitPointsX(int CHP1) , and thus the hit points are never modified. 现在解决您的问题:您永远不会使用setcurrentHitPointsX(int CHP1) ,因此永远不会修改生命值。

You print a negative value, since you print the current hit points minus 500. 您打印一个负值,因为您打印当前的生命值减去500。

This: 这个:

System.out.println(getCHP1() - 500);

Is technically this: 在技​​术上是这样的:

System.out.println(75 - 500);

Which results in a negative value: -425 得出负值: -425

To solve this you could actually use your method: 为了解决这个问题,您实际上可以使用您的方法:

setcurrentHitPoints1(-500);
System.out.println(getCHP1()); // prints 0

But from your text the champion is actually receiving 500 damage, so you should add the current hit points into the calculation. 但是从您的文字来看,冠军实际上会受到500点伤害,因此您应该在计算中加上当前的生命值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM