如何使一个“如果(变量= 100 ||变量= 200)”更好?

时间:2019-04-07 18:27:56

标签: c#

我在Microsoft Visual Studio 2017中使用Windows Forms App, 我有一个显示Messagebox.Show()并更新级别变量的函数。代码非常难看。

我该怎么做,它是无限的,而不是那么丑陋?

    void Level() //Levels.
    {
        if (count == 100 || count == 200 || count == 300 || count == 400 || count == 500 || count == 600 || count == 700 || count == 800 || count == 900 || count == 1000 ||
            count == 1100 || count == 1200 || count == 1300 || count == 1400 || count == 1500 || count == 1600 || count == 1700 || count == 1800 || count == 1900 || count == 2000 ||
                count == 2100 || count == 2200 || count == 2300 || count == 2400 || count == 2500 || count == 2600 || count == 2700 || count == 2800 || count == 2900 || count == 3000 ||
            count == 3100 || count == 3200 || count == 3300 || count == 3400 || count == 3500 || count == 3600 || count == 3700 || count == 3800 || count == 3900 || count == 4000)
        {
            level = level + 1;
            MessageBox.Show("Congratulations, you ranked up to level " + level.ToString(), "Cookie Clicker 2.0");
            label2.Text = "Level: " + level.ToString();

        }
        else if (count >= 4000)
        {
                    MessageBox.Show("Congratulations! We have not fixed any more in this game.",  "Cookie Clicker 2.0", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                       MessageBoxDefaultButton.Button1);
        }
    }

2 个答案:

答案 0 :(得分:5)

您可以按以下方式使用C#Modulo operator

void Level()
{
    if (count % 100 == 0 && count < 4000)
    {
        level = level + 1;
        MessageBox.Show("Congratulations, you ranked up to level " + level.ToString(), "Cookie Clicker 2.0");
        label2.Text = "Level: " + level.ToString();
    }
    else if (count >= 4000)
    {
        MessageBox.Show("Congratulations! We have not fixed any more in this game.",  "Cookie Clicker 2.0", MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
    }
}

编辑:根据评论,计数似乎可以达到96,99,102 ..因此,这是另一种解决方案(将下一级保留为单独的变量):

private int _nextLevel = 100;
void Level()
{
    if (count > _nextLevel && count < 4000)
    {
        _nextLevel += 100;
        level = level + 1;
        MessageBox.Show("Congratulations, you ranked up to level " + level.ToString(), "Cookie Clicker 2.0");
        label2.Text = "Level: " + level.ToString();
    }
    else if (count >= 4000)
    {
        MessageBox.Show("Congratulations! We have not fixed any more in this game.", "Cookie Clicker 2.0", MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
    }
}

答案 1 :(得分:0)

添加

using System.Collections.Generic;

我看不到您在哪里获得“计数”值,但是您可以按照以下步骤操作,将其保存为List

List<int> acceptedValues = new List<int>{ 100, 200, 300}; // add all the values you need

if (acceptedValues.Contains(count))
{
    level = level + 1;
    MessageBox.Show("Congratulations, you ranked up to level " + level.ToString(), "Cookie Clicker 2.0");
    label2.Text = "Level: " + level.ToString();

}
else if (count >= 4000)
{
     MessageBox.Show("Congratulations! We have not fixed any more in this game.",  "Cookie Clicker 2.0", MessageBoxButtons.OK,
     MessageBoxIcon.Exclamation,
     MessageBoxDefaultButton.Button1);
}

编辑:如果所有可能的值都可以被100整除,尽管我先检查count >= 4000然后检查count % 100 == 0,但Risto的回答方法更好