现在我会用什么代码?我有一部分工作,但它开始有点混乱。
谢谢。
对于想要了解更多信息的人:
说我有一美元,我去商店买东西。我必须要求用户输入他们花费的金额,然后计算更改并打印到屏幕上。
然后我应该使用最少数量的四分之一,硬币和硬币,并将其打印到屏幕上。
非常感谢任何帮助。
答案 0 :(得分:7)
我将在这里走出困境,并假设OP正在谈论变更,就像从交易中退回的钱一样。
如果是这种情况,那么它可能是家庭作业,所以只有伪代码。
最简单的第一次尝试方法如下。让cost
为交易成本,tendered
为交付的金额(以美分计),让我们进一步假设您的经济只有美元账单,季度和便士(以制作我的代码)更小)。
change = tendered - cost
if change < 0:
print "Pay up some more cash, cheapskate!"
stop
dollars = 0
quarters = 0
cents = 0
while change >= 100:
dollars = dollars + 1
change = change - 100
while change >= 25:
quarters = quarters + 1
change = change - 25
while change >= 1:
cents = cents + 1
change = change - 1
print dollars " dollar(s), " quarters " quarter(s), and " cents " cent(s)."
现在无疑可以通过使用模运算和除运算符来提高效率,但我将其作为读者的练习。
我的建议是用铅笔和一些纸条坐下以下栏目(以10美元的价格购买2美元和9点三分钱):
tendered cost change dollars quarters cents
-------- -------- -------- -------- -------- --------
1000 293
并使用纸张中的当前值逐行扫描代码,,并在新的值发生变化时写下新值。
这将极大地帮助您理解。
响应您的更新:
我有一美元,我去商店买东西。我必须要求用户输入他们花费的金额,然后计算更改并打印到屏幕上。然后我应该使用最少数量的四分之一,硬币,镍币和便士,然后将其打印到屏幕上。
这与我上面的内容非常相似:
tendered = 100
input cost
cost = int (cost * 100)
change = tendered - cost
if change < 0:
print "Pay up some more cash, cheapskate!"
stop
print "Change is ", (format "$9.99", change / 100)
quarters = 0, dimes = 0, nickels = 0, pennies = 0
while change >= 25:
quarters = quarters + 1
change = change - 25
while change >= 10:
dimes = dimes + 1
change = change - 10
while change >= 5:
nickels = nickels + 1
change = change - 5
while change >= 1:
pennies = pennies + 1
change = change - 1
print quarters, " quarters"
print dimes , " dimes"
print nickels , " quarters"
print pennies , " pennies"
答案 1 :(得分:2)
自动化流程的更“先进”方式是:
Private Function CalculateChange(ByVal dAmount As Decimal) As Decimal()
Dim arrNotesAvailable() As Decimal = {200D, 100D, 50D, 20D, 10D, 5D, 2D, 1D, 0.5D, 0.2D, 0.1D, 0.05D, 0.02D, 0.01D}
Dim arrChangeAmounts(arrNotesAvailable.Length - 1) As Decimal
For iIndex As Integer = 0 To arrNotesAvailable.Length - 1
arrChangeAmounts(iIndex) = dAmount \ arrNotesAvailable(iIndex)
dAmount = dAmount Mod arrNotesAvailable(iIndex)
Next
Return arrChangeAmounts
End Function
这将自动返回一个数组,其中包含要发布的每个单位的确切数量。
更高级的解决方案:
Private Function CalculateChange(ByVal dAmount As Decimal) As Decimal()
Dim arrNotesAvailable() As Decimal = {200D, 100D, 50D, 20D, 10D, 5D, 2D, 1D, 0.5D, 0.2D, 0.1D, 0.05D}
Dim arrChangeAmounts(arrNotesAvailable.Length - 1) As Decimal
For iIndex As Integer = 0 To arrNotesAvailable.Length - 2
arrChangeAmounts(iIndex) = (dAmount * 100) \ (arrNotesAvailable(iIndex) * 100)
dAmount = dAmount Mod arrNotesAvailable(iIndex)
Next
arrChangeAmounts(arrNotesAvailable.Length - 1) = Math.Ceiling((dAmount * 100) / (arrNotesAvailable(arrNotesAvailable.Length - 1) * 100))
Return arrChangeAmounts
End Function
这将分解第二个指定单位所需的纸币和硬币数量,然后用最小的货币单位“填充”剩余部分。对于那些不以1(1美分或1日元)的单位进行交易的国家。在我们国家,我们的交易价格为5美分。
答案 2 :(得分:0)
刚做了类似的作业。 公共类Form1 '姓名:亚伦假日 '类:IME 211 '目的:改变计算
Dim Owed As Double
Dim Tend As Double
Dim Change As Double
Dim _20D As Integer
Dim _10D As Integer
Dim _5D As Integer
Dim _1D As Integer
Dim _25C As Integer
Dim _10C As Integer
Dim _5C As Integer
Dim _1C As Integer
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear Due Amounts
txtOwed.Text = ""
txtTend.Text = ""
lblChange.Text = ""
'Clear Dollar Amounts
lbl20D.Text = ""
lbl10D.Text = ""
lbl5D.Text = ""
lbl1D.Text = ""
'Clear Cents Amounts
lbl25C.Text = ""
lbl10C.Text = ""
lbl5C.Text = ""
lbl1C.Text = ""
txtOwed.Focus()
End Sub
Private Sub btnRing_Click(sender As Object, e As EventArgs) Handles btnRing.Click
'Check to see if Empty
If ((txtOwed.Text) = Nothing) Then
lblChange.Text = ("Error")
MessageBox.Show("Please Enter Amount Owed")
Exit Sub
End If
If ((txtTend.Text) = Nothing) Then
lblChange.Text = ("Error")
MessageBox.Show("Please Enter Amount Tendered")
Exit Sub
End If
'Set Dim Values
Owed = txtOwed.Text
Tend = txtTend.Text
'Calculate Change due
Change = (Tend - Owed)
lblChange.Text = Change.ToString("C2")
'Check if they paid enough
If ((lblChange.Text) < 0) Then
lblChange.Text = ("Pay Up!")
MessageBox.Show("Please Pay full Amount!")
Exit Sub
End If
'Set Dim Values
_20D = 0 '$20
_10D = 0 '$10
_5D = 0 '$5
_1D = 0 '$1
_25C = 0 '$0.25
_10C = 0 '$0.10
_5C = 0 '$0.05
_1C = 0 '$0.01
'Find Amounts of Each
Do While Change >= 20
_20D = _20D + 1
Change = Change - 20
Loop
'Display $20s
lbl20D.Text = _20D
'================================================
Do While Change >= 10
_10D = _10D + 1
Change = Change - 10
Loop
'Display $10s
lbl10D.Text = _10D
'================================================
Do While Change >= 5
_5D = _5D + 1
Change = Change - 5
Loop
'Display $5s
lbl5D.Text = _5D
'================================================
Do While Change >= 1
_1D = _1D + 1
Change = Change - 1
Loop
'Display $1s
lbl1D.Text = _1D
'================================================
Do While Change >= 0.25
_25C = _25C + 1
Change = Change - 0.25
Loop
'Display $0.25s
lbl25C.Text = _25C
'================================================
Do While Change >= 0.1
_10C = _10C + 1
Change = Change - 0.1
Loop
'Display $0.10s
lbl10C.Text = _10C
'================================================
Do While Change >= 0.05
_5C = _5C + 1
Change = Change - 0.05
Loop
'Display $0.05s
lbl5C.Text = _5C
'================================================
Do While Change >= 0.01
_1C = _1C + 1
Change = Change - 0.01
Loop
'Display $0.01s
lbl1C.Text = _1C
'Display Thank you
If ((lblChange.Text) >= 0) Then
MessageBox.Show("Thank you, Come Again")
End If
End Sub
End Class