vb.net中的折扣和利率计划

时间:2012-10-17 03:44:08

标签: asp.net vb.net

我想弄清楚如何完成这个程序并让计算显示在文本框中。这是我到目前为止,基本上代码将采取购买价格和赎回率,并计算折扣和利率。不知道我做错了什么。

Option Strict On

Public Class _Default
    Inherits System.Web.UI.Page

    Dim Purchase As Double
    Dim Redemption As Double
    Dim DiscountRate As Double
    Dim InterestRate As Double

    Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) _
                                            Handles btnCalculate.Click

        Double.TryParse(txtPurchase.Text, Purchase)
        Double.TryParse(txtRedemption.Text, Redemption)
        Double.TryParse(txtDiscount.Text, DiscountRate)
        Double.TryParse(txtInterest.Text, InterestRate)

        If (CDbl(txtPurchase.Text) <= 0) Then
            MsgBox("Please enter an amount greater than 0")

        End If

        If (CDbl(txtRedemption.Text) <= 0) Then
            MsgBox("Please enter an amount greater than 0")
        End If

        DiscountRate = Purchase - Redemption / Purchase
        InterestRate = Purchase - Redemption / Redemption

    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

看起来您遇到Operator Precedence问题需要一些括号。您在第一种情况下通过购买划分您的赎回,然后从您的购买中减去赎回。在第二种情况下,您将通过兑换兑换兑换,并从您的购买中减去1的结果。试试这个。您还需要将结果分配给您正在显示的控件。

DiscountRate = (Purchase - Redemption) / Purchase
InterestRate = (Purchase - Redemption) / Redemption
txtDiscount.Text = DiscountRate.ToString
txtInterest.Text = InterestRate.ToString

或更简单

txtDiscount.Text = ((Purchase - Redemption) / Purchase).ToString
txtInterest.Text = ((Purchase - Redemption) / Redemption).ToString

答案 1 :(得分:0)

解析它们时,它们不会自动绑定到控件。看起来你只需要在计算后添加它:

txtDiscount.Text = DiscountRate
txtInterest.Text = InterestRate