+ 1
Hey ! How to make 3 or more dimension array? String type ... / c#
suppose each dimension containe "a" and "b" string [ , , ] ab = {{"a","a","a"},{"a","a","b"},...,{"b","b","b"}} How to print the whole array?
7 Réponses
+ 2
string[ , , ] ab = { { { "a", "a", "a" }, { "a", "a", "b" }, { "b", "b", "a" }, { "b", "a", "b" }, { "b", "a", "a" } } };
int r1 = ab.GetLength(0);
int r2 = ab.GetLength(1);
int r3 = ab.GetLength(2);
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < r2; j++)
{
for(int k = 0;k < r3; k++)
{
Console.Write(string.Format("{0} ", ab[i, j, k]));
}
Console.WriteLine();
}
}
+ 1
The problem is in :
public string nts (int a)
You can't add parameters that different from the return type
In this case you can't use int as a parameters
Use this instead :
public string nts (string a)
And change the case to a string value like "0" and "1"
0
Thank you Wardana ... thank you Matthews
Is it possible to don't declare the whole array and make the program complete it ?( When the array continues regular like this)
a,a,a
a,a,b
a,b,a
a,b,b
b,a,a
b,a,b
b,b,a
b,b,b
0
I wrote this (using Wardana code)
What's the problem ?
(it should print the array i mentioned at the previous post )
0
public string nts(int a)
{
string b;
switch (a)
{
case 0:
b="a";
break;
case 1:
b="b";
break;
}
return b;
}
string[ , , ] ab = new string[2,2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for(int k = 0;k < 2; k++)
{
ab[i, j, k] = {nts(i), nts(j), nts(k)};
Console.Write("{0}", ab[i, j, k]);
}
Console.WriteLine();
}
}
0
Thank u a lot Wardana
I'll try it ...
0
hi Wardana
Thanks ...it worked ...
https://code.sololearn.com/clu0099s4kCV/?ref=app