Visual Basic创建循环以计算偶数

时间:2018-09-13 00:27:04

标签: basic

程序需要输入正数,并使用循环函数对每个偶数进行计数,如果输入O,则该循环函数不计算奇数和结尾。

我不确定如何创建循环或不确定是否可以在循环中使用if函数。

Dim Count = 0
While (number mod 2 = 0) do
Count + 1 = Count

5 个答案:

答案 0 :(得分:0)

我实际上不是很理解这个问题,但是就您所希望的奇数而言,我建议在count上加2而不是一个,因为count变量以零开头:

/// Places Our Model At The Position Of An Existining ARPlaneAnchor
///
/// - Parameter gesture: UITapGestureRecognizer
@IBAction func placeModel(_ gesture: UITapGestureRecognizer){

    //1. Get The Touch Location
    let touchLocation = gesture.location(in: self.augmentedRealityView)

    //2. Perform An ARSCNHitTest For Any Existing Planes
    guard let result = self.augmentedRealityView.hitTest(touchLocation, types: [.existingPlane, .existingPlaneUsingExtent]).first else { return }

        //3. Get The World Transform
        let hitTestTransform = SCNMatrix4(result.worldTransform)

        //4. Initialize Our Position Either From .m41, .m42, .m43 Or From Columns.3
        let positionFromMatrix4 = SCNVector3(hitTestTransform.m41, hitTestTransform.m42, hitTestTransform.m43)
        let positionFromColumns =  SCNVector3(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)

        //5. Log Them To Check I'm Not Being A Moron
        print(
            """
            Position From Matrix 4 == \(positionFromMatrix4)
            Position From Columns == \(positionFromColumns)
            """)

        /*
        Position From Matrix 4 == SCNVector3(x: -0.39050543, y: -0.004766479, z: 0.08107365)
        Position From Columns == SCNVector3(x: -0.39050543, y: -0.004766479, z: 0.08107365)
        */

        //6. Add A Node At The Position & Add It To The Hierachy
        let boxNode = SCNNode(geometry: SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0))
        boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.cyan
        boxNode.position = positionFromMatrix4
        self.augmentedRealityView.scene.rootNode.addChildNode(boxNode)
}

顺便说一句,您希望什么时候停止计数?在2,然后回到0?  如果是这样,请使用if语句

Dim Count+2

答案 1 :(得分:0)

假设您正在从某些输入中获取数字,这就是您可以执行的操作。与While True进行无限循环,然后对于输入中给出的每个数字,使用number mod 2 = 0检查其是否为偶数。这将永远持续下去,因此您需要添加一些条件(另一个if语句)以使其停止while循环。有关while循环的更多信息,请点击此处:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement

Dim Count = 0
While True do
  
  If (number mod 2 = 0) Then
    Count + 1 = Count
  End If 
End While  

答案 2 :(得分:0)

如果您提供示例输入,这将有所帮助,以便我们可以使用实际代码并指导您找到正确的解决方案。

如果您以数字数组的形式接收输入,则可以简单地使用forforeach循环输入,并添加其他条件以检查是否要退出,以检查是否为0:

For Each number As Integer In numbers
    If (number mod 2 = 0) Then
       Count = Count + 1
    End If 

    If (number = 0) Then
       Exit For
    End If
Next

如果您已有现有的代码,其中number已经在每次迭代中重新初始化/重新定义,那么您所拥有的与您所需要的非常接近:

While (number <> 0)
    If (number mod 2 = 0) Then
       Count = Count + 1
    End If
End While

答案 3 :(得分:0)

函数计算偶数:

REM function gets input, exits at 0 and adds positive even numbers.
DO
    INPUT X
    IF X = 0 THEN PRINT Y; " even numbers": END
    IF X > 0 THEN
        IF X / 2 = X \ 2 THEN Y = Y + 1
    END IF
LOOP

答案 4 :(得分:0)

我对此完全不确定。只是一个猜测...

Dim j as Integer

Dim i As integer

j = 0

i = 2

For i = 1 to 100

j = j+i

Print j

Loop

End Sub