Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my simple if statement work?

I've designed a simple card game where two cards are displayed and the user has to bet on whether they will get a card that is in between the two cards displayed. If the user doesn't want to bet, they just deal again. The user begins with £100.

The game works fine in most aspects, but has a huge flaw. The user can bet more than they have in their balance. So, if the user has £100, they bet £105, and they win, they will have £205 in their balance. This is clearly bad! And if they have £100, they bet £105 and they lose, their balance stays the same. This is also pretty bad.
So I thought a simple if-statement would sort this out:

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;
}

Why doesn't this work? I'm pretty sure it's something so simple, but I'm pretty new to C#, so go easy on me!

like image 931
New Start Avatar asked Sep 16 '10 08:09

New Start


People also ask

Why is my IF ELSE statement not working?

If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear". length < 3); if ("Jon".

Why is Python ignoring my IF statement?

Python is not ignoring anything. It is simply doing what you are telling it! You are using the equality comparison operator, == , where you should be using = to assign a value to a variable. Otherwise the code is fine, despite being incredibly awkward in its construction.

What is the problem with IF statement?

1. What is the problem with IF statement? Explanation: The IF statement has a priority according to which conditions are being tested. Whenever it is found to be true, all the following ELSIF statements are skipped and the END IF is executed.

How do you write a simple if statement?

Working of 'simple if' statement The statement inside the if block are executed only when condition is true, otherwise not. If we want to execute only one statement when condition is true, then braces ({}) can be removed. In general, we should not omit the braces even if, there is a single statement to execute.


4 Answers

You should have an else there:

if (wager > balance)
{
    winLoseLabel.Text = "You can't bet more than you have!";
}
else
{  
    switch (betResult)
    {
        //...
    }
}
like image 139
Dan Dumitru Avatar answered Oct 26 '22 23:10

Dan Dumitru


Your if-statement is correct, however, you do not end the routine if it's triggered.

You can do this by adding a "return;" statement after setting the label, or, if you are depending on code underneath what you are showing us, you can include the switch-statement in the "else" part of the if-statement...

like image 33
Christian Wattengård Avatar answered Oct 26 '22 23:10

Christian Wattengård


After your if statement you go into the case statement anyway, shouldn't you have an else around the case statement/?

like image 42
Iain Ward Avatar answered Oct 27 '22 00:10

Iain Ward


I dont understand exactly, but try

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

or

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;
    }
}  
like image 32
Serkan Hekimoglu Avatar answered Oct 27 '22 00:10

Serkan Hekimoglu