I'm writing string[] to show multiple rows in the output. A stupid question, why the output is not 5 x 5?(I used i and j both from 0-4, then the ouput should be an array 5 x 5), can anyone help?
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>0 30 3 13 25 30 </string>
<string>0 30 10 22 27 </string>
<string>0 30 12 17 </string>
<string>0 30 5 </string>
<string>0 30 </string>
</ArrayOfstring>
Here's my input: .../3/10/12/5
Codes below:
public IEnumerable<string> route5(int city1, int city2, int city3, int city4)
{
int[] city = new int[] { 0, city1, city2, city3, city4 };
string[] layout = new string[5];
for (int i = 0; i < 5; i++)
{
string message = "";
for (int j = 0; j < 5; j++)
{
if (j - i == 0) message= "0"+" ";
if (j > i)
{
if (j - i == 1) message+= (city[j]).ToString()+" ";
else if (j - i == 2) message+= (city[j] + city[j - 1]).ToString()+" ";
else if (j - i == 3) message+= (city[j] + city[j - 1] + city[j - 2]).ToString() + " ";
else message+= (city1 + city2 + city3 + city4).ToString() + " ";
}
else
{
if (i - j == 1) message+= (city[i]).ToString()+" ";
else if (i - j == 2) message+= (city[i] + city[i - 1]).ToString() + " ";
else if (i - j == 3) message+= (city[i] + city[i - 1] + city[i - 2]).ToString()+ " ";
else message+= (city1 + city2 + city3 + city4).ToString() + " ";
}
}
layout[i]= message;
}
return layout;
}
if (j - i == 0) message= "0"+" ";
, themessage=
should bemessage+=
instead. As it is now, that code overwrites themessage
string rather than appending to it, which is what I believe you intended.if (j > i)
should likely beelse if (j > i)
instead. As it's now, you have one extra value being added rather than just the likely intended 5 values.