Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

.net - Calculate the number of dollars, quarters, dimes, nickels, an pennies with random amount of pennies

I have a homework in VB.NET where I have to calculate the number of dollars, quarters, dimes, nickels, an pennies I will receive when I cash in my pennies at a bank. The number of pennies I have can be random numbers. And so far, my code is here:

Option Explicit On
Option Strict Off
Option Infer Off


Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()

End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

    Dim intDollars As Integer
    Dim intQuarters As Integer
    Dim intDimes As Integer
    Dim intNickels As Integer
    Dim intPennies As Integer

    Integer.TryParse(txtNumPennies.Text, intDollars)
    intDollars = txtNumPennies.Text  100
    txtDollars.Text = Convert.ToString(intDollars)

    Integer.TryParse(txtNumPennies.Text, intQuarters)
    intQuarters = (txtNumPennies.Text - txtDollars.Text * 100)  25
    txtQuarters.Text = Convert.ToString(intQuarters)

    Integer.TryParse(txtNumPennies.Text, intDimes)
    intDimes = (txtNumPennies.Text Mod 25)  10
    txtDimes.Text = Convert.ToString(intDimes)

    Integer.TryParse(txtNumPennies.Text, intNickels)
    intNickels = (txtNumPennies.Text Mod 10)  5
    txtNickels.Text = Convert.ToString(intNickels)

    Integer.TryParse(txtNumPennies.Text, intPennies)
    intPennies = (txtNumPennies.Text Mod 5)  1
    txtPennies.Text = Convert.ToString(intPennies)

End Sub

Private Sub txtNumPennies_TextChanged(sender As Object, e As EventArgs) Handles txtNumPennies.TextChanged

    txtDollars.Text = String.Empty
    txtQuarters.Text = String.Empty
    txtDimes.Text = String.Empty
    txtNickels.Text = String.Empty
    txtPennies.Text = String.Empty

End Sub
End Class

Somehow, it is not calculating right, especially the nickles. I would gladly appreciate any helpful tips! Thank you so much in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not removing the number of pennies that have already been converted, so you are doubling up on your money (if only real life was that easy!). In addition, you keep trying to convert the text without checking if it is valid - and you are repeating yourself.

if Integer.TryParse(txtNumPennies.Text, numPennies) Then
    ' all your conversions go here. I am providing two examples below.
    intDollars = numPennies  100
    txtDollars.Text = Convert.ToString(intDollars)
    numPennies = numpennies - (intDollars * 100)

    intQuarters = numpennies  25
    txtQuarters.Text = Convert.ToString(intQuarters)
    numPennies = numpennies - (intQuarters * 25)
    ' ... and so on
End If

If you follow the logic above, you will see it mirrors what happens in real life - convert pennies to dollars first, the converted pennies no longer exist.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...