关于类中多维数组的问题
圣诞节快乐,麻烦各位帮忙解惑。关于多维数组方法返回值的问题。有三个构造函数,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