当前位置:首页 » VB程序题 » 正文

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程序设计教程  答案 陈斌  著

来源:济亨网

本文链接:http://www.wb98.com/post/161.html

本站文章搜索:

<< 上一篇下一篇 >>

文章搜索

Tags列表

赞助商链接

湘公网安备 43011102000514号 - ICP备08100508号