2013-11-25 46 views
1

作業任務是簡單地允許用戶輸入值(字符串)「FD__」,並將其與已知輸入列表進行匹配並返回true/false。這個ID已經定義好了,當我輸入一個ID爲的ID爲「FD3」時,它將返回true,如果該值沒有被定義,它不僅不會給出結果,而且會導致程序崩潰,所以在結論真實的作品,但錯誤不。任何信息都可能有幫助。根據數組驗證輸入時的無限循環


設計:http://i.imgur.com/bJnFAMX.png


Public Class Form1 

    'variables 
    Dim Products(9) As String 
    Dim Entered As String 
    Dim Found As Boolean 

    Public Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click 
     'Product variable array elements 
     Products(0) = "FD1" 
     Products(1) = "FD2" 
     Products(2) = "FD3" 
     Products(3) = "FD4" 
     Products(4) = "FD5" 
     Products(5) = "FD6" 
     Products(6) = "FD7" 
     Products(7) = "FD8" 
     Products(8) = "FD9" 
     Products(9) = "FD10" 
     'process 
     Entered = txtFind.Text 'define entered value as variable 
     Found = FindNumber() 'sub function 
     If Found Then 
      lblResult.Text = Found 'change the results 
     End If 
    End Sub 

    Public Function FindNumber() 
     'If true statements 
     Do While (Found = False) 
      If Entered = Products(0) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(1) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(2) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(3) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(4) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(5) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(6) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(7) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(8) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      ElseIf Entered = Products(9) Then 
       Found = True 
       lblResult.ForeColor = Color.LawnGreen 
      Else 'keeps crashing 
       Found = False 
       lblResult.ForeColor = Color.Red 
      End If 
     Loop 
     Return Found 
    End Function 
End Class 
+0

它不會崩潰。 FindNumber()是一個無限循環,因爲'Found'將始終爲假(Do While Found = False)。而不是while while指令,只循環一次Products並返回true或false。 – Jaxedin

回答

0

實際上,你可以用很多更少的代碼...我要繼續前進,做一些批判,而在它做到這一點。

'1. Functions should have return types 
'2. You should pass the function what it needs to do its job 
Public Function FindNumber(numberEntered As String) As Boolean 
    'Rather than evaluating if a condition is true or false, and then returning 
    'true or false, you can just directly return the condition. 
    Return Products.Contains(numberEntered) 
End Function 

然後在您的主程序:

'This doesn't need to be a class level variable 
Dim entered = txtFind.Text 
'Local variables should be lowercase 
Dim found = FindNumber(entered) 
If found Then 
    'With Option Strict you can't assign a Boolean to a String... 
    'but you can do .ToString() instead 
    lblResult.Text = found.ToString() 
    'And this was really dependent on the result of the function... 
    'Also it has nothing to do with finding the number... 
    lblResult.ForeColor = Color.LawnGreen 
Else 
    lblResult.ForeColor = Color.Red 
End If 

它看起來很長,但拿出的意見,我向你保證這將是一個更簡單的(較短)的程序。

0

你可以通過使用。載功能這個簡單的像這樣使用優化這整個代碼:

如果mylist.contains(「SomeText」則會),那麼 「改變前景色爲綠色 其他 」改變前景色爲紅色 end if

這會緩解您的問題。

+0

我已經知道這個選項,問題是實際的任務要求你這樣做。 – Cem