VB的新手,尝试在VB中进行一些计算编码,但它似乎不起作用?
我已将整数存储到'Access'。
Dim max_number As Integer = 63
If Access > max_number Then
Access = max_number
End If
While Access > max_number Or Access >= 0
If Access - 32 >= 0 Then
Access = Access - 32
Text1.Text = "1"
ElseIf Access - 16 >= 0 Then
Access = Access - 16
Text2.Text = " 1"
ElseIf Access - 8 >= 0 Then
Access = Access - 8
Text3.Text = "1"
ElseIf Access - 4 >= 0 Then
Access = Access - 4
Text4.Text = "1"
ElseIf Access - 2 >= 0 Then
Access = Access - 2
Text5.Text = "1"
ElseIf Access - 1 >= 0 Then
Access = Access - 1
Text6.Text = " 1"
End If
Access = 0
End While
非常感谢。
答案 0 :(得分:1)
在循环中删除“Access = 0”并将循环更改为并且您不需要测试max_number,因为它已经在之前完成。
While Access > 0
如果你试图在纸上运行它,你会注意到如果Access等于2.它将进入你的if(做Access = Access - 2)并且Access将等于0,你将是在一个有趣的循环中。
但你不需要一个while循环。
Dim max_number As Integer = 63
If Access > max_number Then
Access = max_number
End If
If Access > 0 Then
If (Access And 32) > 0 Then Text1.Text = "1"
If (Access And 18) > 0 Then Text2.Text = "1"
If (Access And 8) > 0 Then Text3.Text = "1"
If (Access And 4) > 0 Then Text4.Text = "1"
If (Access And 2) > 0 Then Text5.Text = "1"
If (Access And 1) > 0 Then Text6.Text = "1"
End If
答案 1 :(得分:0)
你陷入无限循环,因为你在循环结束时将Access
设置为零。
另外,我不确切地知道你要做什么,但你可能想要查看Mod
运算符,因为它可能会减少循环所需的迭代次数。
无论如何,要打破无限循环,试试这个:
Dim max_number As Integer = 63
If Access > max_number Then
Access = max_number
End If
While Access > max_number Or Access >= 0
If Access - 32 >= 0 Then
Access = Access - 32
Text1.Text = "1"
ElseIf Access - 16 >= 0 Then
Access = Access - 16
Text2.Text = " 1"
ElseIf Access - 8 >= 0 Then
Access = Access - 8
Text3.Text = "1"
ElseIf Access - 4 >= 0 Then
Access = Access - 4
Text4.Text = "1"
ElseIf Access - 2 >= 0 Then
Access = Access - 2
Text5.Text = "1"
ElseIf Access - 1 >= 0 Then
Access = Access - 1
Text6.Text = " 1"
Else
Exit While
End If
End While