简体   繁体   中英

Switch statement in a C# calculator

The calculator can't calculate percentage operations. I will provide the switch statement below. I hope you can help me!

It is only the last case "%": number = (percentage / 100) * totalNumber; that doesn't work.

The debugger says:

Control cannot fall through from one case label to another.

private void button15_Click(object sender, EventArgs e) {
        switch (operationPerformed) {
            case "+":
                textBox_Result.Text=(resultValue + Double.Parse(textBox_Result.Text)).ToString();
                break;
            case "-":
                textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
                break;
            case "*":
                textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
                break;
            case "/":
                textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
                break;
            case "%":
                number = (percentage / 100) * totalNumber;

            default:
                break;
        }
    }

I expect it to calculate the percentage of a certain number, but it doesn't work for some reason.

I think you make a mistake after case "%" :

Actual formula for Calculating Percentage is: (Part / Whole) * 100

you should try something like this:

case "%":
   result = (obtained / total) * 100; 
   break;

check this and please don't add pseudocode

case "%":
    textBox_Result.Text = (Double.Parse(textBox_Result.Text) * resultValue / 100.0).ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM