質問

So I'm taking a class on C# and am currently attempting to take a rather simple program and make it a little more complicated. However my googlefu skills aren't quite working. I was hoping for a little help and a little explaining as well, since everything online that explains it, is doing from an experts standpoint not a noobs perspective.

So basically there is an enumerator type declared and i know you can insert lines of code so the switch can make it's decision depending on what is defined or chosen. However I would like for this to interactively happen so that a user can enter a value from the enumeration list and then have switch function do its magic. I've tried a few different things and nothing works. Mind you I'm a perl, PHP and PowerShell programmer and I've tried some things that worked in all of them, but I can't quite get it to work out correctly. Anywho, below is the code. Any help is greatly appreciated!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
class Program
{
    enum Color
    {
        red,
        orange,
        blue,
        black,
        white,
        green,
        purple,
        yellow
    }
    static void Main(string[] args)
    {
       Color favorite = Color + Console.ReadLine();
        switch (favorite)
        {
            case Color.red:
                Console.WriteLine("You chose red!");
                break;
            case Color.orange:
                Console.WriteLine("you chose orange!!!!!!");
                break;
            case Color.blue:
                Console.WriteLine("YOU CHOSE BLUEEEE!!");
                break;               
            case Color.black:
                Console.WriteLine("you chose black");
                break;
            case Color.white:
                Console.WriteLine(" you chose white!")
                    break;
            case Color.green:
                Console.WriteLine("you chose green!!!!!");
                break;
            case Color.purple:
                Console.WriteLine("you chose purple!!");
                break;
            case Color.yellow:
                Console.WriteLine("you chose yellow!!!");
                break;
        }
        Console.ReadLine();




    }
}

}

役に立ちましたか?

解決

You are looking for the Enum.TryParse method. It will convert a string to an enum type and will return false if the string could not be converted to an enum.

Color favorite 
if (Enum.TryParse(Console.ReadLine(), out favorite)) {
   // You're switch goes where
}
else {
   Console.WriteLine("That's not a color!");
}

他のヒント

If your input is in form "Red", "Orange", etc then you can use the Enum.Parse():

var favorite = (Color)Enum.Parse(typeof(Color), Console.ReadLine());

Be ware that if the user inputs a bad color it will throw exception, in that case you can use TryParse.

Change the assignment of favorite.

var input = Console.ReadLine();
var favorite = (Color)Enum.Parse(typeof(Color), input);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top