Schreiben Sie die FunkQon makeX() mit dem Parameter size.
Ihre FunkQon sollte einen (Text-)Kasten der entsprechenden Höhe und Breite zeichnen, bestehend aus
Punkten und Hash-Symbolen (#). Die #-Zeichen sollen dabei ein Kreuz bilden.
Testen Sie Ihre FunkQon mit folgendem Code:
void setup() {
makeX(5);
makeX(10);
}
void setup ()
{
makeX(5);
makeX(10);
}
void makeX(int n)
{
for(int z =0 ; z < n; z++)
{
for(int s = 0; s < n; s++)
{
if (s == z && s > z || z == s )
{
print("#");
}
else
{
print(".");
}
}
println();
}
}
I am nearly finished.
I just need to make the line on the oder side.
I think I need to expand the if cause.
Well, that’s already never going to be true, is it? s can’t equal z and be greater than z at the same time! So the first half of this expression is always false.
So what it really says is… if False OR z equals s…
But False is never going to satisfy the condition. So we can simplify it!
This is the same expression, without the useless first two thirds:
if ( s == z )
Now, since you are drawing an X (which I can thankfully tell based on the function name), you draw one line for it when s and z are equal. What can you say about the relationship between s, z and n along the other line? (Hint: What can you say about n-s and z?)
void setup ()
{
makeX(5);
makeX(10);
}
void makeX(int n)
{
for (int z =0; z < n; z++)
{
for (int s = 0; s < n; s++)
{
if (s == z || n - s == z+ 1 )
{
print("#");
} else
{
print(".");
}
}
println();
}
}