vb交换数组

vb交换数组,第1张

从键盘里读入10个数

for i = 1 to 10

input data( i )

next i

for i = 1 to 5

tmp = data( i )

data( i ) = data( 10 - i + 1 )

data( 10 - i + 1 ) = tmp

next i

for i = 1 to 10

print data( i ),

next i

输入一个5*5方阵

for i = 1 to 5

for j = 1 to 5

input data( i, j )

result = 1

for i = 1 to 5

result = result * data( i, i )

next i

print result

vb2012版本可以吗?

Public Class Form1

Function Reverse(a() As Integer)

        Dim leng As Integer = a.Length / 2 - 1

        Dim i As Integer

        Dim swap As Integer

        For i = 0 To leng

            swap = a(i)

            a(i) = a(a.Length - 1 - i)

            a(a.Length - 1 - i) = swap

        Next

        Return a

    End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim m%(0 To 10)

        Dim n%(0 To 12) '//vs2012只能这样声明

        Dim i As Integer

'初始化数组同时输出:

        dataTxt.Text += "m%:" + vbCrLf

        For i = 0 To 10

            m(i) = Int(Rnd() * 100)

            dataTxt.Text += m(i).ToString() + " "

        Next

        '交换:

        m = Reverse(m)

        '交换后输出:

        dataTxt.Text += vbCrLf + "m%:" + vbCrLf

        For i = 0 To 10

            dataTxt.Text += m(i).ToString() + " "

        Next

        dataTxt.Text += vbCrLf + "n%:" + vbCrLf

        For i = 0 To 12

            n(i) = Int(Rnd() * 100)

            dataTxt.Text += n(i).ToString() + " "

        Next

        n = Reverse(n)

        dataTxt.Text += vbCrLf + "n%:" + vbCrLf

        For i = 0 To 12

            dataTxt.Text += n(i).ToString() + " "

        Next

End Sub

End Class

 张志晨


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12102636.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存