当我计算更改(货币 - 账单)时,它总是显示为零(0)。我不知道我哪弄错了。
这是我的班级:
class changecalc
{
int a, b;
public int B
{
get { return b; }
set { b = value; }
}
public int A
{
get { return a; }
set { a = value; }
}
public changecalc()
{
A = 0;
B = 0;
}
public changecalc(int C,int D)
{
C = a;
D = b;
}
public int calculate()
{
//this is the money-bill
return a - b;
}
}
在我的表格中:
if (int.Parse(txtboxmoney.Text) >= int.Parse(txtboxbill.Text)) {
//display the change
changecalc aa = new changecalc(int.Parse(txtboxmoney.Text), int.Parse(txtboxbill.Text));
change.Text = aa.calculate().ToString();
}
else {
//error if money is lower than the bill
txtboxmoney.Clear();
change.Clear();
MessageBox.Show("Your Money is not enough");
}
我在哪里错过了?
答案 0 :(得分:4)
以下功能更新C& D为零时应更新后备字段,&湾这使得田地成为一个& b默认为零,因此差异始终为零。
public changecalc(int C,int D)
{
C = a;
D = b;
}
按如下方式更改代码:
public changecalc(int C,int D)
{
a = C;
b = D;
}
您也可以考虑重构您的课程:
更新的代码:
class ChangeCalculator
{
public int Money { get; set; }
public int Bill { get; set; }
public ChangeCalculator()
{
Money = 0;
Bill = 0;
}
public ChangeCalculator(int money, int bill)
{
Money = money;
Bill = bill;
}
public int Calculate()
{
return Money - Bill;
}
}