ASP.net MVC 5 C# 从 IF 语句中返回双精度值

ASP.net MVC 5 C# returning the double from within IF statement

如何从 IF 语句中访问 double

 [HttpPost]
    public ActionResult Iflexcst([Optional] float Quan, [Optional] float kammid, [Optional] float kammod, [Optional] float SpacerInnerDim, [Optional] float SpacerOuterDim, [Optional] float WashOuterDim, [Optional]float IsoWashInnerDim, [Optional]float SteelWashInnerDim, [Optional] float SteelWashQuan, [Optional] float IsoWashQuan, FormCollection form)
    {
        //other functions removed
        if (WashOuterDim <= 43.6)
        {

            double washerSerrTime = 0.6;
            double SerrWasherRun = (washerSerrTime * IsoWashQuan) * (1 / 60);
            ViewBag.SerrWasherRun = SerrWasherRun;

            double SerrWasherRunPrice = ((SerrateSetup / Quan) + washerSerrTime) * serrateRate;
            ViewBag.SerrWasherRunPrice = SerrWasherRunPrice;

        }

        if (WashOuterDim > 43.6)
        {


            double washerSerrTime = 0.833;
            double SerrWasherRun = (washerSerrTime * IsoWashQuan) * (1 / 60);
            ViewBag.SerrWasherRun = SerrWasherRun;

            double SerrWasherRunPrice = ((SerrateSetup / Quan) + washerSerrTime) * serrateRate;
            ViewBag.SerrWasherRunPrice = SerrWasherRunPrice;


        }


        // Adds together all costs 
        float Price = (float)SerrWasherRunPrice;

它建议为 "SerrWasherRunPrice" 创建一个获取和设置或使其成为一个 public 变量,但这两种解决方案都让我与另一个声明发生冲突。

将变量声明移到 if

之外
[HttpPost]
public ActionResult Iflexcst([Optional] float Quan, [Optional] float kammid, [Optional] float kammod, [Optional] float SpacerInnerDim, [Optional] float SpacerOuterDim, [Optional] float WashOuterDim, [Optional]float IsoWashInnerDim, [Optional]float SteelWashInnerDim, [Optional] float SteelWashQuan, [Optional] float IsoWashQuan, FormCollection form)
{
    //other functions removed
    double SerrWasherPrice = 0;
    if (WashOuterDim <= 43.6)
    {

        double washerSerrTime = 0.6;
        double SerrWasherRun = (washerSerrTime * IsoWashQuan) * (1 / 60);
        ViewBag.SerrWasherRun = SerrWasherRun;

        SerrWasherRunPrice = ((SerrateSetup / Quan) + washerSerrTime) * serrateRate;
        ViewBag.SerrWasherRunPrice = SerrWasherRunPrice;

    }
   else
    {
        double washerSerrTime = 0.833;
        double SerrWasherRun = (washerSerrTime * IsoWashQuan) * (1 / 60);
        ViewBag.SerrWasherRun = SerrWasherRun;

        SerrWasherRunPrice = ((SerrateSetup / Quan) + washerSerrTime) * serrateRate;
        ViewBag.SerrWasherRunPrice = SerrWasherRunPrice;


    }


    // Adds together all costs 
    float Price = (float)SerrWasherRunPrice;

声明后的数字并不重要,因为它将在 if 子句中更改。另外,第二个if应该改为else

因为您一直将它存储在 ViewBag 上,所以您可以从那里取出它,这样您就不必对代码进行太多更改。

// Adds together all costs 
float Price = (float)ViewBag.SerrWasherRunPrice;