VBA更改计算器

时间:2010-09-24 23:10:11

标签: vb.net

我正在创建一个VB 2008更改计算器作为一项任务。该计划是使用支付的金额 - 应计金额计算总额。(这是正常的)。在那之后,它将这个数额分解为美元,四分之一,硬币,镍币和便士。我遇到的问题是有时便士,镍币或硬币的数量将是负数。例如2.99美元= 3美元和-1便士。

解决

感谢回复,这是我能用我有限的知识做的工作。

Option Explicit On
Option Strict Off
Option Infer Off

Public Class frmMain

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear boxes

        lblDollarsAmount.Text = String.Empty
        lblQuartersAmount.Text = String.Empty
        lblDimesAmount.Text = String.Empty
        lblNickelsAmount.Text = String.Empty
        lblPenniesAmount.Text = String.Empty
        txtOwed.Text = String.Empty
        txtPaid.Text = String.Empty
        lblAmountDue.Text = String.Empty
        txtOwed.Focus()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close application' 
        Me.Close()
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        ' Find Difference between Total Price and Total Received 
        lblAmountDue.Text = Val(txtPaid.Text) - Val(txtOwed.Text)
        Dim intChangeAmount As Integer = lblAmountDue.Text * 100

        'Declare Integers  
        Dim intDollarsBack As Integer
        Dim intQuartersBack As Integer
        Dim intDimesBack As Integer
        Dim intNickelsBack As Integer
        Dim intPenniesBack As Integer

        ' Change Values 
        Const intDollarValue As Integer = 100
        Const intQuarterValue As Integer = 25
        Const intDimeValue As Integer = 10
        Const intNickelValue As Integer = 5
        Const intPennyValue As Integer = 1

        'Dollars 
        intDollarsBack = CInt(Val(intChangeAmount \ intDollarValue))
        intChangeAmount = intChangeAmount - Val(Val(intDollarsBack) * intDollarValue)
        lblDollarsAmount.Text = intDollarsBack.ToString

        'Quarters 
        intQuartersBack = CInt(Val(intChangeAmount \ intQuarterValue))
        intChangeAmount = intChangeAmount - Val(Val(intQuartersBack) * intQuarterValue)
        lblQuartersAmount.Text = intQuartersBack.ToString

        'Dimes 
        intDimesBack = CInt(Val(intChangeAmount \ intDimeValue))
        intChangeAmount = intChangeAmount - Val(Val(intDimesBack) * intDimeValue)
        lblDimesAmount.Text = intDimesBack.ToString

        'Nickels 
        intNickelsBack = CInt(Val(intChangeAmount \ intNickelValue))
        intChangeAmount = intChangeAmount - Val(Val(intNickelsBack) * intNickelValue)
        lblNickelsAmount.Text = intNickelsBack.ToString

        'Pennies 
        intPenniesBack = CInt(Val(intChangeAmount \ intPennyValue))
        intChangeAmount = intChangeAmount - Val(Val(intPenniesBack) * intPennyValue)
        lblPenniesAmount.Text = intPenniesBack.ToString

    End Sub
End Class

5 个答案:

答案 0 :(得分:1)

在$ 2.99的示例中,查看您计算inDollarsBack的位置。当你需要时,你显然得到3的值。如果没有放弃它,想一想将2.99除以1会产生3的值的可能原因。

答案 1 :(得分:1)

您可以使用以下更改计算器,然后更新UI。

Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.VisualBasic

