Revizuiți soluția pentru a crea o metodă reutilizabilă
Următorul cod este o soluție posibilă pentru provocarea de la unitatea anterioară.
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]} ");
}
}
Acest cod este doar "o soluție posibilă", deoarece este posibil să fi adăugat fluxuri de linii în locuri diferite sau este posibil să fi formatat codul în mod diferit.
Indiferent de diferențele minore de cod, atunci când rulați codul, ar trebui să vedeți unul dintre următoarele mesaje de ieșire:
```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.
```
Rezultatul trebuie să fie dependent de valoarea variabilei luck .
Dacă ați terminat provocarea, felicitări! Continuați verificarea cunoștințelor în unitatea următoare.
Important
Dacă ai avut probleme la finalizarea acestei provocări, ia în considerare revizuirea unităților anterioare înainte de a continua. Toate ideile noi pe care le discutăm în alte module vor depinde de înțelegerea ideilor care au fost prezentate în acest modul.