0

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

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
4

2 回答 2

1

看起来您遇到了运算符优先级问题,您需要一些括号。在第一种情况下,您将您的兑换除以购买,然后从您的购买中减去。在第二个中,您将赎回除以赎回,然后从您的购买中减去结果 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
于 2012-10-17T03:53:28.677 回答
0

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

txtDiscount.Text = DiscountRate
txtInterest.Text = InterestRate
于 2012-10-17T03:51:12.950 回答