Public Class ChangeCalculator

    Private denominations As List(Of Denomination)

    Public Sub New()

        Me.denominations = New List(Of Denomination)

        With Me.denominations

            .Add(New Denomination With {.Value = 1D})
            .Add(New Denomination With {.Value = 0.25D})
            .Add(New Denomination With {.Value = 0.1D})
            .Add(New Denomination With {.Value = 0.05D})
            .Add(New Denomination With {.Value = 0.01D})

        End With

    End Sub

    Public Sub Calculate(ByVal change As Decimal)

        Me.ResetDenominationUnits()

        Dim remainingChange = change

        For Each denomination In (From d In Me.denominations
                                  Order By d.Value Descending
                                 )

            If remainingChange > denomination.Value Then

                denomination.Units = CInt(
                    Conversion.Int(remainingChange / denomination.Value)
                )
                remainingChange -= denomination.Value * denomination.Units

            End If

        Next

    End Sub

    Public ReadOnly Property Dollars As Integer
        Get
            Return Me.GetUnits(1D)
        End Get
    End Property

    Public ReadOnly Property Quarters As Integer
        Get
            Return Me.GetUnits(0.25D)
        End Get
    End Property

    Public ReadOnly Property Dimes As Integer
        Get
            Return Me.GetUnits(0.1D)
        End Get
    End Property

    Public ReadOnly Property Nickels As Integer
        Get
            Return Me.GetUnits(0.05D)
        End Get
    End Property

    Public ReadOnly Property Pennies As Integer
        Get
            Return Me.GetUnits(0.01D)
        End Get
    End Property

    Private Function GetUnits(ByVal denomination As Decimal) As Integer
        Return (From d In Me.denominations
                Where d.Value = denomination
               ).Single().Units
    End Function

    Private Sub ResetDenominationUnits()

        For Each denomination In Me.denominations
            denomination.Units = 0
        Next

    End Sub

End Class

Public Class Denomination

    Public Property Value As Decimal
    Public Property Units As Integer

End Class

答案 2 :(得分:1)

由于我的回答类似于Tim Murphy's,我认为这个问题比较直接,你不需要update the UI

Module Module1

    Sub Main()
        Console.WriteLine("Enter the amount of change or type EXIT to close.")
        Dim input = Console.ReadLine
        Do While input.ToUpper.Trim <> "EXIT"
            Dim c = GetChange(CDec(Val(input)))
            Console.WriteLine("{0} dollars, {1} quarters, {2} dimes, {3} nickels and {4} pennies", _
                              c.Dollars, c.Quarters, c.Dimes, c.Nickels, c.Pennies)

            Console.WriteLine(vbCrLf & vbCrLf & "Enter the amount of change or type EXIT to quit.")
            input = Console.ReadLine
        Loop
    End Sub

    Public Function GetChange(ByVal change As Decimal) As Change
        Dim denominations = New Decimal() {1D, 0.25D, 0.1D, 0.05D, 0.01D}
        Dim c(4) As Integer


        For i = 0 To denominations.Length - 1
            If change >= denominations(i) Then
                c(i) = CInt(Conversion.Int(change / denominations(i)))
                change -= (c(i) * denominations(i))
            End If
        Next

        Dim r As New Change
        With r
            .Dollars = c(0)
            .Quarters = c(1)
            .Dimes = c(2)
            .Nickels = c(3)
            .Pennies = c(4)
        End With

        Return r
    End Function

    Public Structure Change
        Dim _dollars As Integer
        Dim _quarters As Integer
        Dim _nickels As Integer
        Dim _dimes As Integer
        Dim _pennies As Integer

        Public Property Dollars() As Integer
            Get
                Return _dollars
            End Get
            Set(ByVal value As Integer)
                _dollars = value
            End Set
        End Property

        Public Property Quarters() As Integer
            Get
                Return _quarters
            End Get
            Set(ByVal value As Integer)
                _quarters = value
            End Set
        End Property

        Public Property Dimes() As Integer
            Get
                Return _dimes
            End Get
            Set(ByVal value As Integer)
                _dimes = value
            End Set
        End Property

        Public Property Nickels() As Integer
            Get
                Return _nickels
            End Get
            Set(ByVal value As Integer)
                _nickels = value
            End Set
        End Property

        Public Property Pennies() As Integer
            Get
                Return _pennies
            End Get
            Set(ByVal value As Integer)
                _pennies = value
            End Set
        End Property
    End Structure

End Module

答案 3 :(得分:0)

Public Shared Function CalculateChange(ByVal dblDollarsPaid As Double, ByVal dblDollarsOwed As Double)
    Dim intChangeCents As Integer
    Dim change As New Change()

    intChangeCents = CInt((dblDollarsPaid - dblDollarsOwed) * 100)

    change.Dollars = intChangeCents \ 100
    intChangeCents = intChangeCents Mod 100

    change.Quarters = intChangeCents \ 25
    intChangeCents = intChangeCents Mod 25

    change.Dimes = intChangeCents \ 10
    intChangeCents = intChangeCents Mod 10

    change.Nickels = intChangeCents \ 5
    intChangeCents = intChangeCents Mod 5

    change.Pennies = intChangeCents

    Return change
