小数小于零的分区vb.net

时间:2017-11-07 13:13:49

标签: vb.net division modulus

我正在vb.net中编写一个程序,它允许用户输入一定数量的钱,然后输出制作该数量所需的最少数量的硬币。例如,如果用户输入£3.43,程序将输出1 x£2,1xx£1,2x£20p,1x 2p和1x 1p。

我让这个程序适用于一英镑和两英镑的硬币,但是一旦程序检查到较小的硬币(E.G.低于1英镑),程序就无法识别任何东西。程序使用除法和模数来做到这一点,对于每个硬币,它将金额除以硬币的值(忽略余数),将名为“余数”的变量的值设置为金币模数的金额和将金额重置为余数的值

代码如下:

Public Class MinimumCoins
Dim ammount As Decimal
Dim TwoPound As Integer
Dim OnePound As Integer
Dim fifty As Integer
Dim twenty As Integer
Dim ten As Integer
Dim five As Integer
Dim two As Integer
Dim one As Integer
Dim remainder As Decimal

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    ammount = InputBox("Enter Ammount")

    TwoPound = ammount \ 2
    remainder = ammount Mod 2
    ammount = remainder

    OnePound = ammount \ 1
    remainder = ammount Mod 1
    ammount = remainder

    remainder = remainder * 100

    fifty = ammount \ 50
    remainder = ammount Mod 50
    ammount = remainder

    twenty = ammount \ 20
    remainder = ammount Mod 20
    ammount = remainder

    ten = ammount \ 10
    remainder = ammount Mod 10
    ammount = remainder

    five = ammount \ 5
    remainder = ammount Mod 5
    ammount = remainder

    two = ammount \ 2
    remainder = ammount Mod 2
    ammount = remainder

    one = ammount \ 1

    lblOutput.Text = TwoPound & " X" & " £2.00" & vbNewLine & OnePound & " X" & " £1.00" & vbNewLine & fifty & "X" & " 50p" & vbNewLine & twenty & " X" & " 20p" & vbNewLine & ten & " X" & " 10p" & vbNewLine & five & " X" & " 5p" & vbNewLine & two & " X" & " 2p" & vbNewLine & one & " X" & " 1p"



End Sub
End Class

程序运行的前两次(一磅和两磅硬币)它完全正常但是程序显示任何小于1英镑的金额都不需要硬币。

1 个答案:

答案 0 :(得分:1)

您将ammount设置为Mod 1的剩余部分(因此,对于2.53的输入,您的ammount现在为0.53)

然后将剩余部分乘以100并将其存储回remainder,剩余部分为53ammount仍为0.53 ..

然后你继续在0.53的ammount上做数学,所以你的程序会问这些问题:

  • “50p进入0.53p多少次?” 0
  • “20p进入0.53p的次数是多少?” 0
  • “10p进入0.53p的次数是多少?” 0
  • 依旧......

我没有给你你问题的确切答案,但是这里有足够的工作来解决它 - 如果你认为最后一部分是通过自己而不是我给予它,这将是一个更好的学习练习你直接答案:)

祝你好运