2011-05-06 74 views
1

我有問題,我的代碼問題編程波雷費密碼

Public Class Form1 

Dim lm(4, 4) As Char 
Dim pt As String = "" 
Dim ct As String = "" 
Dim key As String = "" 
Dim n1, n2, n3, n4, m1, m2, m3, m4 As Integer 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    key = k.Text 
    key.Replace(" ", "") 
    pt = plaintext.Text 
    If pt.Length Mod 2 <> 0 Then 
     pt = pt & "e" 
    End If 
    pt = UCase(pt) 
    pt.Replace("J", "I") 
    Dim Value As String 
    Dim NewValue As String 
    key.Replace("J", "I") 
    Value = key & "ABCDEFGHIKLMNOPQRSTUVWXYZ" 
    NewValue = "" 

    Value = UCase(Value) 
    NewValue = Value(0) 
    For i As Integer = 1 To Value.Length - 1 
     If Not (InStr(1, NewValue, Value(i)) > 0) Then 
      NewValue = NewValue & Value(i) 
     End If 
    Next 
    Dim p As Integer = 0 
    For n As Integer = 0 To 4 
     For m As Integer = 0 To 4 
      lm(n, m) = NewValue(p) 
      p = p + 1 
     Next 
    Next 
    Dim leave As Boolean = False 
    For q As Integer = 0 To pt.Length - 1 Step 2 
     For n1 = 0 To 4 
      For m1 = 0 To 4 
       If lm(n1, m1).Equals(pt(q)) Then 
        leave = True 
        Exit For 
       End If 
      Next 
      If leave Then 
       Exit For 
      End If 
     Next 
     leave = False 
     For n2 = 0 To 4 
      For m2 = 0 To 4 
       If lm(n2, m2).Equals(pt(q + 1)) Then 
        leave = True 
        Exit For 
       End If 
      Next 
      If leave Then 
       Exit For 
      End If 
     Next 
     If n1 = n2 Then 
      If m1 >= 4 Then 
       m3 = 0 
      Else 
       m3 = m1 + 1 
      End If 
      If m2 >= 4 Then 
       m4 = 0 
      Else 
       m4 = m2 + 1 
      End If 
     ElseIf m1 = m2 Then 
      If n1 >= 4 Then 
       n3 = 0 
      Else 
       n3 = n1 + 1 
      End If 
      If n2 >= 4 Then 
       n4 = 0 
      Else 
       n4 = n2 + 1 
      End If 
     Else 
      n3 = n1 
      m3 = m2 
      n4 = n2 
      m4 = m1 
     End If 
     ct = ct & lm(n3, m3) & lm(n4, m4) 
    Next 
    ciphertext.Text = ct 
    ct = "" 
End Sub 

End Class 

它在這個線路CT給出錯誤= CT & LM(N3,M3)&流明(N4,M4)陣列超出範圍 我的cuoldn't找到爲什麼有人可以幫助我?

+0

你可以使用調試器來檢查變量嗎?一個是超出範圍? – 2011-05-06 09:08:01

+0

明文:莎拉 鍵:電腦 m4變爲5 – a2m22 2011-05-06 09:20:13

回答

1

啊,看準:)

Dim leave As Boolean = False 
For q As Integer = 0 To pt.Length - 1 Step 2 
    For n1 = 0 To 4 

你的第一次迭代之前將其設置爲false,並在循環中只(Q之前+ 1)

leave = False 
    For n2 = 0 To 4 

需要重啓leave = False每當你循環:

Dim leave As Boolean 
For q As Integer = 0 To pt.Length - 1 Step 2 
leave = False ' This was missing 
    For n1 = 0 To 4 
+0

你是對的,謝謝:) – a2m22 2011-05-06 10:56:20

0

對於較短的代碼,請嘗試:

(a,b,f,g,c)="".join(input("CODE: ").split()),input("Polybius S: "),"","",1 
for(i)in(a): 
    if(c%2)==0: 
     g+=i 
    else: 
     f+=i 
    c+=1 
for(j)in(range(0,len(f))): 
    if(b.find(f[j])%5)!=(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))!=(int(((b.find(g[j]))-(b.find(g[j])%5))/5)): 
     print(b[((int(((b.find(f[j]))-(b.find(f[j])%5))/5))*5)+(b.find(g[j])%5)],end="") 
     print(b[((int(((b.find(g[j]))-(b.find(g[j])%5))/5))*5)+(b.find(f[j])%5)],end="") 
    elif(b.find(f[j])%5)==(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))!=(int(((b.find(g[j]))-(b.find(g[j])%5))/5)): 
     print(b[((((int(((b.find(f[j]))-(b.find(f[j])%5))/5))-1)%5)*5)+b.find(f[j])%5],end="") 
     print(b[((((int(((b.find(g[j]))-(b.find(g[j])%5))/5))-1)%5)*5)+b.find(g[j])%5],end="") 
    elif(b.find(f[j])%5)!=(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))==(int(((b.find(g[j]))-(b.find(g[j])%5))/5)): 
     print(b[((int(((b.find(f[j]))-(b.find(f[j])%5))/5))*5)+((b.find(f[j])%5)-1)%5],end="") 
     print(b[((int(((b.find(g[j]))-(b.find(g[j])%5))/5))*5)+((b.find(g[j])%5)-1)%5],end="") 

好的也許不是那麼短。或者簡單