End Function

用法:

Dim c As Change
c = CalculateChange(13.26, 2.99)
Console.WriteLine("{0} dollars, {1} quarters, {2} dimes, {3} nickels and {4} pennies", _
                   c.Dollars, c.Quarters, c.Dimes, c.Nickels, c.Pennies)

\是整数除法的运算符,例如5 \ 2是2,而不是2.5。

Change结构来自Alex Essilfie's answer

答案 4 :(得分:0)

很简单,就是这样。要解决负面更改,请删除除Explicit

之外的所有选项
Option Explicit On

Public Class frmMoneyChange
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lblFivesAmount.Text = String.Empty
        lblTensAmount.Text = String.Empty
        lblDollarsAmount.Text = String.Empty
        lblQuartersAmount.Text = String.Empty
        lblDimesAmount.Text = String.Empty
        lblNickelsAmount.Text = String.Empty
        lblPenniesAmount.Text = String.Empty
        txtOwed.Text = String.Empty
        txtPaid.Text = String.Empty
        lblAmountDue.Text = String.Empty
        txtOwed.Focus()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Me.Close()
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click


        Dim decChangeAmount As Decimal
        Dim intDollarsBack As Integer
        Dim intQuartersBack As Integer
        Dim intDimesBack As Integer
        Dim intNickelsBack As Integer
        Dim intPenniesBack As Integer
        Dim decPaid As Decimal
        Dim decOwed As Decimal

        Const intFiveDollarValue As Integer = 5
        Const intTenDollarValue As Integer = 10
        Const intDollarValue As Integer = 1
        Const decQuarterValue As Decimal = 0.25
        Const decDimeValue As Decimal = 0.1
        Const decNickelValue As Decimal = 0.05
        Const decPennyValue As Decimal = 0.01



        decOwed = Convert.ToDecimal(txtOwed.Text)
        decPaid = Convert.ToDecimal(txtPaid.Text)
        decChangeAmount = decPaid - decOwed
        lblAmountDue.Text = decChangeAmount.ToString("C")

        'Ten Dollars
        intDollarsBack = (decChangeAmount * 100 \ intTenDollarValue * 100)
        decChangeAmount = decChangeAmount - (intDollarsBack) * intTenDollarValue
        lblTensAmount.Text = intDollarsBack.ToString

        'Five Dollars
        intDollarsBack = (decChangeAmount * 100 \ intFiveDollarValue * 100)
        decChangeAmount = decChangeAmount - (intDollarsBack) * intFiveDollarValue
        lblFivesAmount.Text = intDollarsBack.ToString

        'Dollars 
        intDollarsBack = (decChangeAmount * 100 \ intDollarValue * 100)
        decChangeAmount = decChangeAmount - (intDollarsBack) * intDollarValue
        lblDollarsAmount.Text = intDollarsBack.ToString

        'Quarters 
        intQuartersBack = (decChangeAmount * 100 \ decQuarterValue * 100)
        decChangeAmount = decChangeAmount - (intQuartersBack) * decQuarterValue
        lblQuartersAmount.Text = intQuartersBack.ToString()

        'Dimes 
        intDimesBack = (decChangeAmount * 100 \ decDimeValue * 100)
        decChangeAmount = decChangeAmount - (intDimesBack) * decDimeValue
        lblDimesAmount.Text = intDimesBack.ToString

        'Nickels 
        intNickelsBack = (decChangeAmount * 100 \ decNickelValue * 100)
        decChangeAmount = decChangeAmount - (intNickelsBack) * decNickelValue
        lblNickelsAmount.Text = intNickelsBack.ToString

        'Pennies 
        intPenniesBack = (decChangeAmount * 100 \ decPennyValue * 100)
        decChangeAmount = decChangeAmount - (intPenniesBack) * decPennyValue
        lblPenniesAmount.Text = intPenniesBack.ToString

    End Sub

End Class