VB中的2字母状态缩写代码

时间:2014-11-13 09:03:34

标签: vb.net

任何人都可以帮助我在学校得到这个,我真的需要帮助。它在Visual Basic 2010 Express中。问题是将状态名称更改为2个字母并计算它们的税率。

加利福尼亚州的零售客户(州代码=“CA”)按购买征收销售税 - 加州税率为10%。

来自纽约(州代码=“NY”)和佛罗里达州(州代码=“FL”)的零售客户也按5%的税率纳税。

我在这里看到了这个,但它似乎对我不起作用:

if statename.tolower = "new york" then 
    statename = "NY"
else if

我能够填写它,但它不适合我,我是VB的新手...这就像我的第二个工作...输入来自文本框,他说我们应该确保当状态输入代码后应将税额添加到总数中。

Public Class Form1

Public Class State
    Public Sub New(ByVal name As String, ByVal abbr As String, ByVal taxPercent As Decimal)
        Me.Name = name
        Me.Abbreviation = abbr
        Me.TaxPercent = taxPercent
    End Sub
    Public Property Name As String
    Public Property Abbreviation As String
    Public Property TaxPercent As Decimal
End Class

我们被告知要使用

                         Try
                        Catch ex As Exception

                        End Try


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

        Dim states As New List(Of State)
        states.Add(New State("California", "CA", 10))
        states.Add(New State("New York", "NY", 5))
        states.Add(New State("Florida", "FL", 5))

        For Each state As State In states
            Console.WriteLine("State: {0} Abbrevation: {1} Tax: {2}%",
                              state.Name, state.Abbreviation, state.TaxPercent)
        Next
    Catch ex As Exception

    End Try
End Sub

结束班

我真的很感谢你在做什么先生,请你解释一下我应该如何把它放在编码中。我正在使用的表单名称是Form1我试图让它工作然后我可以做一个新的项目,这样我就能更好地理解它了谢谢


以下是我到目前为止的完整编码

Public Class Form1

Public Class State
    Public Sub New(ByVal name As String, ByVal abbr As String, ByVal taxPercent As Decimal)
        Me.Name = name
        Me.Abbreviation = abbr
        Me.TaxPercent = taxPercent
    End Sub
    Public Property Name As String
    Public Property Abbreviation As String
    Public Property TaxPercent As Decimal
End Class

Const U_P_S_DECIMAL As Decimal = 7D

Const SALES_TAX_RATE_SINGLE As Single = 0.1 '10 Percent Rate

这是Private Sub

'declear module-level variables
Private TotalQuantityInteger As Integer
Private TotalSalesDecimal As Decimal

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try
        '5 percent salses tex rate
        Const SALES_TEX_RATE_SINGLE As Single = 0.05 '5 percent rate


        Dim states As New List(Of State)
        states.Add(New State("California", "CA", 10))
        states.Add(New State("New York", "NY", 5))
        states.Add(New State("Florida", "FL", 5))

        For Each state As State In states
            Console.WriteLine("State: {0} Abbrevation: {1} Tax: {2}%",
                              state.Name, state.Abbreviation, state.TaxPercent)
        Next

        'Declare variables
        Dim SubDecimal, SalesTaxDecimal, TotalDueDecimal, TotalCostDecimal, ShippingCostDecimal As Decimal
        'Declare variables and convert value from textbox controls to memory
        Dim PriceDecimal As Decimal = Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)
        Dim QuantityInteger As Integer = Integer.Parse(QuantityTextBox.Text, Globalization.NumberStyles.Number)

        'Process - Compute values
        'Subtotal = price times the quantity of books
        TotalCostDecimal = PriceDecimal * QuantityInteger

        'Sales tex = sales tax rate times the subtotal minus discount amount
        SalesTaxDecimal = Decimal.Round(Convert.ToDecimal(TotalCostDecimal * SALES_TEX_RATE_SINGLE), 2)

        SubDecimal = SalesTaxDecimal + ShippingCostDecimal

        'Total due is the subtotal minus discount amount plus sales tex
        TotalDueDecimal = TotalCostDecimal + SubDecimal

        If UPSRadioButton.Checked Then 'compute the shipping cost
            ShippingCostDecimal = U_P_S_DECIMAL * QuantityInteger
        End If


        'Output - display output formatted as currency
        SubtotalTextBox.Text = TotalCostDecimal.ToString("C")
        TotalDueTextBox.Text = TotalDueDecimal.ToString("C")
        salestaxTextBox.Text = SalesTaxDecimal.ToString("N")
        ShippingCostTextBox.Text = ShippingCostDecimal.ToString("N")

        'Accumulate total sales and total books sold
        TotalQuantityInteger += QuantityInteger
        TotalSalesDecimal += TotalDueDecimal
    Catch ex As Exception

    End Try
End Sub

结束班

当完成表单时,它应该就像这个链接中的图片一样

[IMG] http://i61.tinypic.com/2cwjuhk.png[/IMG]

1 个答案:

答案 0 :(得分:0)

您应该使用类来存储State

的相关信息
Public Class State
    Public Sub New(name As String, abbr As String, taxPercent As Decimal)
        Me.Name = name
        Me.Abbreviation = abbr
        Me.TaxPercent = taxPercent
    End Sub
    Public Property Name As String
    Public Property Abbreviation As String
    Public Property TaxPercent As Decimal
End Class

现在您可以填写List(Of State)

Dim states As New List(Of State)
states.Add(New State("California", "CA", 10))
states.Add(New State("New York", "NY", 5))
states.Add(New State("Florida", "FL", 5))

并且你需要封装在一个类中的所有信息,你不需要摆弄字符串操作:

For Each state As State In states
    Console.WriteLine("State: {0} Abbrevation: {1} Tax: {2}%",
                      state.Name, state.Abbreviation, state.TaxPercent)
Next