循环一个字符串以查找奇数VB

时间:2017-05-03 16:40:12

标签: vba loops

我正在循环查找所有奇数的卡号并将它们乘以卡号。它有点难以解释。我无法将奇数和卡号相乘。这是我老师给我的一个例子。您将卡号1和奇数1相乘,依此类推。我没有得到任何错误,它只是冻结。先感谢您。                                                                     资金
卡号:1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
倍数1 2 3 4 5 6 7 8
Evens:2 4 6 8 0 2 4 6 32 =总和1
赔率:1 6 15 28 45 6 21 40 162 =总和2

总和3:194 194 =总和3
步骤4:= 1 + 9 + 4 = 14
    = 1 + 4 = 5 =校验位

Public Class Payment
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
    Dim Sum1 = 0
    Dim Sum2 = 0
    Dim Sum3 = 0
    Dim ready As Boolean
    Dim ccnumb = CardNumber.Text
    Format(CardNumber.Text, "################")
    Dim exp = Mid(ExpDate.Text, 1, 3)
    Dim checkdigit = 0
    If FullName.TextLength = 0 Or cardtype.Text.Length = 0 And ccnumb.Length <= 16 Or exp.Length = 2 Then
        MessageBox.Show("Please enter all credit card information before proceeding.")
        ready = False
    Else ready = True
    End If
    If ready = True Then
        For Each num As Char In ccnumb
            If CInt(CStr(num)) Mod 2 <> 0 Then
                Sum1 += CInt(CStr(num)) * CInt(CStr(num)) Mod 2 <> 0
            Else
                Sum2 += CInt(CStr(num))
            End If
        Next

        Sum3 = Sum1 + Sum2
        Do While Sum3 > 10
            For j = 0 To Sum3.ToString.Length - 1
                For k = 1 To Sum3.ToString.Length - 1
                    Sum3 = j + k
                Next
            Next
        Loop
        Do While exp.Length > 1
            checkdigit = Mid(ExpDate.Text, 1, 1) + Mid(ExpDate.Text, 1, 2)
        Loop
        If Sum3 = checkdigit Then
            MessageBox.Show("Congratulations! Your payment was successful.")
            CustInv.Show()
        Else MessageBox.Show("The checkdigit," & Space(1) & Sum3 & Space(1) & "does not match the month code," & Space(1) & checkdigit & "." & Space(1) & "Please reenter your card information.")
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

&#34;它有点冻结&#34;我的代码是infinite loop&#34;。

这看起来很可疑:

 private void ExportGridView(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        //To Export all pages
        GridView1.AllowPaging = false;
        //GridView1.DataBind();
        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}

要进入循环,Do While Sum3 > 10 For j = 0 To Sum3.ToString.Length - 1 For k = 1 To Sum3.ToString.Length - 1 Sum3 = j + k Next Next Loop 必须大于10.要退出循环,Sum3不得大于10,但您的代码只能递增 { {1}},因此Sum3只能保持大于10。

这意味着一旦进入,这个循环就是无限的。

答案 1 :(得分:0)

@Bohemian提到你的循环需要编辑。以下代码可以帮助您:

For d = 1 To Len(cstr(YourNumber))
        SumDigits = SumDigits + Val(Mid(cstr(YourNumber), d, 1))
Next d