如何提高此代码的效率?

时间:2015-11-19 04:33:37

标签: vb.net visual-studio visual-studio-2010

这段代码的重点是,当用户在文本框中输入他们想要打印多少张纸时,每张纸需要花费0.30美元,如果用户购买超过499张,价格将变为每张纸0.28美元等。但是当他们检查时墨水的无线电按钮每10页打印额外费用为0.10美元。我已经弄清楚如何编码它,但它看起来很乱。

>>>from collections import Counter
>>>import re
>>>s= "['admobapioauthcert', 'admobapinewsession', 'admobendusercampaign']"
>>>w=[i for sb in re.findall(r'(?=(mob)|(api)|(admob))',s) for i in sb ]#Change (mob)|(api)|(admob) what you want
>>>{k:v for k,v in Counter(filter(bool, w)).most_common()}
>>>{'mob': 3, 'admob': 3, 'api': 2}

2 个答案:

答案 0 :(得分:0)

首先,尝试使用just而不是radiobutton并检查checkstate,这样你只需要一个控件

第二,我喜欢不得不直接检查textinput的想法,不要像E.Solicito建议的那样使用按钮进行检查,但这取决于你的偏好

第三,这是代码

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.click

        If txtinput.Text.Length = 0 Then
            paper = 0
        Else
            paper = Val(Me.txtinput.Text)
        End If

        Check_Ink()

    End Sub

    Private Sub Checkbox_Status(sender As Object, e As EventArgs) Handles chb_withink.CheckedChanged
        Check_Ink()
    End Sub

    Private Sub Check_Ink()

        Select Case paper
            Case paper <= 499
                price = paper * 0.3
            Case paper <= 749
                price = paper * 0.28
            Case paper <= 999
                price = paper * 0.27
            Case Else
                price = paper * 0.25
        End Select

        If chb_withink.Checked Then

            remainder = paper Mod 10
            cardstock = paper - remainder
            extra = cardstock / 100
            price += extra

        End If

        Me.lblprice.Text = price

    End Sub

答案 1 :(得分:-1)

试试这个

new int[] { 2,5,12,14}.Contains(x);