我正在尝试计算易货系统,所以首先我将项目添加到库存(json文件)另一种方法...然后尝试计算CalCost方法中的最小最大价格数组,并发送到howmuch方法,如下所示
[WebMethod]
public double[] CalInstrumentCost(String instrumentName, double instrumentQuantity)
{
double[] priv = new double[2];
try
{
string jsonString = File.ReadAllText(filePath);
MusicInstrumentsList Mlist = JsonConvert.DeserializeObject<MusicInstrumentsList>(jsonString);
var MInstrument = Mlist.MusicInstruments_List.Find(item => item.InstrumentsName == instrumentName);
if (MInstrument != null)
{
double miprice = MInstrument.MinInstrumentPrice * instrumentQuantity;
double maPrice = MInstrument.MaxInstrumentPrice * instrumentQuantity;
priv[0] = miprice; // min Price
priv[1] = maPrice; // max Price
return priv;
}
else
{
throw new InvalidOperationException("Invalid Operation");
}
}
catch
{
throw new InvalidOperationException("Invalid Operation");
}
}
但priv不存在,所以我尝试全局定义,但计算结果是INF(1/0 = ⇒ nf),我该怎么办?
[WebMethod]
public double HowMuchInstrument(String instrumentName, double totalBudget)
{
double result = 0;
try
{
string jsonString = File.ReadAllText(filePath);
MusicInstrumentsList Mlist = JsonConvert.DeserializeObject<MusicInstrumentsList>(jsonString);
var MInstrument = Mlist.MusicInstruments_List.Find(item => item.InstrumentsName == instrumentName);
if (MInstrument != null)
{
result = Math.Floor(totalBudget / priv[0]);
}
else
{
return result;
}
}
catch
{
return result;
}
return result;
}
1条答案
按热度按时间3ks5zfa01#
您在
CalInstrumentCost()
内部创建priv
,以便在外部不可见。您可以向
HowMuchInstrument()
添加其他参数并在内部调用CalInstrumentCost()
(如果此方法在同一个类中):备注:
instrumentQuantity
的类型是double
-不能是int
?1.在
CalInstrumentCost()
中使用数组来存储最小值和最大值-您只需创建具有2个属性单独类,而无需依赖数组中的位置1.如果需要更高的精度,可以使用
decimal
代替double