C# 如何将for循环从一个业务类连接到另一个类?
所以我被要求,写一个应用程序,让一个销售商根据他对一个商品的加价比例,看看他能期望获得多少收入。允许用户输入批发商品价格。以表格形式显示该商品的零售价格,标记为5%、6%、7%、8%、9%和10%。我在windows窗体应用程序中编写了此程序,但在将for循环从一个类连接到窗体上另一个类的输出时遇到了问题。这是我的密码: 这是我的商务舱:C# 如何将for循环从一个业务类连接到另一个类?,c#,winforms,C#,Winforms,所以我被要求,写一个应用程序,让一个销售商根据他对一个商品的加价比例,看看他能期望获得多少收入。允许用户输入批发商品价格。以表格形式显示该商品的零售价格,标记为5%、6%、7%、8%、9%和10%。我在windows窗体应用程序中编写了此程序,但在将for循环从一个类连接到窗体上另一个类的输出时遇到了问题。这是我的密码: 这是我的商务舱: namespace RetailPrice { // Business Class class Wholesale { decimal whole
namespace RetailPrice
{
// Business Class
class Wholesale
{
decimal wholesale;
//Constructor
public Wholesale(decimal sale)
{
this.wholesale = sale;
}
public string GetWholesalePrice(string priceIn)
{
decimal price = decimal.Parse(priceIn);
string priceOut = string.Empty;
//for loop for the class wholesale to set
for (int i = 5; i < 11; i++)
{
decimal percent = i / 100;
decimal wsale = price * (decimal.Parse(i.ToString()) / decimal.Parse("100"));
priceOut += string.Format(" Markup of {0} percent is {1:c}\n", i, (decimal.Parse(i.ToString()) / decimal.Parse("100")));
}
return priceOut;
}
}
}
以下是我的表单的外观:
任何帮助都将不胜感激。我也不是最擅长在这个网站上发布问题的人,我也读过如何写一篇好文章,但是任何关于如何改进我的文章的建议都是非常受欢迎的。
我需要表单将计算中的标记发布到for循环中。我认为这是需要做的地方
// Set the intial value for Whole sale price
Decimal wholesalePrice = 0;
wholesalePrice = Decimal.Parse(TxtWholeSale.Text);
//Create an instance of Wholesale that takes wholesaleprice
Wholesale wsale = new Wholesale(wholesalePrice);
以下是输出结果:
所以我的计算不起作用,因为我需要他从中赚的钱,就像他以5%的价格每卖出100美元你就能赚5美元一样。你的代码有几个问题
public string GetWholesalePrice(string priceIn)
{
decimal price = decimal.Parse(priceIn);
string priceOut = string.Empty;
//for loop for the class wholesale to set
for (int i = 5; i < 11; i++)
{
decimal percent = i / 100m;
decimal wsale = price * percent;
priceOut += string.Format(" Markup of {0} percent is {1:c}\n", i, wsale);
}
return priceOut;
}
public string获取批发商价格(string priceIn)
{
十进制价格=decimal.Parse(priceIn);
string priceOut=string.Empty;
//用于设置类批发的for循环
对于(int i=5;i<11;i++)
{
小数百分比=i/100m;
十进制wsale=价格*百分比;
priceOut+=string.Format(“百分之{0}的标记为{1:c}\n”,i,wsale);
}
退货价格;
}
5/100=0
。我将其替换为100m(m表示十进制类型),以便正确计算那么你的问题是什么?我不明白你在问什么。看起来你正是这么做的,所以你的问题不清楚。那它怎么办?它不能正确地进行计算我的问题是如何让计算结果出现在我的表格上。它的输出是这样的:当我输入500时,它表示加价是0.05,而正确时,它需要显示他从销售中获得的金额。你的老师是错的。非常感谢。如果你有时间,还有一个问题。现在它只输出5%的加价,500美元时为25美元。我如何让它输出525美元?@Chad在未来的问题中(如果适用),我建议包括您的输入、预期输出和实际输出。这使您更容易理解代码可能出错的地方。谢谢您,John。我会记得这样做,我建议将
priceIn
参数和返回值也更改为十进制。没有理由编写对字符串进行运算的数学函数。选择最合适的类型,让调用者处理他们在这方面的争论。
public string GetWholesalePrice(string priceIn)
{
decimal price = decimal.Parse(priceIn);
string priceOut = string.Empty;
//for loop for the class wholesale to set
for (int i = 5; i < 11; i++)
{
decimal percent = i / 100m;
decimal wsale = price * percent;
priceOut += string.Format(" Markup of {0} percent is {1:c}\n", i, wsale);
}
return priceOut;
}