请将下面代码中的三个两层for循环,简化为一个两层for循环。 要求:使用泛型来代替函数PrintT的参数数据类型,原代码的功能实现不变! public void PrintT(double[,] e, int[,] root, double[,] w,int RootX,int WX,int EY,int x,int y,int AddDis)
{
RootX = 700;
WX = 360;
EY = 60;
x = 20;
y = EY;
AddDis = 40; for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (e[i, j] != 0)
{ AddToForm(e[i, j].ToString("0.00"),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = 20;
}
}
}
x = RootX;
y = EY;
for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (root[i, j] != 0)
{ AddToForm(root[i, j].ToString(),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = RootX;
}
}
}
x = WX;
y = EY;
for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (w[i, j] != 0)
{ AddToForm(w[i, j].ToString("0.00"),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = WX;
}
}
}
}
{
RootX = 700;
WX = 360;
EY = 60;
x = 20;
y = EY;
AddDis = 40; for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (e[i, j] != 0)
{ AddToForm(e[i, j].ToString("0.00"),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = 20;
}
}
}
x = RootX;
y = EY;
for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (root[i, j] != 0)
{ AddToForm(root[i, j].ToString(),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = RootX;
}
}
}
x = WX;
y = EY;
for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (w[i, j] != 0)
{ AddToForm(w[i, j].ToString("0.00"),x,y); }
else
{ }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = WX;
}
}
}
}
class A<T>
{
public void Get(T[,] e,int RootX,out int x,out int y,int AddDis)
{
for (int i = 1; i <= 7; i++)
{
for (int j = 0; j <= 6; j++)
{
if (e[i, j] != 0)
{ AddToForm(e[i, j].ToString(),x,y); }
x += AddDis;
if (j == 6)
{
y += AddDis;
x = RootX;
}
}
}
}
}
通过out 取值
private static void Print<T>(T[,] array, int x, int y, int addDis)
{
int X = x; for (int i = 1; i < 7; i++)
{
for (int j = 0; j < 6; j++)
{
if (array[i, j] != 0)
{
AddToForm(array[i, j].ToString("0.00"), x, y);
} x += addDis; if (j == 6)
{
y += addDis;
x = X;
}
}
}
}
private static void Print(double[,] e, int[,] root, double[,] w, int RootX, int WX, int EY, int x, int y, int AddDis)
{
RootX = 700;
WX = 360;
EY = 60;
x = 20;
y = EY;
AddDis = 40; Print<double>(e, 20, EY, AddDis);
Print<int>(root, RootX, EY, AddDis);
Print<double>(w, WX, EY, AddDis);
}
AddToForm(array[i, j].ToString("0.00"), x, y);//错误2 “ToString”方法没有采用“1”个参数的重载 }这两个错误应该如何解决,请高手帮忙,谢谢!
for (int j = 0; j < 6; j++)
{
if (Convert.ToDouble(array[i, j])!= 0)
{
AddToForm(Convert.ToDouble(array[i, j]).ToString("0.00"), x, y);
} x += addDis; if (j == 6)
{
y += addDis;
x = X;
}
}