范围不适用于开关盒,需要很好的选择

时间:2013-02-14 13:51:36

标签: c# switch-statement range

我正在尝试为我正在制作的基于文本的RPG创建一个调平系统。我遇到的问题是看起来范围不适用于开关案例。这就是我所拥有的:

int exp = 0, level = 1, hp = 10, hpmax = 10;

if (exp >= 262144) exp = 262144;

switch (exp)            
{
case (4 - 15):
    level = 2;
    hp = 12;
    hpmax = 12;
    break;

case (16 - 63):
    level = 3;
    hp = 14;
    hpmax = 14;
    break;

case (64 - 255):
    level = 4;
    hp = 16;
    hpmax = 16;
    break;

case (256 - 1023):
    level = 5;
    hp = 18;
    hpmax = 18;
    break;

case (1024 - 4095):
    level = 6;
    hp = 20;
    hpmax = 20;
    break;

case (4096 - 16383):
    level = 7;
    hp = 20;
    hpmax = 20;
    break;

case (16384 - 65535):
    level = 8;
    hp = 22;
    hpmax = 22;
    break;

case (65536 - 262143):
    level = 9;
    hp = 24;
    hpmax = 24;
    break;

case (262144 - 999999999):
    level = 10;
    hp = 26;
    hpmax = 26;
    break;

是的,我意识到这不符合我的意愿。我对我发现的替代品不太满意。我正在寻找最简单的解决方案。我是编程的新手,所以任何帮助都会受到赞赏。

5 个答案:

答案 0 :(得分:1)

请注意,如果hpmax始终为= hp,您可以在结尾处设置,而不需要在case中添加{。}}。

使用if-else:

if (4 <= exp && exp <= 15)
{
  level = 2;
  hp = 12;
}
else if (16 <= exp && exp <= 63)
{
  level = 3;
  hp = 14;
}
else if ...

hpmax = hp; // set hpmax afterwards

简化上述内容:

if (exp < 4) // 0-3
  ; // do nothing
else if (exp < 16) // 4-15
  ...
else if (exp < 64) // 16-63
  ...

或简化功能:(类似于utnapistim的建议)

bool in(int exp, int start, int end) { return start <= exp && exp <= end; };

if (in(exp, 4, 15)) // 4-15
  ...
else if (in(exp, 16, 63)) // 16-63
  ...

使用log-base-2:

switch((int)(Math.Log(exp)/Math.Log(2)))
{
  case 2: case 3: // 4-15
    level = 2;
    hp = 12;
    break;
  case 4: case 5: // 16-63
    ...
  default: // 262144-999999999
    ...
}
hpmax = hp;

一种数学(简短的)解决方案:(不适用于所有情况,只需从中获取解决方案)

int log = (int)(Math.Log(exp)/Math.Log(2));
level = 1+log/2;
hp = hpmax = 10+level*2;

答案 1 :(得分:0)

从概念上讲,您可以使用函数进行检查。

使用c ++的示例:

inline bool in_range(int min, int max, int value)
{ return min <= value && value <= max; }

// ....

        int exp = 0, level = 1, hp = 10, hpmax = 10;

        if (exp >= 262144) exp = 262144;

        if (in_range(0, 15, exp)) {
            level = 2;
            hp = 12;
            hpmax = 12;
        } else if (in_range(16, 63, exp)) {
            level = 3;
            hp = 14;
            hpmax = 14;
        } else if ...

答案 2 :(得分:0)

你可能正在寻找if-then-else语句:

           int exp = 0, level = 1, hp = 10, hpmax = 10;


            if ((4 >= exp) && (exp <= 15)){
                level = 2;
                hp = 12;
                hpmax = 12;
            } elseif ((16 >= exp) && (exp <= 63)){
                level = 3;
                hp = 14;
                hpmax = 14;
            } elseif ((64 >= exp) && (exp <= 255)){
                level = 4;
                hp = 16;
                hpmax = 16;
            } elseif ((256 >= exp) && (exp <= 1023)){
                level = 5;
                hp = 18;
                hpmax = 18;
            } elseif ((256 >= exp) && (exp <= 1023)){

             .... etc, you know what do paste here, etc ....

            } elseif ((262144 >= exp) && (exp <= 999999999)){
                level = 10;
                hp = 26;
                hpmax = 26;
            } else {
                ... you probably don't need anything here ...
                ... you already set defaults in the init up top...
            }

答案 3 :(得分:0)

我认为你应该使用定义数组和连续if的组合来为角色找到合适的等级,我会给你一个随机(和更少)值的样本......

<?php
// Definition of experience needed per level, and hp given
$levelsData = array( 1 => array( "exp" => 0,
                                 "hp" => 10),
                     2 => array( "exp" => 4,
                                 "hp" => 12),
                     3 => array( "exp" => 15,
                                 "hp" => 16),
                     4 => array( "exp" => 27,
                                 "hp" => 34),
                     5 => array( "exp" => 500,
                                 "hp" => 120));

// Finding user level, giving $exp as current exp
foreach ($levelsData as $level => $data) {
    if ($exp < $data['exp']) {
        break;
    }
}

// Setting data, we already have $level as the actual level+1
$level--;
$data = $levelsData[$level];
$hp = $data['hp'];
$maxHp = $hp;
?>

答案 4 :(得分:0)

这是我最终使用的:

static void LevelSystem()

    {
        if ((4 <= exp) && (exp <= 15))
        {
            level = 2;
            hpmax = 12;
        }

        else if ((16 <= exp) && (exp <= 63))
        {
            level = 3;
            hpmax = 14;
        }

        else if ((64 <= exp) && (exp <= 255))
        {
            level = 4;
            hpmax = 16;
        }

        else if ((256 <= exp) && (exp <= 1023))
        {
            level = 5;
            hpmax = 18;
        }

        else if ((1024 <= exp) && (exp <= 4095))
        {
            level = 6;
            hpmax = 20;
        }

        else if ((4096 <= exp) && (exp <= 16383))
        {
            level = 7;
            hpmax = 20;
        }

        else if ((16384 <= exp) && (exp <= 65535))
        {
            level = 8;
            hpmax = 22;
        }

        else if ((65536 <= exp) && (exp <= 262143))
        {
            level = 9;
            hpmax = 24;
        }

        else if ((262144 <= exp) && (exp <= 999999998))
        {
            level = 10;
            hpmax = 26;
        }
        else if (exp >= 999999999)
        {
            exp = 999999999;
            level = 99;
            hp = 999999999;
            hpmax = 999999999;
        }
    }

我为了以后的测试目的而制作了这个荒谬的数字,但我把它全部放在一个方法中,所以每当我需要检查exp时我都可以调用它。我测试了它,它完全按照我想要的方式工作。

感谢大家的帮助!