简体   繁体   English

C# 计算器中的 Switch 语句

[英]Switch statement in a C# calculator

The calculator can't calculate percentage operations.计算器无法计算百分比运算。 I will provide the switch statement below.我将在下面提供 switch 语句。 I hope you can help me!我希望你可以帮助我!

It is only the last case "%": number = (percentage / 100) * totalNumber;只有最后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 "%" :我认为你在 case "%" 之后犯了一个错误:

Actual formula for Calculating Percentage is: (Part / Whole) * 100计算百分比的实际公式是:(部分/整体)* 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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