Using a while loop to create a pyramid in C# -
i'm learning here of c# still, have made pyramid asterisks loop:
using system; namespace nimi{ class ohjelma{ static void main(){ for(;;){ console.write("anna korkeus: "); string eka = console.readline(); int luku = int.parse(eka); //first, made if number 0 or lower, ask number again. //hence endless loop @ start. if(luku <= 0){ continue; } else { (int = 0; < luku; i++ ) { (int k=i+1; k < luku; k++) { console.write(" "); } (int j = 2*i+1; j > 0; j--) { console.write("*"); } console.writeline(""); } break; } } } } }
i'm out of curiosity wanting know how work while-loop haven't been able create still. way thought be:
using system; namespace nimi{ class ohjelma{ static void main(){ while(true){ // while-loop version of endless loop. not sure how different is. console.write("anna korkeus: "); string eka = console.readline(); int luku = int.parse(eka); if(luku <= 0){ continue; } else { int = 0; int j = * 2 + 1; int k = i+1; while(i < luku) { while (j > 0){ while (k < luku){ console.write(" "); k++; } console.write("*"); j--; } console.writeline(); i++; } break; } } } } }
don't work out. posts (when value 4:
*
what's correct way transfer for-loop while-loop create pyramid asterisks?
this
using system; namespace nimi { class ohjelma { static void main() { (; ; ) { console.write("anna korkeus: "); string eka = console.readline(); int luku = int.parse(eka); //first, made if number 0 or lower, ask number again. //hence endless loop @ start. if (luku <= 0) { continue; } else { int = 0; while (i < luku) { int k = + 1; while (k < luku) { console.write(" "); k++; } int j = 2 * + 1; while (j > 0) { console.write("*"); j--; } console.writeline(""); i++; } break; } } } } }
and remember: if code correctly formatted, it's easier read. ^ed in visual studio format everything.
(^ed means ctrl+e , d (with d don't need ctrl, work both ways)
Comments
Post a Comment