Amanda said:
Rihgt now I am working on the diamond. I am just doing w/o the option
of giving the user to enter an odd integer, like this:
public static void Diamond()
{
for (int i = 1, j = 0; i <9; i++)
{
for (int p = 8; p >=j; p--)
Console.Write(" ");
for (int p = 0; p <= j; p++)
Console.Write("*");
for (int p = 1; p <= j; p++)
Console.Write("*");
j++;
Console.WriteLine();
}
for (int i = 0, j = 0; i < 9; i++)
{
for (int c = 8; c >= j; c--)
Console.Write("*");
Console.WriteLine();
for (int p = 0; p <= j; p++)
Console.Write(" ");
for (int p = 9; p > j; p--)
Console.Write("*");
j++;
}
Okay. Before moving on to changing what the code does, I'm going to do
two things:
1) Put braces around *every* loop, even the inner ones.
2) Get rid of j.
To explain the second bit further:
In both of your loops, j is computable from i - in the first case,
j=i-1, in the second case j=i. It's very rare to see this sort of
double initialization in production code (at least in my experience)
partly because it makes it harder to understand. The whole goal of this
first stage is to change the code to make it easier to understand,
without changing the behaviour. The technical name for that activity is
*refactoring*.
So, here's your code after the above changes:
public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
for (int p = 8; p >= (i-1); p--)
{
Console.Write(" ");
}
for (int p = 0; p <= (i-1); p++)
{
Console.Write("*");
}
for (int p = 1; p <= (i-1); p++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = 0; i < 9; i++)
{
for (int c = 8; c >= i; c--)
{
Console.Write("*");
}
Console.WriteLine();
for (int p = 0; p <= i; p++)
{
Console.Write(" ");
}
for (int p = 9; p > i; p--)
{
Console.Write("*");
}
}
}
Now let's apply another refactoring suggested by chanmm: change every
inner loop to use <, and count upwards from 0:
public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
for (int p = 0; p < 10-i; p++)
{
Console.Write(" ");
}
for (int p = 0; p < i; p++)
{
Console.Write("*");
}
for (int p = 0; p < i-1; p++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = 0; i < 9; i++)
{
for (int c = 0; c < 9-i; c++)
{
Console.Write("*");
}
Console.WriteLine();
for (int p = 0; p < i+1; p++)
{
Console.Write(" ");
}
for (int p = 0; p < 9-i; p++)
{
Console.Write("*");
}
}
}
Now the nice thing about those loops is that they're all writing either
spaces or stars, with the number of spaces or stars to write being the
right hand side of the middle expression. We'll now use the "extract
method" refactoring to make things simpler again, by having one method
to write stars and one to write spaces. (We could have a single method
which took a parameter which was the character to write, but that's
just a variation on the theme.)
public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
WriteSpaces (10-i);
WriteStars (i);
WriteStars (i-1);
Console.WriteLine();
}
for (int i = 0; i < 9; i++)
{
WriteStars (9-i);
Console.WriteLine();
WriteSpaces(i+1);
WriteStars(9-i);
}
}
static void WriteStars(int count)
{
for (int i=0; i < count; i++)
{
Console.Write("*");
}
}
static void WriteSpaces(int count)
{
for (int i=0; i < count; i++)
{
Console.Write(" ");
}
}
Now, I'll pause there - partly because we've done an awful lot already,
and partly because I don't know whether you're allowed to use extra
methods in your code to make it more readable. Apologies if you've
already stated this in another post.
Now, as for the extra line - note that you're calling Console.WriteLine
at the *end* of each iteration of the first loop, but *in the middle*
of each iteration of the second loop. That should give you a hint as to
how to change the logic.
Do let us know how you get on with the above - and if any of it doesn't
make sense, please say and I'll go back and explain in more detail.