Imports System
Public Class Form1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n, m, o, p, q, r As Integer
Dim r1, s1, t1, t2, t3, t4 As String
Dim sb As New System.Text.StringBuilder() '用 StringBuilder 来构建显示字符串
For n = 0 To 20
For m = 0 To 20
If m <> 0 Then '判断 m 是否为 0,避免除零错误
o = n + m
p = n - m
q = n * m
r = n / m
'用函数将整数转换为基数为 2 的字符串
r1 = IntToBaseString(n, 2)
s1 = IntToBaseString(m, 2)
t1 = IntToBaseString(o, 2)
t2 = IntToBaseString(p, 2)
t3 = IntToBaseString(q, 2)
t4 = IntToBaseString(r, 2)
'构建显示字符串
sb.AppendLine(r1 & " + " & s1 & " = " & t1)
sb.AppendLine(r1 & " - " & s1 & " = " & t2)
sb.AppendLine(r1 & " * " & s1 & " = " & t3)
sb.AppendLine(r1 & " / " & s1 & " = " & t4)
End If
Next
Next
TextBox1.Text = sb.ToString() '次性将构建好的字符串赋值给 TextBox1
End Sub
Private Function IntToBaseString(ByVal num As Integer, ByVal base As Integer) As String
Dim buffer As New System.Text.StringBuilder(33)
Dim i As Integer = 0
Dim isNegative As Boolean = False
'处理负数
If num < 0 Then
isNegative = True
num = -num
End If
'转换为字符串
Do
Dim digit As Integer = num Mod base
If digit < 10 Then
buffer.Insert(0, ChrW(48 + digit))
Else
buffer.Insert(0, ChrW(65 + digit - 10))
End If
num = num \ base
Loop While num > 0
'如果是负数,添加负号
If isNegative Then
buffer.Insert(0, "-")
End If
Return buffer.ToString()
End Function
End Class