No puedo mover el signo de almohadilla hacia la derecha para obtener la siguiente forma. Mi código a continuación no funciona como se esperaba, pero necesito obtener la forma a continuación.
por favor como lo hago
# ## ### #### ##### ###### ####### public class MyProgramTest { public static void StaircaseChallenge(int n) { for (int i = 1; i <= n; i++) { Console.WriteLine(MySpace(i) + HashSign(i)); } } public static string HashSign(int n) { string t = ""; for (int i = 1; i <= n; i++) { t += "#"; } return t; } public static string MySpace(int n) { string t = "/t"; for (int i = 1; i < n; i++) { t += " "; } return t; } }Prueba esto:
public class MyProgramTest { public static void StaircaseChallenge(int n) { for (int i = 1; i <= n; i++) { Console.WriteLine(" ".PadLeft(n - i+1, ' ')+"#".PadLeft(i,'#')); } }O haga algunos cambios en su código:
public class MyProgramTest { public static void StaircaseChallenge(int n) { for (int i = 1; i <= n; i++) { Console.WriteLine(MySpace(n - i + 1) + HashSign(i)); } } public static string HashSign(int n) { string t = ""; for (int i = 1; i <= n; i++) { t += "#"; } return t; } public static string MySpace(int n) { string t = string.Empty; for (int i = 1; i < n; i++) { t += " "; } return t; } }Cambie solo algunas cosas en su código:
public class MyProgramTest { public static void StaircaseChallenge(int n) { for (int i = 1; i <= n; i++) { Console.WriteLine(MySpace(i, n) + HashSign(i)); } } public static string HashSign(int n) { string t = ""; for (int i = 1; i <= n; i++) { t += "#"; } return t; } public static string MySpace(int m, int n) { string t = ""; for (int i = 1; i <= n - m; i++) { t += " "; } return t; } }Tienes que pasar más una variable es n (número de fila) en la función MySpace() para dejar espacio. Cuando pasa el número de fila en la función MySpace(), dejará (número de fila - 1) espacio. Entonces, si ingresa 5, la primera vez dejará 4 espacios y luego pondrá "#" de la misma manera.
Una forma más eficiente de memoria sería usar la clase StringBuilder.
Para esta situación no es crítico, pero es bueno saberlo.
// define the amount of steps int n=8; // amount of leading whitespaces, for later usage int padding=0; // this one is the "working" memory, initialized by n + padding whitespaces StringBuilder currentLine=new StringBuilder(new string(' ',n+padding)); // it counts down from the last index to the one indicated by padding for (int i = currentLine.Length-1; i >=padding; i--) { // replace the char at the current index with #; (here: always the index of the last whitespace) currentLine[i]='#'; // display a copy of the current state on the console, Console.WriteLine(currentLine.ToString()); }