我在223行获得了一个例外://calculation for vat ( room cost take 25% )
System.DivideByZeroException:'试图除以零。'
我尝试过的修复方法是在第224行到第230行添加if else语句以允许总和重试,这样它只是放一个没有崩溃的数字,但这没有帮助。任何可能的修复?
public partial class paint : Form
{
private int price, price21;
public paint()
{
InitializeComponent();
}
// Button for Calculations
private void SUB_Click(object sender, EventArgs e)
{
if (validation() == 0)
{
errorProvider1.Clear();
MessageBox.Show("Totals Below");
}
// northwall calculations and inputs to variables
if (txtnorthheight.Text == "")
{
txtnorthheight.Text = ("0");
}
if (txtnorthlength.Text == "")
{
txtnorthlength.Text = ("0");
}
decimal height1 = decimal.Parse(txtnorthheight.Text);
decimal length1 = decimal.Parse(txtnorthlength.Text);
decimal area1 = height1 * length1;
lblnortharea.Text = area1.ToString("n");
// southwall calculations and inputs to variables
if (txtsouthheight.Text == "")
{
txtsouthheight.Text = ("0");
}
if (txtsouthlength.Text == "")
{
txtsouthlength.Text = ("0");
}
decimal height2 = decimal.Parse(txtsouthheight.Text);
decimal length2 = decimal.Parse(txtsouthlength.Text);
decimal area2 = height2 * length2;
lblsoutharea.Text = area2.ToString("n");
// Eastwall calculations and inputs to variables
if (txteasthheight.Text == "")
{
txteasthheight.Text = ("0");
}
if (txteasthlength.Text == "")
{
txteasthlength.Text = ("0");
}
decimal height3 = decimal.Parse(txteasthheight.Text);
decimal length3 = decimal.Parse(txteasthlength.Text);
decimal area3 = height3 * length3;
lbleastarea.Text = area3.ToString("n");
// Westhwall calculations and inputs to variables
if (txtwesthheight.Text == "")
{
txtwesthheight.Text = ("0");
}
if (txtwestlength.Text == "")
{
txtwestlength.Text = ("0");
}
decimal height4 = decimal.Parse(txtwesthheight.Text);
decimal length4 = decimal.Parse(txtwestlength.Text);
decimal area4 = height4 * length4;
lblwestharea.Text = area4.ToString("n");
// window frame calculations and inputs to variables
if (txtwinheight.Text == "")
{
txtwinheight.Text = ("0");
}
if (txtwinlength.Text == "")
{
txtwinlength.Text = ("0");
}
decimal height5 = decimal.Parse(txtwinheight.Text);
decimal length5 = decimal.Parse(txtwinlength.Text);
decimal area5 = height5 * length5;
lblwinharea.Text = area5.ToString("n");
// culculation to work out total area - window size. this is borken in to two aprts:
// area 1 + area 2 + area 3 + area 5 then take away area 5 which is the window.
decimal areatotal = (area1 + area2 + area3 + area4) - area5;
lbltotalarea.Text = areatotal.ToString("n");
// culculations for total height and length for output in invoice.
decimal Heightt = (height1 + height2 + height3 + height4) - height5;
decimal Legntht = (length1 + length2 + length3 + length4) - length5;
Height2.Text = Heightt.ToString("n");
Legnth.Text = Legntht.ToString("n");
// variables for inputs taken on qucilty options and undercoat option.
if (txtunder.Text == "")
{
txtunder.Text = ("0");
}
if (txtprice.Text == "")
{
txtprice.Text = ("0");
}
decimal price = decimal.Parse(txtprice.Text);
decimal undercoat = decimal.Parse(txtunder.Text);
decimal roomcost = (Heightt * Legntht) * price * undercoat;
lblcost.Text = roomcost.ToString("c");
//culculation for vat ( room cost take 25% )
decimal Vattotal = (25 % roomcost);
lblvat.Text = Vattotal.ToString("c");
//culculation for roomcost + vat
Decimal vt = Vattotal + roomcost;
lblbt.Text = vt.ToString("c");
}
// reset form button
private void Totalcost_Click(object sender, EventArgs e)
{
txtwinlength.Text = "";
txtnorthheight.Text = "";
txtnorthlength.Text = "";
txtwesthheight.Text = "";
txtwestlength.Text = "";
txtsouthheight.Text = "";
txtsouthlength.Text = "";
txteasthheight.Text = "";
txteasthlength.Text = "";
txtwinheight.Text = "";
txtprice.Text = "";
lblnortharea.Text = "";
lblsoutharea.Text = "";
lblwestharea.Text = "";
lbleastarea.Text = "";
lblwinharea.Text = "";
lblcost.Text = "";
lbltotalarea.Text = "";
Height2.Text = "";
Legnth.Text = "";
invoice.Text = "";
}
// invoice number genrator
private void button1_Click(object sender, EventArgs e)
{
Random slumpGenerator = new Random(); int tal;
tal = slumpGenerator.Next();
invoice.Text = tal.ToString("n");
}
// invoice OUTput
private void label22_Click_2(object sender, EventArgs e)
{
Random slumpGenerator = new Random(); int tal;
tal = slumpGenerator.Next();
invoice.Text = tal.ToString("n");
}
//input vaildation only allowing numbers and decimals.
private void txtnorthheight_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtnorthlength_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtsouthheight_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtsouthlength_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtprice_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtwesthheight_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtwestlength_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txteasthheight_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txteasthlength_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtwinheight_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtwinlength_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
private void txtunder_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
MessageBox.Show("Please Enter A Valid Value");
}
}
public int validation ()
{
int flag = 0;
//North input valdation
if (txtnorthheight.Text == "")
{
txtnorthheight.Focus();
errorProvider1.SetError(txtnorthheight, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
if (txtnorthlength.Text == "")
{
txtnorthlength.Focus();
errorProvider1.SetError(txtnorthlength, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
//South input valdation
if (txtsouthheight.Text == "")
{
txtsouthheight.Focus();
errorProvider1.SetError(txtsouthheight, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
if (txtsouthlength.Text == "")
{
txtsouthlength.Focus();
errorProvider1.SetError(txtsouthlength, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
//West input valdation
if (txtwesthheight.Text == "")
{
txtwesthheight.Focus();
errorProvider1.SetError(txtwesthheight, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
if (txtwestlength.Text == "")
{
txtwestlength.Focus();
errorProvider1.SetError(txtwestlength, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
//East input valdation
if (txteasthheight.Text == "")
{
txteasthheight.Focus();
errorProvider1.SetError(txteasthheight, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
if (txteasthlength.Text == "")
{
txteasthlength.Focus();
errorProvider1.SetError(txteasthlength, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
//Window input valdation
if (txtwinheight.Text == "")
{
txtwinheight.Focus();
errorProvider1.SetError(txtwinheight, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
if (txtwinlength.Text == "")
{
txtwinlength.Focus();
errorProvider1.SetError(txtwinlength, MessageBox.Show("please Enter a vaild input between 0.9m and 1.5m.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
//quality input valdation
if (txtprice.Text == "")
{
txtprice.Focus();
errorProvider1.SetError(txtprice, MessageBox.Show("please Enter a vaild input of the above options ( You Must Enter 1 of the Options).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
// undercoat valdation
if (txtunder.Text == "")
{
txtunder.Focus();
errorProvider1.SetError(txtunder, MessageBox.Show("Please enter the Undercoat input of 0.45, if you do not want undercoat enter 0.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).ToString());
flag = 1;
}
return flag;
}
private void txtnorthheight_Click(object sender, EventArgs e)
{
errorProvider1.Clear();
}
//end of valdation
}
答案 0 :(得分:1)
%
符号是C#
中的modulus operator,这意味着在将左侧划分为右侧后,它会为您提供余数。在您的代码中,您似乎希望获得25%的房费,而不是25的剩余部分除以房费。
所以,这一行:
decimal Vattotal = (25 % roomcost);
应改为:
decimal Vattotal = roomcost * .25;
您获得例外的原因是roomcost
为0
,除以零是非法操作。