【问题标题】:If statement does not react如果语句没有反应
【发布时间】:2018-07-12 20:46:30
【问题描述】:

我正在做一个学校项目。我要做一个德国Enigma机器的模拟。

我希望数组是“圆形”的,如果索引超出数组长度,它应该从数组开始重新开始。

我不明白以下内容应该如何工作。如果wh0pos 大于数组长度,即 10,则应减去 10。

If (wh0pos > wheel1.Length) Then ' Hvis 
    wh0pos = wh0pos - wheel1.Length
End If
If (0 > wh0pos) Then
    wh0pos = wh0pos + wheel1.Length
End If

wh1 = wh0 + wheel1(wh0pos) ' Bogstav-værdi + værdiens position i rotor 1

我们的老师不知道可能出了什么问题,所以我希望这里的人能帮助我。完整代码如下。

Public Class Form1

    Dim wheel1(0 To 25), wheel2(0 To 25), wheel3(0 To 25) ' Her definerer jeg de tre rotorer
    Dim whPos1, whPos2, whPos3 As Integer ' Her definerer jeg de tre rotorers position

    Dim wh0 = 0
    Dim wh1 = 0
    Dim wh2 = 0

    Dim charGoBack As Integer

    Dim wh0pos As Integer
    Dim wh1pos As Integer

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        wheel1 = {3, 2, 1, -2, -3, 3, 2, 1, -2, -3} ' Første array (indeholder forskydningen i tal)
        wheel2 = {+1, -1, +2, +1, -3, 3, 2, 1, -2, -3} ' Andet array

        whPos1 = 5 ' Sætter rotor1 position til 0
        whPos2 = 0 ' Sætter rotor2 position til 0
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        whPos1 = whPos1 + 1 ' Hver klik, flyttes rotor-position 1 gang

        If (whPos1 = 25) Then ' Hvis rotor1 er nået sidste bogstav
            whPos1 = 0 ' Sæt rotor1 til 0
            whPos2 = whPos2 + 1 ' Flyt rotor2 én gang
        End If

        wh0pos = wh0 + whPos1 ' Tager process1 og adderer rotorflytningen

        wh0 = (Asc(TextBox1.Text) - 65) ' Konverterer bogstav til ASCII-værdi



        If (wh0pos > wheel1.Length) Then ' Hvis 
            wh0pos = wh0pos - wheel1.Length
        End If
        If (0 > wh0pos) Then
            wh0pos = wh0pos + wheel1.Length
        End If

        wh1 = wh0 + wheel1(wh0pos) ' Bogstav-værdi + værdiens position i rotor 1

        wh1pos = wh1 + whPos2

        If (wh1pos > (wheel2.Length - 1)) Then ' Hvis 
            wh1pos = wh1pos - wheel2.Length
        End If
        If (wh1pos < 0) Then
            wh1pos = wh1pos + wheel2.Length
        End If
        wh2 = wh1 + wheel2(wh1pos) ' Udkom af rotor1 + værdiens position i rotor 2


        Label1.Text = wh0
        Label2.Text = wh1
        Label3.Text = wh2
        charGoBack = (wh2) + 65 ' Tager den krypterede ASCII-værdi og gør klar til at omkonvertere til bogstav igen

        If (charGoBack > (65 + 26)) Then ' Hvis charGoBack er større end ASCII-intervallet for bogstaver
            charGoBack = charGoBack - 65 ' Subtraher resultat med 65
        End If

        If (charGoBack < 0) Then ' Hvis 0 er større end charGoBack
            charGoBack = charGoBack + 65 ' Adder det med 65, så vi kommer op i ASCII-bogstavrækken
        End If



        Label4.Text = Chr(charGoBack) ' Printer krypterede tekst
    End Sub
End Class

【问题讨论】:

  • 我没有详细介绍,但是您尝试一步一步执行吗?它是否完全进入您的 if 语句?此外,我认为您可以完全摆脱 if ,只需使用 Mod: wh0pos = ((wh0pos - 1) Mod wheel1.Length) + 1
  • 我不确定它是否进入了 if 语句,没有。 wh0pos 最终会变得大于 10,然后程序崩溃,说它找不到数组的索引 10。会看Mod
  • 保护wh0pos 的代码并不完全等于wh1pos 的保护代码。也许将其拆分为一个函数? (输入whpos和轮长,输出新的whpos)
  • 所以错误不在wh0pos上,而是在wh1pos上?

标签: arrays vb.net visual-studio


【解决方案1】:

您在 cmets 中提到它在索引等于 10 时崩溃。原因可能是在您的数组中,最后一个元素的索引是 9,但数组的长度是 10(从 0 开始计数)。这可能是它崩溃的原因,即程序试图访问 a[10] 但它超出了范围。

【讨论】:

  • 我也试过If (wh0pos &gt; (wheel1.Length - 1)) Then ' Hvis wh0pos = wh0pos - wheel1.Length End If。它也没有用。无论如何,程序都会尝试索引数组位置 10。
【解决方案2】:

我非常愚蠢,我已经找到了崩溃的原因。

数组没有26个位置,程序试图找到一个没有位置的字母。

换句话说,如果程序要输出 T,它的程序值是 19。if 语句说如果它高于 9,则应减去 9。现在是 19,结果为 10 .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    相关资源
    最近更新 更多