VB.net(鸭子号码)(偶数)

时间:2018-03-24 15:11:36

标签: excel vba vb.net math numeric

我需要使用VB来查找问题中所说的 Duck Numbers 偶数

Q1 :VB.NET应用程序检查一个数字是否为Duck Number <或者

Q2 :偶数两个范围内的数字 实施例

implementation "com.google.firebase:firebase-auth:12.0.0"
implementation "com.google.android.gms:play-services-auth:12.0.0"

implementation "com.android.support:design:27.x.x"
implementation "com.android.support:customtabs:27.x.x"
implementation "com.android.support:cardview-v7:27.x.x"

我想告诉大家我一直在做什么,请尽可能纠正我。

计划1:

Start Number = 10
End Number = 100
Output:
Even numbers from 10 to 100 are
12 14 16 18 22 24 26 28 …….

计划2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i, j, k As Integer
    j = 0
    i = TextBox1.Text
    k = i
    While i > 0
        If (i Mod 10 = 0) Then
            j = 1
            Exit While
            i /= 10
            If k > 0 And j = 1 Then
                MsgBox("The Given Number Is a Duck Number.")
            Else
                MsgBox("The Given Number Is not a Duck Number.")
            End If
        End If
    End While
End Sub

1 个答案:

答案 0 :(得分:1)

我经常使用VBA,但很少使用VB,甚至没有编译器,但我认为这些解决方案应该让您了解更简单的方法。

Public Function isDuck(n As Long) As Boolean
    If InStr(CStr(n), "0") <> 0 Then isDuck = True
End Function

甚至更简单:

 Public Function isDuck(n As Long) As Boolean
    isDuck = (n Like "*0*")
End Function
Public Sub listEvens(nStart As Long, nStop As Long)
    If nStart / 2 <> nStart \ 2 Then nStart = nStart + 1
    Do While nStart <= nStop
        Debug.Print nStart
        nStart = nStart + 2
    Loop
End Sub

......或其他方式(只是为了狡猾!)将用以下代替If nStart...行:

nStart = nStart - (nStart / 2 <> nStart \ 2)

......甚至:

nStart = nStart -(nStart \2=1)

...然后使用For..Next循环:

Public Sub listEvens(nStart As Long, nStop As Long)
    For nStart = nStart - (nStart / 2 = 1) To nStop Step 2
        Debug.Print nStart
    Next
End Sub

所以我无法直接使用如果你有Excel或其他MS Office产品,你可以将它们粘贴在那里进行演示,我怀疑它们很容易“调整”到VB中。

listevens 7,16
 8 
 10 
 12 
 14 
 16 
?isduck(1234)
False
?isduck(0123)
False
?isduck(12034)
True

虽然我确实从这里找到了这个:

或许对你更好:

Dim oddsZeroToFifty() As Integer
oddsZeroToFifty= Enumerable.Range(0, 51).Where(Function(x) x Mod 2 = 1).ToArray