本文介绍了用随机数量的便士计算美元、四分之一、一角硬币、五分硬币、一便士的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.NET 中有一个作业,我必须计算我在银行兑现时会收到的美元、四分之一、一角硬币、五分硬币和一便士的数量.我拥有的便士数量可以是随机数.到目前为止,我的代码在这里:

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.

推荐答案

您并没有删除已经转换的便士数量,因此您的钱增加了一倍(如果现实生活如此简单!).此外,您一直在尝试转换文本而不检查它是否有效 - 而且您正在重复自己.

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.

这篇关于用随机数量的便士计算美元、四分之一、一角硬币、五分硬币、一便士的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:40