vb题目:随机产生20个正整数存入二维数组A(4,5),并输出到文本框Text1中,找出其中的素数存入一维数组B并输

出到Text2中,用选择法对B数组元素排序后输出到Text3中

Private Sub Command1_Click()
Dim i, j, A(1 To 4, 1 To 5) As Integer, p, B(), n, temp
Text1 = ""
For i = 1 To UBound(A, 2)
For j = 1 To UBound(A, 1)
Randomize
A(j, i) = Int(Rnd * 100) '0-99的正整数
Text1 = Text1 & A(j, i) & " "
If fc(A(j, i)) Then
n = n + 1
ReDim Preserve B(1 To n)
B(n) = A(j, i)
End If
Next
Print
Next
If n > 0 Then
Text2 = Join(B)
Else
Text2 = ""
Text3 = ""
Exit Sub
End If
For i = 1 To UBound(B) - 1
p = i
For j = i + 1 To UBound(B)
If B(j) < B(p) Then p = j
Next j
If p <> i Then
temp = B(p)
B(p) = B(i)
B(i) = temp
End If
Next
Text3 = Join(B)
End Sub
Function fc(n) As Boolean
Dim i
If n < 2 Then Exit Function
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then Exit Function
Next
fc = True
End Function追问

求简化。。。那个join(B)是什么意思。。。另外,可否留名咨询?

追答

join(B) 就是把组数连起来输出,可以带格式。
你也可以这样输出,效果是一样的,但啰嗦
for i=lbound(b) to ubound(b)
s=s & b(i) & space(1)
next
text3=trim(s)
-------------------
简化?
UBound(A, 1) -----二维数组的一维上标
UBound(A, 2) -----二维数组的二维上标
这些你都可以用数字指定,但实用的软件很多都是不确定的,所以必须这样做。
其它的没有什么好简化了,素数可能有也可能没有,所以必须是动态数组。判断素数使用函数这样更容易理解,最后就是选择法排序了。

追问

那个能否直接简单的告诉我如何把二维数组A(4,5)转换为一维数组B???我们学的很简单,复杂的不是很懂~~

追答

'修改了一下,按一维数组来处理
Option Explicit
Private Sub Command1_Click()
Dim i, j, A(1 To 20) As Integer, p, B(), n, temp
Text1 = ""
For i = 1 To 20
Randomize
A(i) = Int(Rnd * 100) '0-99的正整数
Text1 = Text1 & A(i) & " "
If fc(A(i)) Then
n = n + 1
ReDim Preserve B(1 To n)
B(n) = A(i)
End If
Next
If n > 0 Then
Text2 = Join(B)
Else
Text2 = ""
Text3 = ""
Exit Sub
End If
For i = 1 To UBound(B) - 1
p = i
For j = i + 1 To UBound(B)
If B(j) i Then
temp = B(p)
B(p) = B(i)
B(i) = temp
End If
Next
Text3 = Join(B)
End Sub
Function fc(n) As Boolean
Dim i
If n < 2 Then Exit Function
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then Exit Function
Next
fc = True
End Function

温馨提示:内容为网友见解,仅供参考
无其他回答

VB编程:产生20个10至100间的随机整数存入一维数组A中,每行5个元素输出...
Private Sub Command1_Click()Dim a(20) As Integer, b(4, 5) As Integer Randomize For i = 1 To 20 a(i) = Int(Rnd * 91) + 10 Print a(i);If i Mod 5 = 0 Then Print Next Print For i = 1 To 4 For j = 1 To 5 b(i, j) = a(j + (i - 1) * 5)Print...

Vb用随机函数产生20个值为20—80的整数,赋值给一个五行四列的二维数组...
Private Sub Form_click()Me.ClsDim s(1 To 5, 1 To 4) As IntegerDim i, j, t As IntegerDim num(1 To 5), max As SingleFor i = 1 To 5t = 0 For j = 1 To 4 s(i, j) = Int(61 * Rnd + 20) t = t + s(i, j) Print s(i, j); Space(2); ...

...2. 编写一个程序,在文本框中任意输入一个自然数N,单击“计算”按钮则...
1. 随机产生30~100 (包括30 、100 )的20个正整数,显示整个数组的值,并求最大值、最小值、平均值,显示结果。2. 编写一个程序,在文本框中任意输入一个自然数N,单击“计算”按钮则将斐波那契数列的第N项显示在标签中,如图压缩包中所示。sy62.rar 3. 编写一个英文打字训练的程序,要求如...

VB编写程序实现以下功能:设有一个二维数组A(4,4),每个数组元素为两位...
Private Sub Command1_Click()'交换第二列和第四列Text1.Text = ""Text2.Text = ""Label1.Caption = ""Dim AA(1 To 4, 1 To 4) As Integer, BB(1 To 4, 1 To 4) As IntegerText1.Text = "交换前的数据" & vbCrLfFor I = 1 To 4 For J = 1 To 4 Randomize A...

随机数生成25个正整数,组成一个5*5的二维数组,然后找出最大元素及最...
Dim A(5, 5) As Integer Dim Max As Integer '记录最大值 Dim X As Byte, Y As Byte '记录最大值坐标 Cls Me.CurrentX = 0 Me.CurrentY = 800 '自动生成25个区间在[100,999]的三位数 Randomize For n1 = 1 To 5 For n2 = 1 To 5 A(n1, n2) = 900 * Rnd + 100 If A(...

用VB程序将1到25的自然数依次赋值给5*5的二维数组并且输出数组的下三...
代码如下:Private Sub Command1_Click()Dim a(1 To 6, 1 To 6) As IntegerFor i = 1 To 5For j = 1 To 5tmp = 99If i < tmp Thentmp = iEnd IfIf j < tmp Thentmp = jEnd IfIf 6 - i < tmp Thentmp = 6 - iEnd IfIf 6 - j < tmp Thentmp = 6 - jEnd Ifa(i...

如何用vb随机产生一个二维数组,找到其最大和最小值并输出,然后再按照行 ...
Private Type MyPoint X As Integer Y As Integer End Type Private Sub Form_Click()Dim N As Integer, A() As Integer N = 5 '如果用户输入,请改成 Inputbox ReDim A(1 To N, 1 To N) As Integer Dim Max As Integer, Min As Integer Dim MaxP As MyPoint, MinP As MyPoint ...

VB编程,输出一个二维数组中每一列的最大值,并指出所在行
private sub cmd1_click()dim i,j,a(3,3) as integer dim b(3) as integer for i=1 to 3 for j=1 to 3 a(i,j)=i+j next j next i for i=1 to 3 for j=1 to 3 if b(i)<a(j,i) then b(i)=a(j,i)end if next j next i end sub 其中,一维数组b存放的就是...

VB 设有一个二维数组A(4,4),请交换其中的第二列和第四列并显示
Private Sub Command1_Click()Dim A(4, 4)'数组赋值Print "交换前:"For i = 1 To 4For j = 1 To 4A(i, j) = (i - 1) * 4 + j'数组显示Print A(i, j),NextPrintNext'交换数组的第二列和第四列For j = 1 To 4t = A(2, j)A(2, j) = A(4, j)A(4, j) = ...

VB编程随机产生“10到129”的数,形成一个4*5 的二维数组,按升序排列...
Print a(i, j);Next Print Next Print "冒泡法升序输出"For i = 0 To (4 * 5 - 1)For j = 0 To (4 * 5 - 1) - 1 - i If a(j \\ 5, j Mod 5) > a((j + 1) \\ 5, (j + 1) Mod 5) Then t = a(j \\ 5, j Mod 5)a(j \\ 5, j Mod 5) = a((j ...

相似回答