• 0

[C#] little noob exercice on a deitel book


Question

im beginning with c# and im at a chapter where " while for switch" is explained i need to to some exercices but there are a couple that i dont know how to do :\

it says to print something like

*********

********

*******

******

*****

****

***

**

*

just using "for" and a Console.Write("*");

i did one that looks

*

**

***

*****

and so on using something like... (uh not sure if this is the code exactly im remembering it, im not at home now...)

string asterisk="*";

for ( int counter = 1; counter <=10; counter++ , asterisk += "*")

{

Console.Write("*");

Console.Writeline("");

}

and it works but i cant do the inverse like at the beginning of the post .

Also the next exercice says to draw a Diamond using the * but with the large of a number beetwin 1 and 19 input by user...

i need a bit of help ^^ thx :D

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You have the loop upside down. Try this:

for ( int counter = 10; counter &gt; 0; --counter)
 ? ?Console.WriteLine( new string('*', counter) );

For the diamond, you simply combine the two steps, go from low to high, then high to low, you just need to space it adequately.

Hope that helps.

Link to comment
Share on other sites

  • 0

int MaxStars = 10;
for(int i=0; i&lt;MaxStars; i++)
{
 ?for(int j=0; j&lt;MaxStars-i; j++)
 {
 ? Console.WriteLine("*");
 }
 Console.WriteLine("\n"); //carrage return
}

Just thought of that real quick, does it work?

Link to comment
Share on other sites

  • 0

Single quotes are character literals and double quotes are string literals. A character literal can only be one character, otherwise it would be a string(of characters)

char c = 'c'; // correct
char ac = 'ac'; // incorrect
string s_ac = "ac"; // correct

The constructor for a string in bwx's example is new string( char /*character initialized to*/, int /* times repeated */ )

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.