【发布时间】:2019-07-02 11:22:08
【问题描述】:
问题
我制作了一个结构、一个枚举和一个方法。 枚举在结构中使用。该方法使用结构体。
当我尝试在 main 中接受 来自用户的输入并调用方法时,我收到了转换错误。 我无法将结构体转换为双精度体。
在这种情况下如何转换用户的输入?
额外信息
(这应该是转换温度类型,现在我只写了1个温度的转换,但其他的都是一样的(除了公式))
PS:我还没有学习列表和接口,我不能在不同的课程中做到这一点。
public enum MyEnum {C,F,K}
public struct MyStruct
{
public double foo;
public MyEnum bar;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Make user put in a double followed by a space and one of the Enums");
string input = Console.ReadLine();
string[] parts = input.Split(' ');
MyStruct temp = new MyStruct ();
temp.foo= double.Parse(parts[0]);
if(parts[1] == "C")
{
TheMethodIUSedForC(parts[0]);
temp.bar= MyEnum.C;
}
}
public MyStruct TheMethodIUSedForC(MyStruct t)
{
if (t.bar== MyEnum.F)
{
t.bar= MyEnum.F;
t.foo = (t.foo* 1.8) + 32;
return t;
}
else if (t.bar== MyEnum.K)
{
t.bar= MyEnum.K;
t.foo= t.foo + 273.15;
return t;
}
else
{
return t;
}
}
【问题讨论】:
标签: c# methods struct enums type-conversion