如何在Visual Basic中检查字符串是否包含重复的符号 [英] How can I check in Visual Basic if string has duplicate symbols

查看:66
本文介绍了如何在Visual Basic中检查字符串是否包含重复的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作Bulls and Cows游戏,我想随机生成一个包含四个不同数字的数字.

I'm making Bulls And Cows game and I want to randomly generate a number with four different digits.

Randomize()
Do
    Random = Int(9000 * Rnd()) + 1000
    'If (hasDupes = True)
    randomTwo = Random
    For i = 0 To randomTwo.Length - 1
        For j = 0 To randomTwo.Length - 1
            If randomTwo(i) = randomTwo(j) Then
                dupes = False
                Exit For
            End If
        Next
    Next
Loop Until dupes = True

^到目前为止,这就是我所没有的.有什么功能可以查找是否有重复的字符或我的错误在哪里?

^ That's what I have so far but it ain't working. Is there any function how I can find if there are duplicate characters or where is my mistake ?

推荐答案

此处是我的实现

Dim sResult As String  
Dim iCnt As Integer  
Dim iTmp As Integer  

iCnt = 0
Do While iCnt < 4
    iTmp = Int(10 * Rnd())
    If InStr(sResult, CStr(iTmp)) = 0 and not (iCnt = 0 and iTmp = 0) Then
        sResult = sResult & CStr(iTmp)
        iCnt = iCnt + 1
    End If
Loop

Debug.Print sResult

阅读RLH注释后,附加检查首位编号必须> 0

Additional check first number must be > 0 after reading RLH comment

这篇关于如何在Visual Basic中检查字符串是否包含重复的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