如何防止if语句在for循环中重复

时间:2016-08-19 05:27:11

标签: c# for-loop

我正在开发一个项目,它在2个for循环中有几个if语句。我想要做的是让每个if语句只在​​执行时执行一次 这是我的代码

 for (int i = 0; i < 4; i++)
        {
            for (int k = 0; k < 5; k++)
            {
                if (PokerCard[k] == two[i])
                {
                    PokerTwo++;
                }
                if (PokerTwo == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == three[i])
                {
                    PokerThree++;
                }
                if (PokerThree == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == four[i])
                {
                    PokerFour++;
                }
                if (PokerFour == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == five[i])
                {
                    PokerFive++;
                }
                if (PokerFive == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == six[i])
                {
                    PokerSix++;
                }
                if (PokerSix == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == seven[i])
                {
                    PokerSeven++;
                }
                if (PokerSeven == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == eight[i])
                {
                    PokerEight++;
                }
                if (PokerEight == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == nine[i])
                {
                    PokerNine++;
                }
                if (PokerNine == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == ten[i])
                {
                    PokerTen++;
                }
                if (PokerTen == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == jack[i])
                {
                    PokerJack++;
                }
                if (PokerJack == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == queen[i])
                {
                    PokerQueen++;
                }
                if (PokerQueen == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == king[i])
                {
                    PokerKing++;
                }
                if (PokerKing == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == ace[i])
                {
                    PokerAce++;
                }
                if (PokerAce == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
            }
        }

正如你可以看到当if语句为true时它将为奖金增加10,但我只想让它做一次,所以当它返回循环时它不会再次运行相同的if语句,但也许另一个如果那个变为真。此外,我尝试在if语句的底部使用return,这在一定程度上有效,但是这只允许执行一个if语句,并且对于我正在处理的项目,我有时需要它来通过两个if声明。删除返回将使它经历更多if语句,但也多次运行相同的一个,这是我不想要的。 任何帮助,将不胜感激。 感谢

5 个答案:

答案 0 :(得分:1)

如果你想每次迭代'i'只执行一次,那么你应该为每个if声明一个计数器;在外部循环语句之后用0初始化all并在每次if语句执行之后递增相应的计数器。 例如:

 for (int i = 0; i < 4; i++)
    {
         c1=0;
         c2=0;
        for (int k = 0; k < 5; k++)
        {
            if (PokerCard[k] == two[i] && c1==0)
            {
                PokerTwo++;
                c1++;
            }
            if (PokerTwo == 2 && c2==0)
            {
                cash = cash + 10;
                winnings = winnings + 10;
                Cash.Text = Convert.ToString(cash);
                Winnings.Text = Convert.ToString(winnings);
                PairWin.BackColor = Color.Red;
                Winner.Visible = true;
                c2++;
             }
           }
          }

并删除return语句。

答案 1 :(得分:1)

您的代码显然需要strategy模式。

由于我不了解您的业务逻辑,我将使用类/接口的通用名称;请根据您的需要调整。 为了简化代码,我将要做的另一个假设是代码中的所有属性都可以从另一个类访问。再次,根据您的代码进行调整。

现在,转到代码。 声明策略的接口:

public interface IStrategy
{
    bool IsApplicable(YourClass instance);

    void Apply(YourClass instance);
}

现在,对于代码中的每个if语句,创建一个实现IStrategy的类:

public class PokerTwoStrategy : IStrategy
{
    public bool IsApplicable(YourClass instance)
    {
        return instance.PokerTwo == 2;
    }

    public void Apply(YourClass instance)
    {
        instance.cash = cash + 10;
        instance.winnings = winnings + 10;
        instance.Cash.Text = Convert.ToString(cash);
        instance.Winnings.Text = Convert.ToString(winnings);
        instance.PairWin.BackColor = Color.Red;
        instance.Winner.Visible = true;
    }
}

创建策略后,在YourClass中创建IStrategy个实例的集合,并在您的方法中迭代该集合并应用可应用的策略:

public class YourClass
{
    private readonly IEnumerable<IStrategy> _strategies;

    // Inject the strategies using DI container or build them manually
    public YourClass(IEnumerable<IStrategy> strategies)
    {
        _strategies = strategies;
    }

    public void YourMethod()
    {
        for(int i = 0; i < 4; i++)
        {
            for(int k = 0; k < 5; k++)
            {
                _strategies.Where(s=>s.IsApplicable(this))
                    .ToList()
                    .ForEach(s => s.Apply(this));
            }
        }
    }
}

现在虽然您已经介绍了更多类型,但您的代码更易于维护且更易于理解。

答案 2 :(得分:0)

希望这会有所帮助,

将一个标志变量设置为默认值,如果在if中检查if是否为0,那么只运行所需的语句,并且在此块内部将标志变量设置为非零值此限制块将被执行如果块被执行,那么在即将到来的迭代中只会执行一次

实施例

Block1_Flag=0
continue_run=0
//Statement_Block1
while(condition){
{
if(Block1_Flag == 0)
{
//so this bolck executed one so need to execute this again hence set Flag to 1 or any other number other then 1
Flag = 1
continue_run = 1
}
if(continue_run == 0){
return 
}
}

将此视为可应用于问题的通用逻辑

答案 3 :(得分:0)

所以我有一些清理代码的建议。

首先,使用数组来计算卡计数,而不是PokerTwoPokerThree等。这样,您就可以用一个循环替换所有这13个if语句,因为看起来每个级别的动作都是一样的。

其次,假设您将卡片存储为int,从0-51开始,您可以使用PokerCard / 4获取诉讼并PokerCard % 13获得排名(假设您排序为首先匹配,然后排名。如果按排名排序然后适合,请使用PokerCard % 4PokerCard / 13)。现在您可以摆脱外部for循环,以及twothree等阵列。

第三,既然您只想运行每个if语句一次,请将它们移到主循环之外,这样您只需在完成计数后检查一次,而不是在每张卡之后检查。

这会将您的代码缩减为类似

var CardCount = new int[13];

for (int k = 0; k < 5; k++)
{
    CardCount[PokerCard[k] % 13]++;
}
for (int c = 0; c < 12; c++)
{
    if (CardCount[c] >= 2)
    {
        //do winning stuff
    }
}

管理起来容易得多。

答案 4 :(得分:0)

嗯,我没有试图找出你想要做什么,我可以回答你的问题,虽然不是“正确”的答案,但这也不是错误的答案。 (我会强烈建议一种不同于你使用的算法/方法 - 重新思考你解决这个问题的方式,它看起来不正确,反复重复的代码,不必要的分支和意志我很难维护等,我离题了..)

按字面意思回答您的问题;

最简单的方法是添加一个检查if if语句是否已被执行。 (我假设你不想在外部for循环中多次触发“if”语句。我也假设你打算使用耦合的if语句(增量)不止一次执行)。

// I'm assuming this code adds winnings for any pairs of cards found?
var PairedWinFlags = new bool[13]; // 13 cards

// Your outer loop
for (int i = 0; i < 4; i++)
{
    for (int k = 0; k < 5; k++)
    {
        ...
        if (PokerCard[k] == two[i])
        {
            PokerTwo++;
        }
        if (PokerTwo == 2 && !PairedWinFlags[1]))
        {
            PairedWinFlags[1] = true; // Set this card flag to true
            cash = cash + 10;
            winnings = winnings + 10;
            Cash.Text = Convert.ToString(cash);
            Winnings.Text = Convert.ToString(winnings);
            PairWin.BackColor = Color.Red;
            Winner.Visible = true;
            return;
        }
        ...

        // ... Do the same for all other card types

        if (PokerAce == 2 && !PairedWinFlags[0]))
        {
            PairedWinFlags[0] = true;
        ...

        if (PokerQueen == 2 && !PairedWinFlags[11]))
        {
            PairedWinFlags[11] = true;
        ...
    }
}

老实说,如果您想保留此代码并可能维护/理解它,我会建议不要使用此解决方案。如果是这样,你应该开始有意义地评论你的代码 - 并且可能会问一个问题,比如“如何计算数组中的整数对?

祝你好运:)