二进制加法 VB.NET

Binary Addition VB.NET

我想就 Visual Basic 2010 中的 二进制加法 寻求一些帮助。user2471711 创建了一个控制台应用程序并且运行良好,最终,我想要这个在 Windows 表格申请中。你们知道我如何在 WinForms 应用程序中做到这一点吗?

Dim a As Integer = 8
Dim b As Integer = 2
Dim c As Integer = 10

Sub Main()
    Dim binary1 As Integer
    Dim binary2 As Integer
    Console.WriteLine("-------BINARY ADDITION--------")
    Console.WriteLine("Enter in your two binary numbers")
    Console.WriteLine("1st number")
    binary1 = Console.ReadLine
    Console.WriteLine("2nd number")
    binary2 = Console.ReadLine
    Console.WriteLine(binary1 & "+" & binary2 & "=")
    binary1 = binary1 + binary2
    Console.Write(add(binary1))
    Console.ReadLine()
End Sub

Function add(ByVal x As Integer)
    For y As Integer = 1 To 8
        If x Mod c >= b Then
            x = x + a
        End If
        a = a * 10
        b = b * 10
        c = c * 10
    Next

    Return x
End Function

在您的 Winforms 上放置两个文本框、一个标签和一个按钮。 这段代码会做你想做的,把它放在按钮的“点击”事件中。

        Dim BinaryResult As Integer = Convert.ToInt32(TextBox1.Text, 2) + Convert.ToInt32(TextBox2.Text, 2)
    Label1.Text = BinaryResult.ToString

这将对文本框中输入的值求和,并在您单击按钮时在标签中显示结果。

* 附加格式 * ('binary' 'normal' 'hex')

        Label1.Text = String.Format("Base-2:{0}    Base-10:{1}    Base-16:{2}",
                                Convert.ToString(BinaryResult, 2),
                                Convert.ToString(BinaryResult, 10),
                                Convert.ToString(BinaryResult, 16).ToUpper)

但是...请注意,这里没有检查。您应该检查文本框包含您期望的内容,但这应该让您开始。