Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim intDays As Integer
Dim decRate As Decimal
Dim decFinalAmount As Decimal
Const _cradPenny As Decimal = 0.01D
Const _cradNickel As Decimal = 0.05D
If IsNumeric(txtDays.Text) Then
intDays = Convert.ToInt32(value:=txtDays.Text)
If intDays >= 10 And intDays <= 22 Then
If radPenny.Checked = True Then
decRate = _cradPenny
ElseIf radNickel.Checked = True Then
decRate = _cradNickel
End If
Else
MsgBox("Nope, nope. Please enter an amount between 10 and 22.", , "Input Error")
txtDays.Text = ""
txtDays.Focus()
End If
End If
decFinalAmount = decRate(intDays - 1) ^ intDays
lblTotalPay.Text = "Your total pay is " & decFinalAmount.ToString("C")
lblTotalPay.Visible = True
End Sub
I have decRate
declared, with constants that are determined by the radio button selected.
What am I missing?
我声明了DecRate,其中的常量由选中的单选按钮确定。我遗漏了什么?
I am expecting to run the formula:
我希望运行公式:
decFinalAmount = decRate(intDays - 1) ^ intDays
更多回答
decRate
is of type Decimal
, not an array of Decimal
values to use the indexer notation like so. Apart from that, what is the math equation used to calculate decFinalAmount
? t = r * (d - 1) ^ d or...?
decRate的类型是Decliter,而不是Decliter值的数组,这样就可以使用索引器表示法。除此之外,用于计算decFinalAmount的数学公式是什么?t = r *(d - 1)^ d或.?
If you're trying to multiply then you need to use the multiplication operator.
如果你正在尝试乘法,那么你需要使用乘法运算符。
优秀答案推荐
In VB.NET, there is no implicit multiplication for a(b+c), you have to write a*(b+c). If you write x(y), you might be referring to a method named x that you are passing a parameter y to, or it could be an array x that you want the y-th value of.
在VB.NET中,a(b+c)没有隐式乘法,必须写a*(b+c)。如果您编写x(Y),您可能会引用一个名为x的方法,您将向该方法传递一个参数y,或者它可能是一个您想要其第y个值的数组x。
Also, it is usually a good idea to factor out things like that calculation. You can make sure that that small part of the program functions correctly on its own, and it makes it re-usable without having to type out the formula again.
此外,将这样的计算计算出来通常是一个好主意。您可以确保程序的这一小部分自行正确运行,并使其可重复使用,而不必再次键入公式。
There are some other parts of VB that you might like to investigate, so here is my take on what you might be able to learn more from:
您可能会想要研究一下VB的其他一些部分,所以下面是我对您可能能够从中学到更多东西的看法:
Function CompoundInterestFinalAmount(initial As Decimal, rate As Decimal, paymentPeriodBase As Double, paymentPeriods As Double) As Decimal
Return CDec(initial * ((1 + rate / paymentPeriodBase) ^ (paymentPeriodBase * paymentPeriods)))
End Function
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Const CRADPENNY As Decimal = 0.01D
Const CRADNICKEL As Decimal = 0.05D
' INTERESTRATE is the actual number, so 5% interest is 0.05D.
Const INTERESTRATE As Decimal = 0.05D
Const DAYSMIN As Integer = 10
Const DAYSMAX As Integer = 22
Dim days As Integer
If Int32.TryParse(txtDays.Text, days) Then
If days >= DAYSMIN AndAlso days <= DAYSMAX Then
Dim initialAmount As Decimal
Select Case True
Case radPenny.Checked
initialAmount = CRADPENNY
Case radNickel.Checked
initialAmount = CRADNICKEL
Case Else
MsgBox("Please choose pennies or nickels.")
Exit Sub
End Select
Dim finalAmount = CompoundInterestFinalAmount(initialAmount, INTERESTRATE, 1, days)
lblTotalPay.Text = $"Your total pay is {finalAmount:C}."
lblTotalPay.Visible = True
End If
Else
MsgBox($"Nope, nope. Please enter an amount between {DAYSMIN} and {DAYSMAX}.", MsgBoxStyle.Information, "Input Error")
txtDays.Clear()
txtDays.Focus()
End If
End Sub
Thanks dr.null - I got the decRate setup as an array and my error was no more!
谢谢,Null博士-我得到了作为数组的DecRate设置,我的错误再也没有了!
更多回答
我是一名优秀的程序员,十分优秀!