Vb编写程序,随机生成 10 个[1,100]的整数,输出其平均值及大于平均值的数。
程序如下:
Private Sub Form_Activate()
Dim a(1 To 10) As Integer '定义存放10个小于等于100的整数数组
Dim sum, i, max, min As Integer '定义sum存放10个数的和,min放10个数中最小值,max放最大值,i循环控制变量
Dim aver As Single '定义aver放平均值
sum = 0
max = 0
min = 100
Randomize
For i = 1 To 10 '生成随机数存入数组,并求出和、最大值、最小值
a(i) = Int(Rnd * 100 + 1)
sum = sum + a(i)
Print a(i)
If a(i) > max Then max = a(i)
If a(i) < min Then min = a(i)
Next i
aver = sum / 10
Print "平均数是" & aver
For i = 1 To 10 '逐渐改变最大值、最小值与平均值最接近的
If (a(i) aver) Then max = a(i)
If (a(i) >= min) And (a(i) < aver) Then min = a(i)
Next i
If (max - aver) > (aver - min) Then '找到最接近的值
Print "最接近平均值得数是" & min
Else
If (max - aver) < (aver - min) Then
Print "最接近平均值得数是" & max
Else
Print "最接近平均值得数是" & min & vbTab & max
End If
End If
End Sub
运行结果如下:
Option Base 1
Private Sub Command1_Click()
Randomize
Dim i As Integer, a(10) As Integer, s As Integer, valAverage As Single
For i = 1 To 10
a(i) = Int(Rnd(i) * 90 + 10)
Print a(i);
s = s + a(i)
Next i
Print
valAverage = s / 10
Print "平均值为:" + CStr(Format(s / 10, "##.000"))
For i = 1 To 10
If a(i) > valAverage Then
Print a(i);
End If
Next
Print
Print
End Sub
Private Sub Command1_Click() '生成10-100间10个数字求平均值及大于平均值的数字
Me.Cls
Dim a() As Integer, i As Integer, he As Integer
Dim pj As Single
ProduceShu 10, 100, 10, a()
Print "生成的10个数字分别为:" & Space(2);
For i = 0 To 9
Print a(i);
he = he + a(i)
Next i
pj = he / 10
Print: Print "平均值为:" & Space(2);
Print Format(pj, "0.00"): Print "大于平均值的数字为" & Space(2);
For i = 0 To 9
If a(i) > pj Then
Print a(i);
End If
Next i
End Sub
Private Function ProduceShu(ByVal m As Integer, ByVal n As Integer, _
ByVal num As Integer, a() As Integer) As Integer '生成10个数字
Dim i As Integer
ReDim a(num)
For i = 0 To num - 1
Randomize
a(i) = Int((n - m + 1) * Rnd + m)
Next i
End Function
用VB编写程序,随机生成10个10~99互不相同的整数,将它们按由小到大的...
Private Sub Command1_Click()Cls Dim a(1 To 10) As Integer For i = 1 To 10 Randomize a(i) = Int(Rnd * 90 + 10)For j = 1 To i - 1 If a(i) = a(j) Then i = i - 1 Exit For End If Next j Next i Print "产生的随机数为:"For i = 1 To UBound(a)Prin...
用vb语言编写一个程序,随机生成两个100-300之间的正整数,显示他们,并...
max As Integer min = 100 max = 300 Randomize() a = CInt((max - min) * Rnd() + min) b = CInt((max - min) * Rnd() + min) TextBox1.Text = a TextBox2.Text = b
编写程序,随机生成10个三位正整数,将它们按由小到大的升序排序,并按每...
public static string str;private static char[] constant = { '0','1','2','3','4','5','6','7','8','9',\/\/ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'\/\/'...
编写VB程序。随机生成100个三位正整数,并统计其中大于500、小于等于5...
If n <= 500 Then b = b + 1 If n > 900 Then c = c + 1 Next Print "大于500的个数:"; a; "小于等于500的个数:"; b; "大于900的个数:"; c End Sub
编写VB程序:用随机函数生成20个数值,并求出其中的奇数和与偶数和 还...
Private Sub Command1_Click() Randomize (Timer) Label1.Caption = "" Label2.Caption = "" Label3.Caption = "" For i = 1 To 20 n = Int(Rnd * 9000 + 1000) Label1.Caption = Label1.Caption + CStr(n) + Space(1) If i Mod 10 = 0 Then Label1.Caption...
vb编写程序 随机产生10∼99间 10个整数 选择法 递增顺序
Form1上放一个Command1,一个list1用来显示随机产生的整数 Private Sub Command1_click()Dim a(9) As Integer '定义数组a用来存放随机产生的10个数 Dim Temp As Integer For i = 0 To 9Randomize a(i) = int(Rnd * 89 + 10) '随机生成一个10-99之间的整数 Next For i = 0 To 9...
vb程序设计:随机产生一个三位正整数,然后逆序输出该数,产生的数与逆序...
数字逆序法:Private Sub Form_Click()Dim x As IntegerDim n1 As Integer, n2 As Integer, n3 As IntegerRandomizex = Int(Rnd * 900) + 100n1 = x \\ 100n2 = (x Mod 100) \\ 10n3 = x Mod 10Print x, n3 * 100 + n2 * 10 + n1End Sub字符串逆序法:Private Sub Form_Click...
vb程序设计题 求代码 随机产生20个0~100的正整数(包括0和100),存放...
'这题目,对于初学者是很有难度的,'运行效果如上图,代码如下:'有问题可以问我Private Sub Command1_Click()Dim arr(1 To 20) As IntegerDim i, a, b As IntegerDim Ave As DoubleDim sum As IntegerDim Mx, Mn As IntegerPrint "随机数数组为:"RandomizeFor i = 1 To 20 arr(i) ...
编一VB应用程序。要求如下: ⑴ 随机产生20个学生的VB程序设计课程的成绩...
Dim a, b, c, d, e As Double Private Sub Command1_Click(Index As Integer)ReDim Preserve x(21) '重新定义一个有21个元素的一维数组x k = UBound(x) - 1 '把一维数组x的下标上界减1剩下就是20个元素(学生)了 Randomize '随机数的初始化 Select Case Index Case 0 Text1 = "随机...
编写程序,随机产生三个[100,300]的整数,并按升序排列输出
include<time.h> int main(){ int a,b,c,t;srand(time(NULL));a=rand()%201+100;b=rand()%201+100;c=rand()%201+100;if(a>b){t=a;a=b;b=t;} if(a>c){t=a;a=c;c=t;} if(b>c){t=b;b=c;c=t;} printf("a=%d b=%d c=%d\\n",a,b,c);getch();return 0...
仲诚嘉欣: Private Sub Command1_Click() Open "d:\data.txt" For Output As #1 Randomize Print "输入文件:" For i = 1 To 10 x = 30 + Int(Rnd * 61) Print #1, x; Print x; Next i Print Close #1 Print "从文件中读出:" Open "d:\data.txt" For Input As #1 ...
端州区13180194758: VB 编写程序,生成10个无重复数(即互不相等)的两位随机数Private Sub Command1 - Click()Dim a(10) As Integer,n As IntegerDim i As Integer,k As ... - ?
仲诚嘉欣:[答案] 两种方法 1 补充你的算法, Private Sub Command1_Click() Dim a(10) As Integer,n As Integer Dim i As Integer,k As Integer Randomize a(1) = Int(Rnd * 90) + 10 k = 1 Do n = Int(Rnd * 90) + 10 For i = 1 To 10 If a(i) = n Then Exit For Next i If i > 10 Then k...
端州区13180194758: 用VB语言随机生成10个不能重复的数?? - ?
仲诚嘉欣: Private Sub Command1_Click() Dim i As Integer Dim a As Integer Randomize For i = 1 To 10 a = Int(10 * Rnd) Print a; Next i End Sub
端州区13180194758: Vb编写程序,随机生成 10 个[1,100]的整数,输出其平均值及大于平均值的数. - ?
仲诚嘉欣: Private Sub Command1_Click() '生成10-100间10个数字求平均值及大于平均值的数字 Me.Cls Dim a() As Integer, i As Integer, he As Integer Dim pj As Single ProduceShu 10, 100, 10, a() Print "生成的10个数字分别为:" & Space(2); For i = ...
端州区13180194758: VB怎么写随机生成10个[1,100]之间的数,并从大到小排列的程序? - ?
仲诚嘉欣:[答案] private sub command1_click() dim a(1 to 10) as integer,i,j for i=1 to 10 a(i)=int(rnd*100)+1 print a(i); next print for i=1 to 10 for j=1 to i if a(i)
端州区13180194758: vb语句 设计一个随机数程序,生成10个范围是1到100随机整数,并求出这10个数的最大值,最小值和平均数 - ?
仲诚嘉欣:[答案] '声明变量dim Min_num as intdim Max_num as intdim Sum_num as intdim Arr_num(9) as int'赋予初始值Min_num=101Max_num=-1Sum_num=0'循环10次获得10个随机数for i=1 to 10 randomize Arr_num(i-1)=int(r...
端州区13180194758: vb编写一个程序,利用随机函数Rnd生成10个10 - 99之间的整数,并计算其中素数的个数 - ?
仲诚嘉欣:n= [ 10+int(90*rnd) ]st = [ st + " " + n ]forj=2 to [ n ][ n1=n1+1 ][ exit for ]text1.text=[ st ]text2.text= [ n-n1 ]
端州区13180194758: 帮我解一下这道VB编程题:“随机产生10个两位整数,找出其中的最大值、最小值和平均值”帮帮忙啦! - ?
仲诚嘉欣:[答案] dim a(0 to 9), max,min,i as integerdim avg as singlerandomizefor i=0 to 9a(i)=int(rnd*90+10)nextmax=a(0)min=a(0)for i=0 to 9if a(i)>max then max=a(i)if a(i)
端州区13180194758: 用VB编写程序,随机生成10个10~99互不相同的整数,将它们按由小到大的顺序排列起来 - ?
仲诚嘉欣: Private Sub Command1_Click()ClsDim a(1 To 10) As IntegerFor i = 1 To 10Randomizea(i) = Int(Rnd * 90 + 10)For j = 1 To i - 1If a(i) = a(j) Theni = i - 1Exit ForEnd IfNext jNext iPrint "产生的随机数为:"For i = 1 To UBound(a)...
端州区13180194758: VB新手,想知道如何编辑代码可以随机产生10个不重复的1 - 100的数 - ?
仲诚嘉欣: randomize dim a(9) as byte for i = 0 to 9 a(i) = int(rnd * 99 + 1) next