标题:关于类中多维数组的问题
只看楼主
asjiazhen
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2010-8-25
结帖率:62.5%
已结贴  问题点数:20 回复次数:6 
关于类中多维数组的问题
圣诞节快乐,麻烦各位帮忙解惑。
关于多维数组方法返回值的问题。有三个构造函数,Class1和Class2是关于多维数组返回的,Class3是一维数组返回的问题。
textbox1中输入一个值,在Class1和Class2进行多维数组运算,然后将计算结果返回到主程序中,return m1[0][0]返回的是多维数组的初值,return a[0]则返回的是一维数组的运算结果。请帮忙解决下。
主程序
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dwsz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double[] a = new double[6] { 1, 3, 4, 2, 6, 4 };
        double[] b = new double[4] { 4, 2, 3, 6 };
        double[] c = new double[3] { 6, 5, 1 };
        double[][] m = new double[3][] { new double[6], new double[4], new double[3] };
        double[][] m1 = new double[3][] { new double[6], new double[4], new double[3] };

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            label2.Text = "";
            label3.Text = "";
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        m[i][j] = a[j];
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m[i][j] = b[j];
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        m[i][j] = c[j];
                    }
                }
            }

            double n = Convert.ToDouble(textBox1.Text);

            Class1 a1 = new Class1(n, m);
            double mm = a1.fangfa1();
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        label1.Text += m[i][j].ToString() + "\0";
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        label1.Text += m[i][j].ToString() + "\0";
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        label1.Text += m[i][j].ToString() + "\0";
                    }
                }
                label1.Text += "\r";
            }

            Class2 a2 = new Class2();
            a2.fangfa2(n, m);
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        m1[i][j] = m[i][j];
                        label2.Text += m1[i][j].ToString() + "\t";
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m1[i][j] = m[i][j];
                        label2.Text += m1[i][j].ToString() + "\t";
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        m1[i][j] = m[i][j];
                        label2.Text += m1[i][j].ToString() + "\t";
                    }
                }
                label2.Text += "\r";
            }

            Class3 a3 = new Class3();
            a3.fangfa3(n, a);
            for (int i = 0; i < 6; i++)
            {
                label3.Text += a[i].ToString() + "\r";
            }

            
        }
    }
}

类1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dwsz
{
    class Class1
    {
        double[][] m1 = new double[3][] { new double[6], new double[4], new double[3] };
        public Class1(double n, params double[][] m)
        {
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
            }
        }

        public double fangfa1()
        {
            return m1[0][0];
        }
    }
}

类2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dwsz
{
    class Class2
    {
        double[][] m1 = new double[3][] { new double[6], new double[4], new double[3] };
        
        public double fangfa2(double n, params double[][] m)
        {            
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
            }            
            return m1[1][0];            
        }        
    }
}

类3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dwsz
{
    class Class3
    {
        public double fangfa3(double n, params double[] a)
        {
            for (int i = 0; i < 6; i++)
            {
                a[i] = a[i] + n;
            }
            return a[0];
        }
    }
}

例如:textbox1的值是2,计算结果在窗口中Label1和Label2中显示的是1, 3, 4, 2, 6, 4  4, 2, 3, 6  6, 5, 1;Label3中显示的是3,5,6,4,8,6
搜索更多相关主题的帖子: 主程序 圣诞节快乐 
2010-12-25 12:06
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:5 
代码太长了 唤作是我也没有耐心了 LZ可以上传完整项目 或者贴出自己疑惑的部分

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-25 13:26
asjiazhen
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2010-8-25
得分:0 
类2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dwsz
{
    class Class2
    {
        double[][] m1 = new double[3][] { new double[6], new double[4], new double[3] };
        
        public double fangfa2(double n, params double[][] m)
        {            
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 1)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
                if (i == 2)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        m1[i][j] = m[i][j] + n;
                    }
                }
            }            
            return m1[1][0];            
        }        
    }
}
涂红这部分出了点问题,返回值不是所求,而是多维数组初值。该如何解决。
2010-12-25 20:19
asjiazhen
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2010-8-25
得分:0 
这个项目就是完整的。
2010-12-25 20:19
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:5 
public double fangfa2(double n, params double[][] m)//好像是因为这里,楼主去看一下C#的书。
 我的理解是:params关键字后面不能跟二维的数组。如何改吗,要花时间想想。

   唯实惟新 至诚致志
2010-12-25 22:32
刘晓鹏
Rank: 2
等 级:论坛游民
帖 子:70
专家分:68
注 册:2010-1-4
得分:5 
C#二维数组不是这样的么 数据类型 变量名[,]
2010-12-26 11:48
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:0 
楼主的是锯齿数组啊,是那种每维的无素个数不同的数组,是C#独有的。6楼你不知道吗?

   唯实惟新 至诚致志
2010-12-26 12:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-329881-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.114606 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved