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;
}
}
}
9397 次
3 に答える
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();
}
}
于 2012-11-21T14:23:46.367 に答える
3
あなたがしたい:
if (Convert.ToInt32(txtHours.Text) > 30)
{
//normal code here
}
else
{
//normal code
}
最初にそれをintとして解析する必要がある場合は、ifがブール条件をテストできるように、比較を行います。
于 2012-11-21T14:23:52.463 に答える
0
試す
if (Convert.ToInt32(txtHours.Text) > 30)
{ ...
于 2012-11-21T14:25:37.490 に答える