namespace exer4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTotal_Click(object sender, EventArgs e)
{
int overtime = Convert.ToInt32(txtHours.Text) - 30;
int salary = Convert.ToInt32(txtHours.Text)*250;
double tax = (salary + overtime) * .10;
int deduction = salary - (300 + 400);
//operator '>' cannot be applied to operands of type 'string' and 'int'
if Convert.ToInt32(txtHours.Text > 30)
{
lblName.Text = txtName.Text;
lblSalary.Text = Convert.ToString(overtime *120) + (salary - (deduction - tax));
}
else
{
// //operator '*' cannot be applied to operands of type 'string' and 'int'
lblSalary.Text = Convert.ToString(txtHours.Text) * 250;
}
}
}
答案 0 :(得分:4)
你的if语句应该是
if (Convert.ToInt32(txtHours.Text) > 30)
另一行应该是
lblSalary.Text = Convert.ToString(Convert.ToInt32(txtHours.Text) * 250);
或者更好的是将txtHours文本转换为int并将其保存在变量中以便重用。
private void btnTotal_Click(object sender, EventArgs e)
{
int hours = Convert.ToInt32(txtHours.Text);
int overtime = hours - 30;
int salary = hours * 250
double tax = (salary + overtime) * .10;
int deduction = salary - (300 + 400);
if(hours > 30)
{
lblName.Text = txtName.Text;
lblSalary.Text = ((overtime *120) + (salary - (deduction - tax))).ToString();
}
else
{
lblSalary.Text = salary.ToString();
}
}
答案 1 :(得分:3)
你想这样做:
if (Convert.ToInt32(txtHours.Text) > 30)
{
//normal code here
}
else
{
//normal code
}
如果首先需要像往常一样将其解析为int,那么进行比较,以便if可以测试布尔条件。
答案 2 :(得分:0)
尝试
if (Convert.ToInt32(txtHours.Text) > 30)
{ ...