Loops 单元测试-带有for循环和try-catch的字典

Loops 单元测试-带有for循环和try-catch的字典,loops,dictionary,for-loop,Loops,Dictionary,For Loop,我是软件开发领域的新手。有谁能帮我理解如何创建for循环和try catch 问题是: 32有6个因素 创建一个字典,其中32的每个因子都是对应因子的一个键(应该有6个键) 使用For循环遍历从1到32的所有整数值。使用Try-Catch处理字典中没有键的情况。如果找到密钥,则断言该密钥乘以当前对的值为32 这是我的答案- Dim Key1为整数 作为整数的Dim值1 Dim Key2为整数 作为整数的Dim值2 Dim Key3为整数 将数值3设置为整数 Dim Key4为整数 将值4设置为整

我是软件开发领域的新手。有谁能帮我理解如何创建for循环和try catch

问题是: 32有6个因素 创建一个字典,其中32的每个因子都是对应因子的一个键(应该有6个键) 使用For循环遍历从1到32的所有整数值。使用Try-Catch处理字典中没有键的情况。如果找到密钥,则断言该密钥乘以当前对的值为32

这是我的答案- Dim Key1为整数 作为整数的Dim值1 Dim Key2为整数 作为整数的Dim值2 Dim Key3为整数 将数值3设置为整数 Dim Key4为整数 将值4设置为整数 Dim Key5为整数 作为整数的Dim值5 Dim Key6为整数 整数值6

    Dim result As Integer

    Key1 = 1
    Value1 = 32
    Key2 = 4
    Value2 = 8
    Key3 = 2
    Value3 = 16
    Key4 = 32
    Value4 = 1
    Key5 = 8
    Value5 = 4
    Key6 = 16
    Value6 = 2

    myDictionary.Add(Key1, Value1)
    myDictionary.Add(Key2, Value2)
    myDictionary.Add(Key3, Value3)
    myDictionary.Add(Key4, Value4)
    myDictionary.Add(Key5, Value5)
    myDictionary.Add(Key6, Value6)

    For loopCounter As Integer = 1 To 32
    loopCounter = Key1 And Value1

     If loopCounter > Key6 Then
     Assert.Fail("There is no such key in the dictionary")

     Else

      result = Key1 * Value1

      loopCounter = Key1 + 1 And Value1 + 1

       End If

       Try

       result = Key1 * Value1

       loopCounter = Key6 + 1 And Value6 + 1

       Catch

       Debug.Print(String.Format("KeyNotFoundException"))

       End Try

       Next

       Assert.AreEqual(result, 32, "Key times Value is 32")

       End Sub

       End Class
我知道这是错误的,但不知道如何改变它


谢谢。

首先,您不应该将loopcounter设置为任何值。您可以引用loopcounter,但不建议更改索引。回复:
对于整数为1到32的loopCounter,loopCounter=Key1和Value1
感谢您的回答。是否要删除“loopCounter=Key1和Value1”?但是剩下的呢?我知道这是完全错误的,但不知道如何修复它。我也不确定我是否理解你的if声明。为什么16岁以上的人都会失败?基本上,循环迭代的一半将通过assert.fail逻辑。我如何编写“使用For循环循环遍历从1到32的所有整数值。使用Try-Catch处理字典中没有键的问题”?您在这里尝试使用的实际语言是什么?