C# 计算自动取款机可以分发的纸币数量

C# 计算自动取款机可以分发的纸币数量,c#,algorithm,C#,Algorithm,我试图创建一个程序,它将显示估算的金额以及ATM可以分发的10美元和1美元纸币的数量,但它不会显示正确的1美元纸币数量 int amount = int.Parse(txtAmount.Text); int tenNotes=0, oneNotes=0; CalculateNotes(amount, ref tenNotes, ref oneNotes); private void CalculateNotes( int amount, ref int tenNotes, ref int On

我试图创建一个程序,它将显示估算的金额以及ATM可以分发的10美元和1美元纸币的数量,但它不会显示正确的1美元纸币数量

int amount = int.Parse(txtAmount.Text);
int tenNotes=0, oneNotes=0;
CalculateNotes(amount, ref tenNotes, ref oneNotes);

private void CalculateNotes( int amount, ref int tenNotes, ref int OneNotes)
{
   tenNotes = amount /10;
   OneNotes = amount - amount % 10;
   rtbDisplay.AppendText("Ammount is " + amount + "Ten notes is" + tenNotes + "One notes is" + OneNotes);
}

这是我为1美元纸币尝试过的不同计算方法的输出,但它不起作用。
我应该使用out而不是ref,还是我的计算有错误?谢谢您的帮助。

您应该更改这一行

OneNotes = amount - amount % 10;
对这个

OneNotes = amount - (tenNotes * 10);
请重新考虑使用int.Parse从文本框读取输入。如果用户键入的整数值无效,则会出现异常。使用Int32.TryParse可以轻松避免此异常

最后,我还建议对参数使用out关键字,而不是ref.

请参见

您应该更改此行

OneNotes = amount - amount % 10;
对这个

OneNotes = amount - (tenNotes * 10);
请重新考虑使用int.Parse从文本框读取输入。如果用户键入的整数值无效,则会出现异常。使用Int32.TryParse可以轻松避免此异常

最后,我还建议对参数使用out关键字,而不是ref.

参见

除Steve给出的解决方案外,您还可以执行以下操作:

更改:

OneNotes = amount - amount % 10;
致:

其他备选方案- 应该注意的是,您尝试执行的操作已经是System.Math库中预先存在的函数。因此,您可以替换以下代码块:

tenNotes = amount /10;
OneNotes = amount - amount % 10;
与:


作为Steve给出的解决方案的替代方案,您还可以执行以下操作:

更改:

OneNotes = amount - amount % 10;
致:

其他备选方案- 应该注意的是,您尝试执行的操作已经是System.Math库中预先存在的函数。因此,您可以替换以下代码块:

tenNotes = amount /10;
OneNotes = amount - amount % 10;
与: