简体   繁体   English

在if语句中声明变量,然后在代码中稍后使用它

[英]Declare variable in if statement and then using it later in code

I am programming a console "game" and I need to declare a "hp" of character in IF statements which depends on level of this character. 我正在编程一个控制台“游戏”,我需要在IF语句中声明一个字符的“ hp”,具体取决于该字符的级别。

if ((char_level > 0) && (char_level < 4))
        {
            char_hp = 100;
        }
if ((level > 4) && (level < 6))
        {
            char_hp = 120;
        }
if ((level > 6) && (level < 8))
        {
            char_hp = 150;
        }
if ((level > 8) && (level < 10))
        {
            char_hp = 180;
        }

Then I need to use it later in code in a fight. 然后,我需要稍后在战斗中使用它。 After a successful fight character gets a new level and after that, program will get back to check these IF statements and if level is bigger than 4, character's hp will be increased to 120. But declaration of char_hp in IF statements does not change the value of hp in general and when the next fight comes after reaching level 4, character's hp is still like at the end of previous battle was. 成功的战斗角色获得新的级别之后,程序将返回以检查这些IF语句,如果级别大于4,则角色的hp将增加到120。但是在IF语句中声明char_hp不会更改该值一般而言,当达到等级4后下一场战斗时,角色的hp仍然像上一场战斗结束时一样。 I am new in C# programming and I have tried everything but I can't solve it, if it is possible. 我是C#编程的新手,我已经尝试了所有方法,但如果可能,我无法解决。

The same problem is with the "hp" of enemy that is randomly generated...then I need to use it in that fight 同样的问题是随机产生的敌人的“ hp” ...然后我需要在战斗中使用它

if((level>0) && (level<4))
        {
            random_enemy_hp = RND.Next(89, 111);
            goto enemy;
        }
if((level>4) && (level<6))
        {
            random_enemy_hp = RND.Next(109, 141);
            goto enemy;
        }
if ((level > 6) && (level < 8))
        {
            random_enemy_hp = RND.Next(149, 184);
            goto enemy;
        }
if ((level > 8) && (level < 10))
        {
            random_enemy_hp = RND.Next(189, 221);
            goto enemy;
        }

EDIT: I meant "saving values to variables" in IF statements, so I can use them later in code. 编辑:我的意思是在IF语句中“将值保存到变量”,因此以后可以在代码中使用它们。 This is how my code starts, then there are "Console.WriteLine()"-s, principe of a fight and statements shown above. 这就是我的代码的开始方式,然后有“ Console.WriteLine()”-s,战斗原理和上面显示的语句。

string name;
int char_hp = 100;
int level = 1;
int random_enemy_hp;
Random RND = new Random();

You're completely on the wrong track. 您完全走错了路。 You should be doing something like: 您应该执行以下操作:

int[] charLevelHp = { 100, 100, 100, 100,
                      120, 120, 120,
                      150, 150,
                      180, 180 };

int charLevel = 1;
int charHp = charLevelHp[charLevel];

I can't help but notice that you're comparing with char_level for your first couple if-statement, but you're comparing to level for your subsequent if-statements 我不禁注意到,你与比较char_level你的第一对夫妇的if语句,但你比较level为你以后的if语句

if ((char_level > 0) && (char_level < 4))
    {
        char_hp = 100;
    }
if ((level > 4) && (level < 6))
    {
        char_hp = 120;
    }

I think you might have intended to use char_level for all of the conditions. 我认为您可能打算将char_level用于所有条件。

if ((char_level > 0) && (char_level < 4))
    {
        char_hp = 100;
    }
if ((char_level > 4) && (char_level< 6))
    {
        char_hp = 120;
    }

If that's the issue, it would be consistent with the kinds of errors you're seeing. 如果那是问题,那将与您所看到的错误类型保持一致。

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

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