Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i get this equation to work within this nested if/else statement for c#?

After i get pass 20 on the user end my if/else doesn’t pick up the next variation of the equatuion. I know its because of setting the total = (numberofchecks * twentychecks) + (fee); but i can’t figure out what to use to get it to move to the next variation of the equation. The negative, over 1000, and format exceptions work.

total = (numberofchecks * twentychecks) + (fee);

            TotalLabel.Text = total.ToString("C");

            // if else (5 or 6)

            if ( numberofchecks < 20 && numberofchecks == 0)
            {
                total = (numberofchecks * twentychecks) + (fee);
               
            }
            else if ( numberofchecks < -1)
            {
                MessageBox.Show("Number of checks must be between 0 and 1000");
            }
            else if (numberofchecks == 20 && numberofchecks <= 39)
            {
                total = (numberofchecks * thirtyninechecks) + (fee);
                
            }
            
            else if (numberofchecks == 40 && numberofchecks <= 59)
            {
                total = (numberofchecks * fiftyninechecks) + (fee);
                
            }
            else if (numberofchecks == 60 && numberofchecks <=1000)
            {
                total = (numberofchecks * thousandchecks) + (fee);
                
            }
            else if (numberofchecks > 1000)
            {
               MessageBox.Show("Number of checks must be between 0 and 1000");
            }
            
        }
        catch(FormatException )
        {
            MessageBox.Show(" Please enter a number");
        }

this is a screenshot of what happens when i test numbersof above twenty

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looking at the code, seems the conditions are not set up correctly.

A better conditional set up is to always check for inappropriate cases first.

So the following checks scope can be reduced to valid ones.

// Check inappropriate cases first to reduce the scope of other checks
if (numberofchecks < 0 || numberofchecks > 1000) {
    MessageBox.Show("Number of checks must be between 0 and 1000");
} else {
    // Guess you don't want 20 to satisfy the twentychecks as well? 
    if (numberofchecks < 20) {
        // cases: 0 to 19
        total = (numberofchecks * twentychecks) + (fee);
    } else if (numberofchecks < 40) {
        // cases: 20 to 39
        total = (numberofchecks * thirtyninechecks) + (fee);
    } else if (numberofchecks < 60) {
        // cases: 40 to 59
        total = (numberofchecks * fiftyninechecks) + (fee);
    } else {
        // cases: 60 to 999
        total = (numberofchecks * thousandchecks) + (fee);
    }
    TotalLabel.Text = total.ToString("C");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading