c# - Print ASCII of a trangle shape with varable input -
struggling print. know should 2 loops print out repeated letters, however, having problems indent lines. should simple console c# program print out shape below input 3.
xxxxx xxx x
with input 4 should like
xxxxxxx xxxxx xxx x
here code. 2 loops letters correctly lines lined @ left, not center.
static void main(string[] args) { string num = console.readline().trim(); int n = convert.toint32(num); int k=1; for(int = n; i>=1; i--) { console.writeline("\n"); console.writeline("".padleft(n)); (int j = (2*i-1); j>=1;j--) { console.write("0"); } } console.read(); }
the statement:
console.writeline("".padleft(n));
is right idea it's not quite there.
in case, n
number of lines wish print , invariant. number of spaces need @ start of each line should begin @ 0 , increase 1 each line.
in case, printing any number of spaces followed newline (because it's writeline
) not want, should using write
instead.
so code between int n = convert.toint32(num);
, console.read();
better off like:
for (int linenum = 0; linenum < n; linenum++) { int spacecount = linenum; int xcount = (n - linenum) * 2 - 1; console.write("".padleft(spacecount)); console.writeline("".padleft(xcount, 'x')); }
you'll notice other changes there. first, i've used more meaningful variable names, i'm bit of stickler - using i
okay in circumstances find it's more readable use descriptive names.
i've used padleft
x
string well, remove need inner loop.
Comments
Post a Comment