编写程序,随机产生20整数到文本框中,将其中的奇数按从小到大的顺序在一文本框中输出.
编写程序,随机产生20整数到文本框中,将其中的奇数按从小到大的顺序在一文本框中输出.
Dim a(19) As Integer
Dim i, j As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mystr As String
mystr = ""
For i = 0 To 19
Randomize()
a(i) = Int(90 * Rnd() + 10)
mystr = mystr & a(i) & Space(2)
If i = 9 Then
mystr = mystr & vbCrLf
End If
Next
TextBox1.Text = mystr
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s() As Object
Dim k As Integer
Dim mystr As String = ""
Static m As Integer
For i = 0 To 19
If a(i) Mod 2 <> 0 Then
m = m + 1
End If
s(m) = a(i)
Next
For i = 0 To m - 2
For j = i + 1 To m - 1
If s(i) > s(j) Then
k = s(i)
s(i) = s(j)
s(j) = k
End If
Next
Next
For i = 0 To m
mystr = mystr & s(i)
Next
MsgBox(mystr, , "从小到大")
End Sub
我想问一下为什么vb.net没c#.net的执行速度快,这是个问题啊!
Dim a(20) As Integer
Dim i, j As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mystr As String
mystr = ""
For i =1 To 20
Randomize()
a(i) = Int(90 * Rnd() + 10)
mystr = mystr & a(i) & Space(2)
If i mod 5=0 Then
mystr = mystr & vbCrLf
End If
Next
TextBox1.Text = mystr
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dim k ,temp as integer
dim str as string
for i= 1 to 19
k =i
for j= 2 to 20
if a(k)>a(j) then
k=j
next j
temp=a(i)
a(i)=a(k)
a(k)=temp
next i
for i= 1 to 20
if a(i) mod 2 <>0 then
str=str & a(i) & space(2)
if a(i) mod 5 =0 then
str=str & vbcrlf
end if
next i
msgbox("从小到大排列奇数:" & str )
end sub
不好意思
昨天不是在环境中写的
有点错误
今天把代码拷过来了
Dim a(20) As Integer
Dim i, j As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mystr As String
mystr = ""
For i = 1 To 20
Randomize()
a(i) = Int(90 * Rnd() + 10)
mystr = mystr & a(i) & Space(2)
If i Mod 5 = 0 Then
mystr = mystr & vbCrLf
End If
Next
Textbox1.Text = mystr
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim k, temp As Integer
Dim str As String = ""
For i = 1 To 19
k = i
For j = i To 20
If a(k) > a(j) Then
k = j
End If
Next j
temp = a(i)
a(i) = a(k)
a(k) = temp
Next i
For i = 1 To 20
If a(i) Mod 2 <> 0 Then
str = str & a(i) & Space(2)
End If
Next i
MsgBox("从小到大排列奇数:" & Chr(10) & str)
End Sub