给你一个最简单的冒泡排序代码:
在商南等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、成都网站建设 网站设计制作按需策划设计,公司网站建设,企业网站建设,品牌网站设计,成都营销网站建设,外贸网站建设,商南网站建设费用合理。
将三个数放到一个数组中。
a(0)=val(text1.text):a(1)=val(text2.text):a(2)=val(text3.text)
dim flag as Boolean,temp as Integer
for i = 0 to 2
flag = true
for j = 2 to 1
if a(j)a(j-1) then
temp = a(j-1)
a(j-1) = a(j)
a(j) = temp
flag = false
end if
next j
if flag then Exit For
next i
text4.text=a(0):text5.text=a(1):text6.text=a(2)
Option Explicit
Private Sub Command1_Click() '排序
Dim i%, j%, S() As String, t As String
ReDim S(Len(Trim(Text1)))
For i = LBound(S) To UBound(S)
S(i) = Mid(Trim(Text1), i + 1, 1)
Next
For i = 0 To UBound(S)
For j = i + 1 To UBound(S)
If S(j) S(i) Then t = S(i): S(i) = S(j): S(j) = t
Next
Text2 = Text2 S(i)
Text2 = Trim(Text2)
Next
End Sub
Private Sub Command2_Click() '读入
Dim Txt As String
Open App.Path "\test12.txt" For Input As #11
Do While Not EOF(11)
Line Input #11, Txt
Text1 = Text1 Txt
Loop
Close #11
End Sub
Private Sub Command3_Click() '追加
Open App.Path "\test12.txt" For Append As #12
Print #12, Text2
Close #12
End Sub
第一题:
不需要任何控件,代码如下:
Private Sub Form_Click()
Dim A() As Integer, N As Integer
Dim St As String, I As Integer, J As Integer
Randomize
Do
St = InputBox("数字的个数", "输入", Int(Rnd * 100))
If St = "" Then
MsgBox "请输入数字!"
Else
N = Int(Val(St))
If N 1 Then
MsgBox "请输入大于0的数字!"
Else
Exit Do
End If
End If
Loop
ReDim A(N)
For I = 1 To N
Do
St = InputBox("第" + Str(I) + "个数字", "输入", Int(Rnd * 100))
If St = "" Then
MsgBox "请输入数字!"
Else
A(I) = Int(Val(St))
Exit Do
End If
Loop
Next
For I = 1 To N - 1
For J = I + 1 To N
If A(I) A(J) Then
A(0) = A(I)
A(I) = A(J)
A(J) = A(0)
End If
Next
Next
For I = 1 To N
Open App.Path "\" Trim(Str(I)) ".txt" For Output As #1
Print #1, A(I)
Close #1
Next
Print "已经把"; N; "个数写入到"; App.Path; "\1.txt 到 "; N; ".txt中.请查看."
End Sub
'已经运行过.
第二题:
DIM 是变量声明语句,它的格式为:
dim 变量名[as 格式] [,变量名[as 格式][,变量名[as 格式]......]
其中:
变量名:以字母或汉字开始的字串,代表一个变量
格式有以下几种:
属于数字的有五种:
(1)字节型:byte可取值0-255
(2)整形:integer可取值-32768至32767
(3)长整形:long(可取值范围很大的正负整数)
(4)单精度型:single(可取值小数)
(5)双精度型:double(可取值范围更大,小数位数更多的小数)
字符串型:string(可代表由字母\数字或汉字组成的字符集合)
布尔型:boolean(取值为ture\false)
日期型:date(可表示形如2009-5-26 02:36这样的组合)
如果要用姓名\住址\单位名称...等用字符串型(string)
eg:dim name as string(用name变量表示名字时,声明成字符串变量)
如果是用数字需要做计算,如工资\合计\人数....等要用数字型,但有一个原则,优先选用范围小的(按照字节型(byte)\整形(integer)\长整形(long)\单精度型(single)\双精度型(double)的顺序选择),够用就可以了,这样可以占用内存少,运算速度快.
eg:dim count as integer(用integer表示员工人数时,可声明成整形变量)
eg:dim sum as single(用sum表示工资时,可声明成单精度型变量)
不知是否说得清楚了.
什么都不说,先上效果图——
编码——
Private Sub Command1_Click()
Dim s1 As String, s_num As String, a As String
Dim lennum As Integer, lenuchar As Integer, lenlchar As Integer
Dim s_lchar As String, s_uchar As String
Dim i As Integer, length As Integer, value As Integer
s1 = Text1.Text
s_num = "" '用来存放数字的字符串变量
s_uchar = "" '用来存放大写字母的字符串变量
s_lchar = "" '用来存放小写字母的字符串变量
length = Len(s1)
For i = 1 To length
a = Mid(s1, i, 1)
value = Asc(a)
Text2.Text = value
If value = 48 And value = 57 Then s_num = s_num a
If value = 65 And value = 90 Then s_uchar = s_uchar a
If value = 97 And value = 122 Then s_lchar = s_lchar a
Next i
' 把字符串中的各种字符分类成 数字类,大写字母,小写字母
lennum = Len(s_num)
lenuchar = Len(s_uchar)
lenlchar = Len(s_lchar)
Dim sz() As Integer '定义动态数组
Dim temp As Integer
ReDim sz(1 To lennum) As Integer '定义数组供 数字的ASCII码
For i = 1 To lennum
a = Mid(s_num, i, 1)
sz(i) = Asc(a)
Next i
For i = lennum To 1 Step -1 '最常用的冒泡排序法
For j = 1 To i - 1
If sz(j) sz(j + 1) Then temp = sz(j): sz(j) = sz(j + 1): sz(j + 1) = temp
Next j
Next i
s_num = "" '清空s_num ,因为要重新排列了
For i = 1 To lennum
s_num = s_num Chr(sz(i))
Next i
ReDim sz(1 To lenuchar) As Integer '重新定义数组供 大写字母的ASCII码,接下来程序思路与上面类似
For i = 1 To lenuchar
a = Mid(s_uchar, i, 1)
sz(i) = Asc(a)
Next i
For i = lenuchar To 1 Step -1
For j = 1 To i - 1
If sz(j) sz(j + 1) Then temp = sz(j): sz(j) = sz(j + 1): sz(j + 1) = temp
Next j
Next i
s_uchar = ""
For i = 1 To lenuchar
s_uchar = s_uchar Chr(sz(i))
Next i
ReDim sz(1 To lenlchar) As Integer '重新定义数组供 小写字母的ASCII码,接下来程序思路与上面类似
For i = 1 To lenlchar
a = Mid(s_lchar, i, 1)
sz(i) = Asc(a)
Next i
For i = lenlchar To 1 Step -1
For j = 1 To i - 1
If sz(j) sz(j + 1) Then temp = sz(j): sz(j) = sz(j + 1): sz(j + 1) = temp
Next j
Next i
s_lchar = ""
For i = 1 To lenlchar
s_lchar = s_lchar Chr(sz(i))
Next i
Text2.Text = s_uchar s_lchar s_num
End Sub
该程序主要运用了Asc()函数获得ASCII码来分辨字母与数字,用 动态数组和重定义来多次运用数组进行排序(冒泡排序法)和赋值操作,用chr()函数还原字符串。
我的程序有点略长,中间有少许重复部分,但我觉得思路比较清晰易懂,适合初学者。你也可以试着自己改进。
你这个是冒泡排序么?从大到小么?
For j = 0 To lstNetworkRisk.Items.Count - 1 应该改成
For j = i To lstNetworkRisk.Items.Count - 1
另外尽量不要用GOTO语句。而且完全也可以不用这个GOTO。
还有不知道你用的什么版本的.net 如果是3.5的完全可以直接用linq。