【问题标题】:Visual basic membership program using select case? [closed]使用选择案例的 Visual Basic 会员计划? [关闭]
【发布时间】:2014-12-03 09:52:48
【问题描述】:

我正在编写一个程序,询问用户他们的年龄和他们成为会员的年数,我试图通过使用选择案例来做到这一点。但是它似乎不起作用,所有年龄段都很好,但是如果我使用复选框说他们是老手并且应该获得折扣,它就不起作用了。此外,他们成为会员的年数不会改变价格/类别,我的程序很简单,我不确定为什么它不起作用。

节目截图:http://gyazo.com/ebab66526068f4c81a30c624aada7f7c

代码:

Public Class Form1

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    Dim x As Integer
    Dim y As Integer

    x = Val(txtAge.Text)
    y = Val(txtYears.Text)


    Select Case x
        Case Is <= 18
            lblPrice.Text = ("£60")
            lblCategory.Text = ("Junior")
        Case 19 To 49
            lblPrice.Text = ("£120")
            lblCategory.Text = ("Senior")
        Case Is >= 50
            lblPrice.Text = ("£80")
            lblCategory.Text = ("Over 50")
        Case chkVeteran.Checked
            lblPrice.Text = ("£50")
        Case Is <= 18 And y >= 2
            lblPrice.Text = ("£40")
            lblCategory.Text = ("2 year junior discount")
        Case Is >= 50 And y >= 10
            lblPrice.Text = ("£90")
            lblCategory.Text = ("10 year senior discount")
        Case chkVeteran.Checked And y >= 10
            lblPrice.Text = ("£20")
            lblCategory.Text = ("10 year veteran discount")
    End Select


End Sub

结束类

【问题讨论】:

  • 定义“它不起作用”。它做什么?当您在调试器中逐步执行此操作时,观察到的行为与预期行为有何不同?发生这种情况时的运行时值是多少?
  • Select Case x 指定x 的选择,然后您将其与不相关的Case chkVeteran.Checked 进行比较。我会将值和字符串放入变量中,然后在案例之后应用复选框逻辑,然后更新 UI

标签: vb.net select


【解决方案1】:

使用 select case 非常困难,因为当您使用 Case Is = 2 之类的代码时,您将无法同时传递 x 和 y 的值

if then else更好用

公开课表1

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

    x = Val(txtAge.Text)
    y = Val(txtYears.Text)


    If x <= 18 Then
        lblPrice.Text = ("£60")
        lblCategory.Text = ("Junior")
    ElseIf x >= 19 And x <= 49 Then
        lblPrice.Text = ("£120")
        lblCategory.Text = ("Senior")
    ElseIf x >= 50 And y < 10 Then
        lblPrice.Text = ("£80")
        lblCategory.Text = ("Over 50")
    ElseIf chkVeteran.Checked And y> Then
        lblPrice.Text = ("£50")
    ElseIf x <= 18 And y >= 2 Then
        lblPrice.Text = ("£40")
        lblCategory.Text = ("2 year junior discount")
    ElseIf x >= 50 And y >= 10 Then
        lblPrice.Text = ("£90")
        lblCategory.Text = ("10 year senior discount")
    ElseIf chkVeteran.Checked And y >= 10 Then
        lblPrice.Text = ("£20")
        lblCategory.Text = ("10 year veteran discount")
    Else
        MessageBox.Show("Some of the fields are empty")
    End If
End Sub

结束类

【讨论】:

    【解决方案2】:

    只有一个Case 块可以运行,但您似乎希望多个块可以运行。考虑一下您的子句:

    Case Is <= 18
        lblPrice.Text = ("£60")
        lblCategory.Text = ("Junior")
    Case chkVeteran.Checked
        lblPrice.Text = ("£50")
    Case Is <= 18 And y >= 2
        lblPrice.Text = ("£40")
        lblCategory.Text = ("2 year junior discount")
    

    如果 x 小于或等于 18,则 first Case 块将运行。无论chkVeterany 的状态如何,其他的都将被忽略。一系列Case 子句需要本质上是互斥的,这样逻辑才有意义。例如:

    Case Is <= 18 And y < 2
        lblPrice.Text = ("£60")
        lblCategory.Text = ("Junior")
    Case Is <= 18 And y >= 2
        lblPrice.Text = ("£40")
        lblCategory.Text = ("2 year junior discount")
    

    这两个子句是互斥的,所以任何时候只能应用其中一个。每个子句将从第一个到最后一个顺序检查,第一个匹配的将执行。所以你应该设置你的逻辑,使得匹配的 first 子句应该是匹配的 only 子句。


    编辑:回应单独的评论线程...

    您将逻辑定义为:

    体育俱乐部有三类会员费。青少年(18 岁以下)每年支付 60 英镑,老年人(19-49 岁)支付 120 英镑,退伍军人(50 岁及以上)支付 80 英镑。成为会员 2 年或以上的青少年可减免 20 英镑。成为会员 10 年或更长时间的老年人和退伍军人可享受 30 英镑的优惠。

    在我看来,这听起来像是 2 个条件序列。第一个将定义基本费率,如下所示:

    If age < 19 Then
        rate = 60
    Else If age < 50 Then
        rate = 120
    Else
        rate = 80
    End If
    

    第二个定义基本费率的折扣,如下所示:

    If age < 19 And tenure > 1 Then
        rate = rate - 20
    Else If age > 18 And tenure > 9 Then
        rate = rate - 30
    End If
    

    两个独立的条件序列,每个都检查准确需求中明确定义的条件。然后,您可以进一步重构它们以使语义更清晰。例如,将“Junior、Senior、Veteran”的定义提取到单独的逻辑中:

    If IsJunior(age) Then
        rate = 60
    Else If IsSenior(age) Then
        rate = 120
    Else If IsVeteran(age) Then
        rate = 80
    End If
    

    您可以为其中的每一个创建函数,例如:

    Function IsJunior(ByVal age As Integer) As Boolean
        Return age < 19
    End Function
    

    这将实现的细节提取到更小的辅助函数中,并允许以语义概念而不是实现细节来表达顶层逻辑,从而使逻辑的整体表达更清楚地匹配需求给出的描述。

    【讨论】:

    • 这些都没有意义,所以我决定将其作为 if 语句。
    • 如果我使用 Elseif,如果我使用 'Elseif x = 19 to 49" 为什么它不起作用
    • @Finn:定义“不起作用”。有错误吗?更新后的代码是什么?调试时它的行为如何?
    • 我现在才写,写完我会贴出来
    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2014-02-09
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多