作业帮 > 综合 > 作业

VB随机产生10个不重复的大写英文字母,看看我哪边错了

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/10 14:57:04
VB随机产生10个不重复的大写英文字母,看看我哪边错了
Private Sub Command1_Click()
Dim a(1 To 10)
Dim flag As Boolean, i%, j%
Randomize
For i = 1 To 10
   Do
        a(i) = Int(26 * Rnd) + 65
        flag = False
        For j = 1 To i - 1
            If a(i) = a(j) Then
                flag = True
                Exit For
            End If
        Next j
    Loop While flg = True
Next i
Print "随机产生10个不重复的大写英文字母:"
For i = 1 To 10
    Print Chr(a(i)),
Next i
End Sub
VB随机产生10个不重复的大写英文字母,看看我哪边错了
'试一下:
Private Sub Command1_Click()
Dim a(1 To 10) As String
Dim i As Integer,j As Integer
For i = 1 To 10
Randomize
a(i) = Chr(Int(26 * Rnd) + 65)
If i > 1 Then
For j = 1 To i - 1
If a(i) = a(j) Then Exit For
Next
End If
If j < i And i > 1 Then i = i - 1
Next
MsgBox Join(a,",")
End Sub
再问: 你这样的确可以,我想问为什么我那个错了。而且我的显示方法和你的不一
再答: '给你修改一下,最好的方法是用字典,这个效率太低,如果上万很容易假死机 Private Sub Command1_Click() Dim a(1 To 10) Dim flag As Boolean, i%, j% Randomize For i = 1 To 10 flag = False Do Until flag a(i) = Int(26 * Rnd) + 65 If i = 1 Then Exit Do For j = 1 To i - 1 If a(i) = a(j) Then Exit For Next If j = i Then flag = True Loop Next Print "随机产生10个不重复的大写英文字母:" For i = 1 To 10 Print Chr(a(i)); Next Print End Sub '--------------------------------- '给你用字典写,这方法最好: Private Sub Command1_Click() Dim s As String, n As Long With CreateObject("scripting.dictionary") Do Until n = 10 Randomize s = Chr(Int(26 * Rnd) + 65) If Not .Exists(s) Then .Add s, "" n = n + 1 End If DoEvents Loop MsgBox Join(.keys, ",") End With End Sub