2015-07-28 84 views
1

我的代碼新手和即時通訊使用此代碼。看起來,我總是從這個函數得到真實的響應。我究竟做錯了什麼 ?Visual Basic - 函數來檢查數字是否是二進制的

Private Function binary() As Boolean 
    Dim number, temp As Integer 
    Dim status As Boolean 
    TextBox1.Text = number 
    status = True 

    While (True) 
     If (number = 0) Then 
      Exit While 
     Else 
      temp = number Mod 10 
      If (temp > 1) Then 
       status = False 
       Exit While 
      End If 
      number = number/10 
     End If 
    End While 

    Return status 

End Function 

回答

2

你有你的分配方式不對周圍:

TextBox1.Text = number 

有了這個,number將始終爲0,其初始值,所以你While循環立即退出,每一次。它應該是:

number = Convert.ToInt32(TextBox1.Text) 

或者更好的是,在把它作爲參數傳遞給函數:

Private Function binary(number as Integer) As Boolean 
    Dim temp As Integer 
    Dim status As Boolean 

    status = True 

    While (True) 
     If (number = 0) Then 
      Exit While 
     Else 
      temp = number Mod 10 
      If (temp > 1) Then 
       status = False 
       Exit While 
      End If 
      number = number/10 
     End If 
    End While 

    Return status 
End Function 

然後:

Dim isBinary as Boolean 
isBinary = binary(Convert.ToInt32(TextBox1.Text)) 
+0

我知道,我不是想去做這個(但因爲我不能做任何其他)我必須給你一些信用,所以**非常感謝你詹姆斯**。 – grunf00

相關問題