五笔打字通主页
3. 参照下图所示界面,编写一个求由一位随机整数构成的数组每一行与每一列之和。
解,在窗体上建2个按钮,一个图片框,再建2个文本框(用于输出求行,列的和),此题VB代码如下:
Option Base 1
Dim a(4, 5) As Integer
Dim SumL As Integer, SumC As Integer
Private Sub Command1_Click()
Dim intI As Integer, intJ As Integer
Picture1.Cls
Randomize '随机种子
For I = 1 To 4
For J = 1 To 5
a(I, J) = Int(Rnd * 9) + 1
Picture1.Print a(I, J);
Next J
Picture1.Print
Next I
End Sub
Private Sub Command2_Click()
Dim intI As Integer, intJ As Integer
Text1.Text = ""
Text2.Text = ""
For I = 1 To 4 '求每一行的和
SumL = 0
For J = 1 To 5
SumL = SumL + a(I, J)
Next J
Text1.Text = Text1.Text & SumL & Chr(13) & Chr(10)
Next I
For J = 1 To 5 '求每一列的和
SumC = 0
For I = 1 To 4
SumC = SumC + a(I, J)
Next I
Text2.Text = Text2.Text & SumC & " "
Next J
End Sub
此题程序运行效果图如下:
---- 新编Visual Basic程序设计教程 答案 陈斌 著
来源:济亨网
本文链接:https://wb98.com/post/161.html