c# - For loop skips straight to the end -
i started studying c# , encountered problem 1 of assignments. assignment create pyramid made of stars. height designated user input.
for reason first for
loop skips end. while debugging, noticed variable height
receives bar
's value, after skips end. have no clue why, code seems fine me.
the do
-while
loop there ask user new value, in case value entered 0
or lower.
using system; namespace viope { class vioppe { static void main() { int bar; { console.write("anna korkeus: "); string foo = console.readline(); bar = int.parse(foo); } while (bar <= 0); (int height = bar; height == 0; height--) { (int spaces = height; spaces == height - 1; spaces--) { console.write(" "); } (int stars = 1; stars >= height; stars = stars * 2 - 1) { console.write("*"); } console.writeline(); } } } }
the condition in for
loop condition has keep being true in order go loop body. this:
for (int height = bar; height == 0; height--)
should be:
for (int height = bar; height >= 0; height--)
otherwise, assignment performed, check whether height
0, , if it's not (which bound case), that's end of loop.
see msdn documentation for
loops more information.
Comments
Post a Comment