新手,vb排列大小的问题
用vb编写一个程序随机产生10个二位数,放入数组a中,从中选出一个最大的和最小的数显示出来,但是有点小问题,请看
我正在做这样的练习题:
我的源代码是这样的
Private Sub Command1_Click()
Dim b As Integer
Dim d As Integer
Dim a(10) As Integer
d = a(0)
c = a(0)
For i = 0 To 9
Randomize
a(i) = Rnd() * 100
If a(i) > d Then
d = a(i)
End If
If a(i) < c Then
c = a(i)
End If
Print "所产生的数 " & a(i)
Next
Print "最大的数是 " & d
Print "最小的数是 " & c
End Sub
这样运行,最大的数是可以出来的,但是最小的数是:0,这个我也想了,因为c=a(0)
这个值是空值,认为是0,这样,进行循环,判断,只能是0,那么,怎么办,才能完成这个作业?
[解决办法]
c = 100
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
最新版本:20070212
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
Private Sub Command1_Click()
Dim c As Integer
Dim d As Integer
Dim a(10) As Integer
d = 0 : c = 100
dim i as integer
For i = 0 To 9
Randomize
a(i) = Rnd() * 100
If a(i) > d Then d = a(i)
If a(i) < c Then c = a(i)
Print "第 " & (i+1) & " 个数: " & a(i)
Next
Print "最大的数是 " & d
Print "最小的数是 " & c
End Sub
实际上,没有必要使用数组
Private Sub Command1_Click()
Dim c As Integer
Dim d As Integer
Dim a As Integer
d = 0 : c = 100
dim i as integer
For i = 0 To 9
Randomize
a = Rnd() * 100
If a > d Then d = a
If a < c Then c = a
Print "第 " & (i+1) & " 个数: " & a
Next
Print "最大的数是 " & d
Print "最小的数是 " & c
End Sub