嘿伙计们,我对编程非常陌生,我正在尝试制作一个基本的程序来计算摊铺的成本,这是我的第二次尝试并且我已经正确标记了所有内容但我仍然得到了错误,有人可以帮我吗?
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate(object sender, EventArgs e)
{
const double dPAVING_COST = 5.40;
int iLength;
int iWidth;
int iArea;
double dCost;
// obtain length from user
iLength = Convert.ToInt32(txtLength.text);
// obtain width from user
iWidth = Convert.ToInt32(txtWidth.Text);
// calculate area to be paved
iArea = iWidth * iLength;
//calculate cost
dCost = iArea * dPAVING_COST;
// output the data
lblDisplay.Text = "Area: " + iArea.ToString("N2")
+ Environment.NewLine
+ " Paving cost: "
+ dCost.ToString("C");
}
private void txtWidth(object sender, EventArgs e)
{
}
private void lblLength(object sender, EventArgs e)
{
}
private void lblWidth(object sender, EventArgs e)
{
}
private void lblDisplay(object sender, EventArgs e)
{
}
private void txtLength(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
我在Console App中对此进行了测试,但没有出现任何错误
确保您正在将有效数据输入到TextBox中以下是一个关于如何使string.Format更清晰,更易读的示例
也将此行iLength = Convert.ToInt32(txtLength.text);
更改为
iLength = Convert.ToInt32(txtLength.Text);
const double dPAVING_COST = 5.40;
int iLength;
int iWidth;
int iArea;
double dCost;
// obtain length from user
iLength = Convert.ToInt32(20);//replace 20 with txtLength.Text
// obtain width from user
iWidth = Convert.ToInt32(32);//relace 32 with txtWidth.Text
// calculate area to be paved
iArea = iWidth * iLength;
//calculate cost
dCost = iArea * dPAVING_COST;
// output the data
Console.WriteLine(string.Format("Area: {0} {1} Paving cost: {2}",iArea.ToString("N2"),
Environment.NewLine,dCost.ToString("C")));