所以我在这段代码上遇到了麻烦。我让美元部分可以正常工作,而其余部分则不能。我试图将529美分换成5美元1季度0硬币0镍4便士,这是我的代码。
Option Explicit On
Option Strict On
Module Module1
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
If (amount >= 100) Then
dollars = amount \ 100
amount = amount - (100 * dollars)
ElseIf (amount >= 25) Then
quarters = amount \ quarters
amount = amount - (25 * quarters)
ElseIf (amount >= 10) Then
dimes = amount \ dimes
amount = amount - (10 * dimes)
ElseIf (amount >= 5) Then
nickels = amount \ nickels
amount = amount - (5 * nickels)
ElseIf (amount >= 1) Then
pennies = amount
End If
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.ReadLine()
End Sub
End Module
答案 0 :(得分:0)
编写一个自动进行硬币计数的功能。你快到了每次计数时,只需使用模数来查找剩余的便士。
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
dollars = parseDenomination(amount, 100)
quarters = parseDenomination(amount, 25)
dimes = parseDenomination(amount, 10)
nickels = parseDenomination(amount, 5)
pennies = amount
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.WriteLine($"{getAmountString(dollars, "Dollar")}, {getAmountString(quarters, "Quarter")}, {getAmountString(dimes, "Dime")}, {getAmountString(nickels, "Nickel")}, {getAmountString(pennies, "Penny")},")
Console.ReadLine()
End Sub
Private Function parseDenomination(ByRef amount As Integer, count As Integer) As Integer
Dim result = amount \ count
amount = amount Mod count
Return result
End Function
Private Function getAmountString(amount As Integer, name As String) As String
Return $"{name}{If(amount = 1, "", "s")}: {amount}"
End Function
我添加了另一种方法来帮助获取字符串输出。不过,“便士/便士”的分类有点像-对您来说是英语。