Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Utfordring: Enumerator | Strukturer og Enumeratorer
C# Utover Det Grunnleggende

bookUtfordring: Enumerator

Fyll inn de tomme feltene for å fullføre enum-definisjonen. Les gjennom koden og finn ut hva enumens navn er, samt hvilke medlemmer den skal ha.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; class Program { // Fill in the blanks below this line ___ ___ { ___ } static void Main(string[] args) { Potion potion = Potion.Invisibility; switch (potion) { case Potion.Invisibility: Console.WriteLine("You drink the Invisibility potion and vanish from sight!"); break; case Potion.Strength: Console.WriteLine("You drink the Strength potion and feel a surge of power!"); break; case Potion.Healing: Console.WriteLine("You drink the Healing potion and your wounds magically mend."); break; case Potion.FireResistance: Console.WriteLine("You drink the Fire Resistance potion and become impervious to flames."); break; case Potion.Teleportation: Console.WriteLine("You drink the Teleportation potion and find yourself in a new location!"); break; default: Console.WriteLine("Invalid potion type!"); break; } } }
  1. Når du får tilgang til en enum-konstant bruker vi syntaksen enumName.constName der enumName er navnet på enumeratoren.
  2. Enum-en skal ha fem elementer. Se på case-ene i switch-setningen for å finne ut hvilke konstanter som brukes. En av dem er Invisibility.
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
using System; class Program { enum Potion { Invisibility, Strength, Healing, FireResistance, Teleportation } static void Main(string[] args) { Potion potion = Potion.Invisibility; switch (potion) { case Potion.Invisibility: Console.WriteLine("You drink the Invisibility potion and vanish from sight!"); break; case Potion.Strength: Console.WriteLine("You drink the Strength potion and feel a surge of power!"); break; case Potion.Healing: Console.WriteLine("You drink the Healing potion and your wounds magically mend."); break; case Potion.FireResistance: Console.WriteLine("You drink the Fire Resistance potion and become impervious to flames."); break; case Potion.Teleportation: Console.WriteLine("You drink the Teleportation potion and find yourself in a new location!"); break; default: Console.WriteLine("Invalid potion type!"); break; } } }
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 11

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

What is the code for the switch statement so I can see all the enum members?

Can you show me the part of the code where the enum is defined?

Can you list all the constants used in the switch statement?

Awesome!

Completion rate improved to 2.04

bookUtfordring: Enumerator

Sveip for å vise menyen

Fyll inn de tomme feltene for å fullføre enum-definisjonen. Les gjennom koden og finn ut hva enumens navn er, samt hvilke medlemmer den skal ha.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; class Program { // Fill in the blanks below this line ___ ___ { ___ } static void Main(string[] args) { Potion potion = Potion.Invisibility; switch (potion) { case Potion.Invisibility: Console.WriteLine("You drink the Invisibility potion and vanish from sight!"); break; case Potion.Strength: Console.WriteLine("You drink the Strength potion and feel a surge of power!"); break; case Potion.Healing: Console.WriteLine("You drink the Healing potion and your wounds magically mend."); break; case Potion.FireResistance: Console.WriteLine("You drink the Fire Resistance potion and become impervious to flames."); break; case Potion.Teleportation: Console.WriteLine("You drink the Teleportation potion and find yourself in a new location!"); break; default: Console.WriteLine("Invalid potion type!"); break; } } }
  1. Når du får tilgang til en enum-konstant bruker vi syntaksen enumName.constName der enumName er navnet på enumeratoren.
  2. Enum-en skal ha fem elementer. Se på case-ene i switch-setningen for å finne ut hvilke konstanter som brukes. En av dem er Invisibility.
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
using System; class Program { enum Potion { Invisibility, Strength, Healing, FireResistance, Teleportation } static void Main(string[] args) { Potion potion = Potion.Invisibility; switch (potion) { case Potion.Invisibility: Console.WriteLine("You drink the Invisibility potion and vanish from sight!"); break; case Potion.Strength: Console.WriteLine("You drink the Strength potion and feel a surge of power!"); break; case Potion.Healing: Console.WriteLine("You drink the Healing potion and your wounds magically mend."); break; case Potion.FireResistance: Console.WriteLine("You drink the Fire Resistance potion and become impervious to flames."); break; case Potion.Teleportation: Console.WriteLine("You drink the Teleportation potion and find yourself in a new location!"); break; default: Console.WriteLine("Invalid potion type!"); break; } } }
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 11
some-alt