Bekijk de oplossing om een herbruikbare methode te maken
De volgende code is één mogelijke oplossing voor de uitdaging van de vorige les.
Random random = new Random();
int luck = random.Next(100);
string[] text = {"You have much to", "Today is a day to", "Whatever work you do", "This is an ideal time to"};
string[] good = {"look forward to.", "try new things!", "is likely to succeed.", "accomplish your dreams!"};
string[] bad = {"fear.", "avoid major decisions.", "may have unexpected outcomes.", "re-evaluate your life."};
string[] neutral = {"appreciate.", "enjoy time with friends.", "should align with your values.", "get in tune with nature."};
TellFortune();
void TellFortune()
{
Console.WriteLine("A fortune teller whispers the following words:");
string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral));
for (int i = 0; i < 4; i++)
{
Console.Write($"{text[i]} {fortune[i]} ");
}
}
Deze code is slechts één mogelijke oplossing, omdat u mogelijk regelfeeds op verschillende plekken hebt toegevoegd of de code anders hebt opgemaakt.
Ongeacht kleine codeverschillen ziet u, wanneer u de code uitvoert, een van de volgende uitvoerberichten:
```Output A
A fortune teller whispers the following words:
You have much to look forward to. Today is a day to try new things! Whatever work you do is likely to succeed. This is an ideal time to accomplish your dreams!
```
```Output B
A fortune teller whispers the following words:
You have much to appreciate. Today is a day to enjoy time with friends. Whatever work you do should align with your values. This is an ideal time to get in tune with nature.
```
```Output C
A fortune teller whispers the following words:
You have much to fear. Today is a day to avoid major decisions. Whatever work you do may have unexpected outcomes. This is an ideal time to re-evaluate your life.
```
De uitvoer moet afhankelijk zijn van de waarde van de luck variabele.
Als u de uitdaging hebt voltooid, gefeliciteerd! Ga door naar de kennistoets in de volgende eenheid.
Belangrijk
Als u problemen ondervindt bij het voltooien van deze uitdaging, kunt u overwegen de vorige lessen te bekijken voordat u doorgaat. Voor alle nieuwe ideeën die in andere modules worden besproken, is het belangrijk dat u de inhoud van deze module begrijpt.