vb隨機英文字母

2009-11-09 7:50 pm
想要隨機出到英文字母
每5位英文字母一行
到listBox1
要40行就停
點樣寫
更新1:

thx,我唔記得左把字母串成一個字串 temp = temp + ConvertToLetter( b )

更新2:

請問怎樣 for loop?

更新3:

佢話ConvertToLetter未宣告x.x 我用的是vb 2008 express edition

更新4:

Dim c As Integer Dim d As Integer Dim f As String Dim a As Random a = New Random Dim temp As String = "" Dim b As Integer = a.Next(1, 26) Do Until (d = 40)

更新5:

Do Until (c = 5) Select Case b Case 1 f = "a" Case 2 f = "b" .... End Select Loop temp = temp + ConvertToLetter(e)

更新6:

ListBox1.Items.Add(temp) Loop End Sub 有什麼問題?

更新7:

是temp = temp + ConvertToLetter(f)

更新8:

sorry,用解釋. 請給我完整的vb碼就可以了

回答 (2)

2009-11-09 9:12 pm
✔ 最佳答案
'建立一個 Random 的變數及 New 左佢
Dim a as random
a = new random

'用兩個 For Loop,第一個 1 ~ 40,第二個 1 ~ 5 ( Loop 中 Loop )
'在第一個 Loop 建立一個 String
Dim temp as String = ""

'在第二個 Loop 隨機抽出數字( 1 ~ 26 ),用一個 integer 儲住
Dim b as integer = a.next(1,26)

'自己寫一個 Function as Char,ByVal as integer
'Pass 個 Random 值入個 Function,根據個值 return 個字母
' 你可以用 IF ELSE 去做,懶的話可以預先用另一個 ListBox 儲住 26 個字母
' 直接 return ListBox2.Items.Item( RandomIndex )

'把字母串成一個字串
temp = temp + ConvertToLetter( b )

'當離開第二個 Loop 後,把 String Add 入 ListBox
ListBox1.Items.Add( temp )


2009-11-09 22:54:23 補充:
// Function 未宣告的原因是你沒有 Define 該 Function
Function LetterConvert(ByVal index As Integer) As Char

 Return ListBox2.Items.Item(index-1)
End Function

// 建立變數
Dim a as Random
a = new Random
Dim b as Integer = 0

2009-11-09 22:55:14 補充:
// For loop
For index As Integer = 1 To 40
 Dim s As String = ""

 For index1 As Integer = 1 To 5
b = a.Next(1, 26)
s = s + LetterConvert( b )
Next
ListBox2.Items.Add(s)
Next

2009-11-09 23:18:56 補充:
給你解釋是想你有一個概念自己想想如何去做
只給你編碼,你不試不想不學,是沒有進步的
阿 Tou 的方法是可以的
但比較勤力,作為一個程式員,應該是「懶得就懶」

用另一個 ListBox,在 Properties 裡的 Items 會見到有 ( Collection ) 的字
選擇後右邊有個「...」的按鈕,按下後會開啟 String Collection Editor
順序打上 A-Z,一個英文字母一行,即:
A
B
C
D
...

2009-11-09 23:23:07 補充:
提一提....
如果在 Loop 中 NEW Object 的話
只會令程式愈來愈食 Ram,嚴格來說,這已經算是一個惡意程式
千萬不要試

2009-11-09 23:29:31 補充:
仲有,那個用來儲存字母的 ListBox
在 Properties 中找出 Visble,然後 Set 做 false
這樣在程式運行時,它就會隱形
2009-11-09 11:20 pm
大概喺咁, 你試試吧

DIM RANDOM_INTEGER AS INTEGER '隨機變數
DIM RANDOM_CHARACTER AS STRING '隨機字符串

FOR LOOP_ID = 1 TO 40 * 5 '40行, 每行5個字符
RANDOM_INTEGER = NEW RANDOM '獲取隨機變數
'隨機變數 = 隨機變數 + 隨機獲取的字母
RANDOM_CHARACTER = RANDOM_CHARACTER + CHARACTER_STRING(RANDOM_INTEGER)
IF LEN(TRIM(RANDOM_CHARACTER)) = 5 THEN '如果長度超過5個字母
LISTBOX.ITEMS.ADD(RANDOM_CHARACTER) '添加入list box
RANDOM_CHARACTER = "" '清空隨機字符串
ENDIF
NEXT LOOP_ID

FUNCTION CHARACTER_STRING(RANDOM_INTEGER AS INTEGER)
SELECT CASE RANDOM_INTEGER MOD 26
CASE 1
CHARACTER_STRING = "A"
CASE 2
CHARACTER_STRING = "B"
CASE 3
CHARACTER_STRING = "C"
CASE 4
CHARACTER_STRING = "D"
CASE 5
CHARACTER_STRING = "E"
CASE 6
CHARACTER_STRING = "F"
CASE 7
CHARACTER_STRING = "G"
CASE 8
CHARACTER_STRING = "H"
CASE 9
CHARACTER_STRING = "I"
CASE 10
CHARACTER_STRING = "J"
CASE 11
CHARACTER_STRING = "K"
CASE 12
CHARACTER_STRING = "L"
CASE 13
CHARACTER_STRING = "M"
CASE 14
CHARACTER_STRING = "N"
CASE 15
CHARACTER_STRING = "O"
CASE 16
CHARACTER_STRING = "P"
CASE 17
CHARACTER_STRING = "Q"
CASE 18
CHARACTER_STRING = "R"
CASE 19
CHARACTER_STRING = "S"
CASE 20
CHARACTER_STRING = "T"
CASE 21
CHARACTER_STRING = "U"
CASE 22
CHARACTER_STRING = "V"
CASE 23
CHARACTER_STRING = "W"
CASE 24
CHARACTER_STRING = "X"
CASE 25
CHARACTER_STRING = "Y"
CASE 0
CHARACTER_STRING = "Z"
END SELECT
RETURN CHARACTER_STRING
END FUNCTION


收錄日期: 2021-04-24 10:50:03
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20091109000051KK00426

檢視 Wayback Machine 備份