gpt4 book ai didi

c# - mscorlib.dll 中出现类型为 'System.FormatException' 的未处理异常

转载 作者:太空狗 更新时间:2023-10-29 20:30:51 30 4
gpt4 key购买 nike

每次我点击计算按钮时,我都会收到以下消息:mscorlib.dll 中出现类型为“System.FormatException”的未处理异常附加信息:输入字符串的格式不正确。

我应该在输入的工资低于 9.75 美元时显示一条消息。

然后高亮显示这行代码:

if (Convert.ToInt32(RateTextBox.Text) < 9.75m)

这是计算按钮方法(我确定我犯了不止一个错误):

 private void CalcButton_Click(object sender, EventArgs e)
{ // The “Calculate” button calculates gross pay, taxes, and net pay and then displays name, department, gross pay, taxes, and net pay using currency format for various amounts in the rich text box
// Gross pay= (hours * rate)
// Taxes= (25% of gross pay)
// Net pay (gross pay ?taxes)



//calculate


Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text);
Taxes = TAX * Gross_pay;
Net_Pay = Gross_pay - Taxes;

annual_salary = Net_Pay;



//display
DisplayOutPut.Text = "";
DisplayOutPut.Text += NameTextBox.Text + "\n";
DisplayOutPut.Text += "Hours:" + HoursTextBox.Text + "\n";
DisplayOutPut.Text += "Rate:" + RateTextBox.Text + "\n";
DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n"; // Hours*Rate
DisplayOutPut.Text += "Taxes:" + Taxes.ToString("C") + "\n";
DisplayOutPut.Text += "Net Pay:" + Net_Pay.ToString("C");

//handling the invalid inputs
if (NameTextBox.Text == "")
{ MessageBox.Show("Name is missing.", "Error"); }

if (Convert.ToInt32(HoursTextBox.Text) >= 70)
{ MessageBox.Show("Please Enter a Valid hour.", "Invalid data type."); }

if (RateTextBox.Text == "" && (RateTextBox.Text == ","))
{ MessageBox.Show("Please Enter a valid amount.", "Invalid data type ($)"); }

if (Convert.ToInt32(HoursTextBox.Text) >= 70)
{ MessageBox.Show("You have exceeded the maximum hours per week."); }

else if (Convert.ToInt32(HoursTextBox.Text) < 10)
{ MessageBox.Show("You cannot input less than 10 hours."); }

if (Convert.ToInt32(RateTextBox.Text) < 9.75m)
{ MessageBox.Show("Please enter the minimum wage."); }


//overtime pay
if (Convert.ToInt32(HoursTextBox.Text) >= 41)
{
Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) * 1.5m;
DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n";
}

//Medical/Dental and 401k deductions...as well as tax collected.
if (MedicalDentalDeductions.Checked)
{
Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 50.00m;
}

if(FourOneKDeduction.Checked)
{
Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 0.05m * 100;


}


if ((MedicalDentalDeductions.Checked) && (FourOneKDeduction.Checked))
{ Taxes = TAX * Gross_pay; }
DisplayOutPut.Text= "Medical/Dental deduction:" + Taxes +"401k deduction:"+ Taxes;

}

最佳答案

您正在将数字转换为整数。因此,如果您输入(比如说)9.5 会失败,因为它不是整数。

您几乎肯定会使用 decimal.TryParse,注意返回值会说明解析是否成功。

decimal userRate;
if (!decimal.TryParse(RateTextBox.Text, out userRate))
{
// Indicate to the user that the input is invalid, and return from
// the method
}
// Now use userRate

关于c# - mscorlib.dll 中出现类型为 'System.FormatException' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19367234/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com