| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 579 人关注过本帖
标题:关于类中多维数组的问题
取消只看楼主 加入收藏
asjiazhen
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2010-8-25
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:2 
关于类中多维数组的问题
圣诞节快乐,麻烦各位帮忙解惑。
关于多维数组方法返回值的问题。有三个构造函数,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
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
快速回复:关于类中多维数组的问题
数据加载中...
 
   



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

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