asp.net mvc -为c#中的每个循环调用带有新参数值的方法
本文关键字:新参数 方法 参数 调用 循环 mvc net asp | 更新日期: 2023-09-27 17:51:21
[HttpPost]
public ActionResult ShowDetailRate(FormCollection form)
{
List<Calculation> finalList = new List<Calculation>();
Calculation calc3 = new Calculation();
double InterestRate = 0;
double guess = 0.01;
double guess2 = 0.03;
for (int i = 0; i < 50; i++)
{
InterestRate = secantInterestRate(guess, guess2, form);
if (radio == "inArrears")
{
radioCalc = 0;
}
else
{
radioCalc = InvoiceAmount;
}
calc3.PeriodStartDate = PeriodStartDate;
calc3.PeriodEndDate = PeriodEndDate;
if (DTC == "365/365")
{
calc3.NumberOfDays = Convert.ToInt32((calc3.PeriodEndDate - calc3.PeriodStartDate).Days) + 1;
}
else if (DTC == "360/360")
{
calc3.NumberOfDays = 30;
}
calc3.InvoiceAmount = InvoiceAmount;
calc3.InterestRate = InterestRate;
calc3.InterestAmount = (PV - radioCalc) * InterestRate / DTCyear * calc3.NumberOfDays;
calc3.Amortization = (calc3.InvoiceAmount - calc3.InterestAmount);
calc3.PresentValue = PV - calc3.Amortization;
calc3.StartValue = PV;
finalList.Add(calc3);
var count = finalList.Count();
if (finalList[count].PresentValue != FV)
{
guess = guess2;
guess2 = calc3.InterestRate;
continue;
}
else
break;
}
return PartialView("ShowDetail", finalList);
}
在上面的方法中,我使用变量InterestRate
来调用一个名为secantInterestRate
的方法,该方法具有3个参数(double
, double
, FormCollection
)。在循环的第一轮中,我希望将前两个参数设置为(0.01,0.03),但在第二个循环中,我希望设置为guess = guess 2
和guess2 = calc3.InterestRate
。并且仍然在循环开始时调用方法secantInterestRate
,但是使用新值。最后我尝试了一个小if:
var count = finalList.Count() - 3;
if (finalList[count].PresentValue != FV)
{
guess = guess2;
guess2 = calc3.InterestRate;
continue;
}
else
break;
但是这不起作用,因为当循环开始时,guess
将是0.01,guess2
将再次是0.03,而不是我想要的。
是否有可能使guess = guess2
和guess2 = calc3.InterestRate
为每一轮新的循环?
像这样?
if (i==0) {
guess = guess2;
guess2 = calc3.InterestRate;
} else {
// you don't want to change the values after the first time through; do nothing
}
PS:我建议您从表单处理代码中提取出计算代码,或者以其他方式使该方法更具可读性。目前很难看到逻辑,因为有这么多行代码,这可能是很难解决像这样的小问题的主要原因。
按以下方式修改代码:
....
for (int i = 0; i < 50; i++)
{
if (i > 0)
{
guess = guess2;
guess2 = calc3.InterestRate;
}
InterestRate = secantInterestRate(guess, guess2, form);
...