I am making a 15 puzzle game for school but right now it only has 9 numbers but if i add 2 digit numbers it messes up the whole grid. So I want there to be 2 digits on all numbers (2→→02).
int[,] tab = { { 2, 1, 7 }, { 6, 9, 4 }, { 3, 0, 7 } };
CodePudding user response:
Since you are working with an 2D array you can do something like this:
int[,] tab = { { 2, 1, 7 }, { 6, 9, 4 }, { 3, 0, 7 } };
for (int i = 0; i < 3; i ) {
for (int j = 0; j < 3; j ) {
Console.Write(String.Format("{0,2:00} ", tab[i,j]));
}
Console.Write("\n");
}
Here we are using String.Format to print the numbers as two digits. Since there is only one digit for the integers in the table, a leading zero will automatically be added. So for your code the output would be:
02 01 07
06 09 04
03 00 07
CodePudding user response:
When displaying the numbers, format the strings. Here's an example:
int value = 2;
string result = String.Format("{0,2:00}", value);
Console.WriteLine(result);
The example displays the following output:
02