小数点小于零的除法 vb.net
Division with decimals below zero vb.net
我正在 vb.net 中编写一个程序,它允许用户输入一定数量的钱,然后输出赚取该数量所需的最少硬币数量。例如,如果用户输入 £3.43,程序将输出 1 x £2、1x £1、2x £20p、1x 2p 和 1x 1p。
我的程序适用于一磅和两磅硬币,但一旦程序检查较小的硬币(例如低于 1 英镑),程序就无法识别任何东西。该程序使用除法和模数来执行此操作,对于每个硬币,它将金额除以硬币的价值(忽略余数),将名为 "remainder" 的变量的值设置为金额模数除以硬币的值硬币并将金额重置为余数
的值
代码如下:
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 英镑的金额都不需要硬币。
您将 ammount
设置为 Mod 1
的余数(因此对于 2.53 的输入,您的 ammount
现在是 0.53)
然后将余数乘以 100 并将其存回 remainder
,因此余数为 53
,ammount
仍为 0.53
..
然后您继续对 0.53 的 ammount
进行数学运算,因此您的程序将提出以下问题:
- "How many times does 50p go into 0.53p?" 0
- "How many times does 20p go into 0.53p?" 0
- "How many times does 10p go into 0.53p?" 0
- 等等..
我还没有给你问题的确切答案,但这里有足够的答案 - 如果你认为最后一部分是你自己而不是我,这对你来说将是一个更好的学习练习直接给你答案:)
祝你好运
我正在 vb.net 中编写一个程序,它允许用户输入一定数量的钱,然后输出赚取该数量所需的最少硬币数量。例如,如果用户输入 £3.43,程序将输出 1 x £2、1x £1、2x £20p、1x 2p 和 1x 1p。
我的程序适用于一磅和两磅硬币,但一旦程序检查较小的硬币(例如低于 1 英镑),程序就无法识别任何东西。该程序使用除法和模数来执行此操作,对于每个硬币,它将金额除以硬币的价值(忽略余数),将名为 "remainder" 的变量的值设置为金额模数除以硬币的值硬币并将金额重置为余数
的值代码如下:
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 英镑的金额都不需要硬币。
您将 ammount
设置为 Mod 1
的余数(因此对于 2.53 的输入,您的 ammount
现在是 0.53)
然后将余数乘以 100 并将其存回 remainder
,因此余数为 53
,ammount
仍为 0.53
..
然后您继续对 0.53 的 ammount
进行数学运算,因此您的程序将提出以下问题:
- "How many times does 50p go into 0.53p?" 0
- "How many times does 20p go into 0.53p?" 0
- "How many times does 10p go into 0.53p?" 0
- 等等..
我还没有给你问题的确切答案,但这里有足够的答案 - 如果你认为最后一部分是你自己而不是我,这对你来说将是一个更好的学习练习直接给你答案:)
祝你好运