为什么我的简单if语句不起作用?

时间:2010-09-16 08:34:19

标签: c# if-statement

我设计了一个简单的纸牌游戏,其中显示了两张牌,用户必须下注他们是否会获得显示在两张牌之间的牌。如果用户不想下注,他们只是再次交易。用户从100英镑开始。

游戏在大多数方面都运行良好,但有一个巨大的缺陷。用户可以比他们的余额更多地下注。因此,如果用户有100英镑,他们下注105英镑,他们赢了,他们将有205英镑的余额。这显然是糟糕!如果他们有100英镑,他们下注105英镑他们输了,他们的余额保持不变。这也很糟糕。
所以我认为一个简单的if语句可以解决这个问题:

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
}  
switch (betResult)
{
    case TIE:
        winloseLabel.Text = "Tie. You still lose. HA!";
        myRules.Balance -= wager;
        break;

    case PLAYERWINS:    
        winloseLabel.Text = "You win. Woop-de-do..";
        myRules.Balance += wager;
        break;

    case DEALERWINS:
        winloseLabel.Text = "You lose. Get over it.";
        myRules.Balance -= wager;
        break;
}

为什么这不起作用?我很确定它是如此简单,但我对C#很陌生,所以对我很轻松!

4 个答案:

答案 0 :(得分:13)

你应该有一个else

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
}
else
{  
    switch (betResult)
    {
        //...
    }
}

答案 1 :(得分:3)

你的if语句是正确的,但是,如果它被触发,你就不会结束例程。

您可以通过添加“返回”来执行此操作设置标签后的声明,或者,如果您依赖于您向我们展示的代码,您可以在if语句的“else”部分包含switch语句...

答案 2 :(得分:2)

在你的if语句之后,无论如何都要进入案例陈述,你不应该在案例陈述中有其他的吗?

答案 3 :(得分:2)

我完全不明白,但试试

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
    return;
}  

if (wager <= balance)
{
    switch (betResult)
    {
        case TIE:
            winloseLabel.Text = "Tie. You still lose. HA!";
            myRules.Balance -= wager;
            break;

        case PLAYERWINS:    
            winloseLabel.Text = "You win. Woop-de-do..";
            myRules.Balance += wager;
            break;

        case DEALERWINS:
            winloseLabel.Text = "You lose. Get over it.";
            myRules.Balance -= wager;
            break;
    }
}  
相关问题