我想对数组的每个行和列以及对角线执行加法运算,并将结果显示给用户. [英] I Want to perform addition on each of the rows and columns and diagonally of the array and display the results to the user.

查看:99
本文介绍了我想对数组的每个行和列以及对角线执行加法运算,并将结果显示给用户.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随机数发生器工作正常.我有一个问题是添加行和列.

The Random generator works fine. What I have a problem with is adding the rows and columns.

   Array
     0,0	 0,1	   0,2  = row total
     1,0	 1,1	   1,2  = row total
     2,0	 2,1	   2,2  = row total
      =          =          =
  col total   coltotal   col total
also add  0,0  1,1  2,2  diagonaly


Private Sub BtnFillArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFillArray.Click

        Dim Row As Integer
        Dim Col As Integer
        Dim Number As Integer

        Number = RandomNumber.Next(RandomNumberLimit)
        For Row = 0 To 2
            For Col = 0 To 2
                TwoDim(Row, Col) = Number
                
                Number += 1
            Next Col
        Next Row

        Lbl3x3ArrCell00.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell01.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell02.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell10.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell11.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell12.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell20.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell21.Text = CStr(RandomNumber.Next(RandomNumberLimit))
        Lbl3x3ArrCell22.Text = CStr(RandomNumber.Next(RandomNumberLimit))
    
LblGBRowR1r0.Text = ""   (holds results of 0,0 0,1 0,2)
    LblGBRowR1r1.Text = ""   (holds results of 1,0 1,1 1,2)
    LblGBRowR1r2.Text = ""   (holds results of 2,0 2,1 2,2)
    
    LblGBColC1c0.Text = ""   (holds results of 0,0 1,0 2,0)
    LblGBColC1c1.Text = ""   (holds results of 0,1 1,1 2,1)
    LblGBColC1c2.Text = ""   (holds results of 0,2 2,1 2,2)
    
    LblGBDiaL.Text = ""      (holds results of 0,0 1,1 2,2)
    LblGBDiaR.Text = ""      (holds results of 02 1,1 2,0)
 End Sub

Tried this below but results were faulty (worked but wrong answers)
 (Example Below)
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
    LblGBRowR1r0.Text = Convert.ToString(CInt(((TwoDim(0, 2) + TwoDim(0, 1) + TwoDim(0, 0)))))
    
    LblGBColC1c0.Text = Convert.ToString(CInt(((TwoDim(0, 0) + TwoDim(1, 0) + TwoDim(2, 0)))))
   
    LblGBDiaL.Text = Convert.ToString(CInt(((TwoDim(0, 0) + TwoDim(1, 1) + TwoDim(2, 2)))))
   
    'End Sub


只需一个简单的方法即可添加谢谢


Just need a simple method to add Thanks

推荐答案

非常糟糕.处理数据,而不是表示数据的字符串.您可以使用2D数组,但是更改数组的大小将是一个问题.如果需要将大小更改为预先已知的最大值,请从一开始就使用最大大小的数组.如果不符合您的要求,请使用通用类型System.Collections.Generic.List.数字列表的列表将表示大小可变的2D数字数组(矩阵).将此数据类型封装在一些包装器类中,这将仅允许有效操作(支持大小相等的所有嵌套数组,等等).

—SA
Very bad. Work with data, not strings representing data. You can use 2D arrays, but changing the size of arrays would be a problem. If you need to change the size up to some maximum known in advance, use the array of maximum size from the very beginning. If it does not meet your requirements, use the generic type System.Collections.Generic.List. The list of lists of number will represent a 2D array (matrix) of numbers with variable size. Encapsulate this data type in some wrapper class which would allow only valid operations (supporting all nested array sized equal, etc.).

—SA


您的第一个问题是最大的问题.您正在将数据以文本形式存储在许多标签控件中.别!控件用于显示事物,而不是用于存储您的工作数据.

您可以从将值存储在二维数组中开始.要分配随机值,您可以使用几个简单的循环遍历这些元素:
Your first problem is the biggest. You''re storing your data as text in a bunch of label controls. Don''t! Controls are for displaying things, not for storing your working data.

You can start by storing your values in a 2 dimensional array. To assign your random values you can iterate through those elements using a couple of simple loops:
Dim RNG As New Random()
Dim values(3, 3) As Integer

For i As Integer = 0 to 2
    For j As Integer = 0 To 2
        values(i, j) = RNG.Next(RandomNumberLimit)
    Next
Next



累加一行值非常容易:



To add up a row of values is pretty easy:

Dim rowTotal As Integer
For i As Integer = 0 to 2
    rowTotal = rowTotal + values(i, 0)
Next



如果您真的想学习基础知识,请暂时忘记标签控件,而只需输出到Debug或编写Console应用即可.



If you really want to learn the basics, forget about the label controls for now and just output to either Debug or write a Console app instead.


这篇关于我想对数组的每个行和列以及对角线执行加法运算,并将结果显示给用户.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