为什么会导致无限循环错误? [英] Why it cause infinity cycle error?
本文介绍了为什么会导致无限循环错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图制作东西,产生随机数,不包括previos(1-10)
im trying to make thing,that generates random number, not includes previos(1-10)
Public Class Form1
Dim NewTicketNumber As Integer
Dim IsTicketDone1 As Boolean = False
Dim IsTicketDone2 As Boolean = False
Dim IsTicketDone3 As Boolean = False
Dim IsTicketDone4 As Boolean = False
Dim IsTicketDone5 As Boolean = False
Dim IsTicketDone6 As Boolean = False
Dim IsTicketDone7 As Boolean = False
Dim IsTicketDone8 As Boolean = False
Dim IsTicketDone9 As Boolean = False
Dim IsTicketDone10 As Boolean = False
Private Sub Make()
NewTicketNumber = GetRandom(1, 10)
If NewTicketNumber = 1 Then
If IsTicketDone1 = True Then
Make()
Else
IsTicketDone1 = True
End If
ElseIf NewTicketNumber = 2 Then
If IsTicketDone2 = True Then
Make()
Else
IsTicketDone2 = True
End If
ElseIf NewTicketNumber = 3 Then
If IsTicketDone3 = True Then
Make()
Else
IsTicketDone3 = True
End If
ElseIf NewTicketNumber = 4 Then
If IsTicketDone4 = True Then
Make()
Else
IsTicketDone4 = True
End If
ElseIf NewTicketNumber = 5 Then
If IsTicketDone5 = True Then
Make()
Else
IsTicketDone5 = True
End If
ElseIf NewTicketNumber = 6 Then
If IsTicketDone6 = True Then
Make()
Else
IsTicketDone6 = True
End If
ElseIf NewTicketNumber Then
If IsTicketDone7 = True Then
Make()
Else
IsTicketDone7 = True
End If
ElseIf NewTicketNumber Then
If IsTicketDone8 = True Then
Make()
Else
IsTicketDone8 = True
End If
ElseIf NewTicketNumber Then
If IsTicketDone9 = True Then
Make()
Else
IsTicketDone9 = True
End If
ElseIf NewTicketNumber Then
If IsTicketDone10 = True Then
Make()
Else
IsTicketDone10 = True
End If
End If
Label1.Text = NewTicketNumber
If IsTicketDone1 = True And IsTicketDone2 = True And IsTicketDone3 = True And IsTicketDone4 = True And IsTicketDone5 = True And IsTicketDone6 = True And IsTicketDone7 = True And IsTicketDone8 = True And IsTicketDone9 = True And IsTicketDone10 = True Then
MsgBox("All Done!")
End If
End Sub
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Dim Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Make()
End Sub
End Class
推荐答案
不幸的是,这段代码不值得调试,因为这不是软件开发人员编写代码的方式。软件开发人员不会像10个布尔变量那样多次重复相同的声明。软件开发人员会声明一个数组或使用一些集合。一个软件开发人员永远不会写如果某事物=真然那么......因为某事物已经是布尔值,所以它应该是如果那么......;并且软件开发人员永远不会尝试编写那么长的if块并且将使用循环。
最重要的是,软件开发人员不会等到任何人教会如何要做到这一点,但会从纯逻辑和第一原则中得出正确的代码。
我不知道在这种情况下如何有效地帮助。如果我修复了这个bug,它就不会真的有用。
-SA
Unfortunately, this code does not deserve debugging, because this is not how a software developer writes code. A software developer would never repeat identical declaration several times like your 10 Boolean variables. A software developer would declare an array or use some collection. A software developer would never write "if something = True then …" because "something" is already Boolean, so it should be "if something then …"; and a software developer will never try to write that long "if" block and will use a loop.
Most importantly, a software developer would not wait until anyone teaches how to do it but would derive right code from pure logic and first principles.
I don''t know how to effectively help in this case. If I fix the bug, it won''t really help.
—SA
首先,是的,错误的编程...
然而,到了这一点,一个随机数就是这个,随机的。我们无法保证为您提供任何价值,只能提供该范围内的价值。所以,假设你运行它并且它永远不会随机给你2的值,你的代码将继续循环直到它获得该值。也许它会给它,也许它不会,但是它会让它变得有趣并且难以找到bug,正如你所发现的那样。
*另外*
在NewTicketNumber = 6之后你停止给出相等检查(所以7,8,9和10不检查值)。最重要的是,在将标志设置为true之前再次调用Make(),这意味着在某些时候您将获得堆栈溢出异常。首先设置标志,然后再次调用Make()。
First, yes, bad programming...
However getting to the point, a random number is just that, random. It is not guaranteed to give you any value, only a value within that range. So lets say you run this and it NEVER randomly gives you the value of 2, your code will continue to loop forever until it gets that value. Maybe it will give it, maybe it won''t, but leaving it up to chance that it will makes for interesting and hard to find bugs, as you are finding out.
* ALSO *
You stopped giving equality checks after NewTicketNumber = 6 (so 7,8,9, and 10 do not check for a value). On top of that you are calling Make() again before setting the flag to true, which means that at some point you will get stack overflow exceptions. Set the flag first, then call Make() again.
这篇关于为什么会导致无限循环错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文