Ich versuche die Farbe eines Balls zufällig zu ändern, aber wenn ich versuche, die Farbe zu verwenden, wird die Farbe des festen Pinsels ist nicht zugewiesen. "Die Verwendung von nicht zugewiesenen lokalen Variablen 'ballColour'"Wenn ich eine Variable im If/Else-Block ändere, sagt ein Fehler, dass es nicht zugewiesen ist
xPos = xPos + GAP_SIZE;
int colour = rand.Next(1, 40);
Color ballColour;
if (colour >= 1 && colour <= 9)
{
ballColour = Color.Blue;
}
else if (colour >= 10 && colour <= 19)
{
ballColour = Color.Orange;
}
else if (colour >= 20 && colour <= 29)
{
ballColour = Color.Green;
}
else if (colour >= 30 && colour <= 39)
{
ballColour = Color.Red;
}
else if (colour == 40)
{
ballColour = Color.Purple;
}
SolidBrush ballColourBrush = new SolidBrush(ballColour); //This is the one that returns the error.
paper.FillEllipse(ballColourBrush, xPos, yPos, BALL_SIZE, BALL_SIZE);
xPos = xPos + BALL_SIZE;
Diese Antwort ist gut genug, aber es sagt genau das, was die bereits bestehenden sagt, mit weniger Erklärung. Nicht wirklich sicher, warum du es hinzugefügt hast. –