从枚举获取整数值 [英] Getting the integer value from enum
本文介绍了从枚举获取整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个基本的 Battleship游戏,以帮助我提高C#技能.现在,我在枚举方面遇到了一些麻烦.我有:
I am working on a basic Battleship game to help my C# skills. Right now I am having a little trouble with enum. I have:
enum game : int
{
a=1,
b=2,
c=3,
}
我希望播放器传递输入"C",并且某些代码返回整数 3
.我如何设置它以接受字符串var( string pick;
)并使用此枚举将其转换为正确的int?我正在阅读的这本书有点令人困惑
I would like the player to pass the input "C" and some code return the integer 3
. How would I set it up for it to take a string var (string pick;
) and convert it to the correct int using this enum? The book I am reading on this is bit confusing
推荐答案
只需解析字符串并将其强制转换为int.
Just parse the string and cast to int.
var number = (int)((game) Enum.Parse(typeof(game), pick));
这篇关于从枚举获取整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文