【问题标题】:visual basic <> issues视觉基本<>问题
【发布时间】:2018-09-24 16:26:46
【问题描述】:

我正在使用输入框在数组中查找元素。这是我目前拥有的:

arrayNumber = InputBox("Please enter your desired number.")
For i = 0 To 9
    If arrayNumber = data(i) Then
        outputMessage1 = ("Your number is found in the first array. " & i & " is the index of the desired number.") 
    Else
    End If
Next

For i = 0 To 9
    If arrayNumber = data2(i) Then
        outputMessage2 = ("Your number is found in the second array. " & i & " is the index of the desired number.")
    Else
    End If
Next

For i = 0 To 9
    If arrayNumber = data3(i) Then
        outputMessage3 = ("Your number is found in the third array. " & i & " is the index of the desired number.")
    Else
    End If
Next

Selection.TypeText (outputMessage1 & " " & outputMessage2 & " " & outputMessage3)

For i = 0 To 9
    If arrayNumber <> i Then
        notFound = ("Sorry the element is not found.")
    Else
    End If
Next

Selection.TypeText (notFound)

...

我目前有两个问题:即使 arrayNumber 确实等于 i,也会弹出 notFound 消息。我该如何解决这个问题?

另外,我知道我目前在每个数组中都有元素的索引。有没有一种方法可以找到元素的最早外观,查看所有三个数组的组合(即,如果 4 是第一个数组中的第三个索引但第二个数组的第二个索引,我想说最早出现在第二个数组)。

【问题讨论】:

  • 调试您的代码,即设置断点并逐行执行。一旦实际状态与您的预期不同,您就发现了 n 个问题,您可以专门对此进行调查。至少,您可以向我们描述细节。您不能仅仅通过阅读代码来解决代码问题。您运行它并观察它的实际效果。

标签: arrays vb.net


【解决方案1】:

这可能会帮助你,或者至少给你一个想法

arrayNumber = InputBox("Please enter your desired number.")
For i = 0 To 9
    If arrayNumber = data(i) Then
        outputMessage1 = ("Your number is found in the first array. " & i & " is the index of the desired number.") 

    ElseIf arrayNumber = data2(i) Then
        outputMessage2 = ("Your number is found in the second array. " & i & " is the index of the desired number.")

    ElseIf arrayNumber = data3(i) Then
        outputMessage3 = ("Your number is found in the third array. " & i & " is the index of the desired number.")
    Else
        notFound = ("Sorry the element is not found.")
    End If
Next
Selection.TypeText (outputMessage1 & " " & outputMessage2 & " " & outputMessage3)

如果if statementelseif 之一被触发,它将跳过另一个ifelseif

【讨论】:

    【解决方案2】:

    明确解决显示“未找到”的问题,即使应用了有效数字也是如此。

    这是我将用作初级开发人员培训元素的类型。主要是因为在我看来,您的需求要么不理解,要么在他们的需求中不清楚和简洁。

    您已声明您的代码正在显示未找到消息,即使提供的数字是 0 到 9 之间的值,而您的代码如下所示:

    For i = 0 To 9
        If arrayNumber <> i Then
            notFound = ("Sorry the element is not found.")
        Else
        End If
    Next
    

    它这样做是因为你告诉它这样做。每当 arrayNumber 不等于 i 时设置 NotFound,但当 arrayNumber 等于 i 时不执行任何操作。

    无论如何,您有一个要求和一个建议的逻辑解决方案,但它们的行为与您的预期不同。我发现,将不符合预期的逻辑转化为“真实”的词有助于澄清我在做什么。我会将这段代码翻译成以下语句:

    对于我范围内的每个数字,如果提供的数字 (ArrayNumber) 是 不等于我的增量器 (i) 然后将 NotFound 设置为“对不起 未找到元素。”

    与往常一样,代码按照我们的指示行事,并且代码的行为与编写的一样。事情是您实际尝试做的是确定一个值是否包含在一个范围内,而您执行的唯一检查是它不包含在该范围内。考虑以下语句:

    当 提供的数字 (ArrayNumber) 不等于找到的任何值 在我的范围内

    该语句更准确地确定了您尝试在代码中执行的操作。虽然它们是相似的陈述,但焦点已经转移。其核心要求是“当 Y 为真时执行 X”。但是另一种看待这种逻辑的方式是“X,直到 Y 为假”。我经常发现数据检查的要求通常写成“当 Y 为真时执行 X”,但我们通常可以更容易地(至少在我看来)将这些要求编码为“X,直到 Y 为假”。

    这给了我们以下陈述:

    NotFound 设置为“抱歉找不到元素”,直到提供 number (arrayNumber) 等于在范围内找到的任何值

    对我来说,这种风格逻辑是很好的防御性编程。它为您提供了一个开始处理数据的立场。这意味着您可以按预期执行,也可以处理未达到预期的数据。

    现在,由于我们有了重新定义的意图声明,我们可以以不同的方式处理代码。

    notFound = ("Sorry the element is not found.")
    For i = 0 To 9
        If arrayNumber = i Then 
            notFound = "" 
            Exit For
        End If
    Next 
    

    此代码意味着我们从数据“未能”包含在范围检查中的立场开始,我们正在验证数据以证明该立场错误。

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 2011-07-29
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多