我在C#中使用#include <codecvt>
#include <locale>
#include <cassert>
template<class F> struct myFacet : F { using F::F; ~myFacet() {} };
int main()
{
std::string in = u8"水"; // UTF-8
// utf8 to wide (could've used en_US.utf8, but this one exists as-is)
std::wstring ws = std::wstring_convert<std::codecvt_utf8<wchar_t>>{}.from_bytes(in);
assert(ws == L"水");
// wide to another mb (have to use a named locale now)
typedef myFacet<std::codecvt_byname<wchar_t, char, std::mbstate_t>> F;
std::string out = std::wstring_convert<F>{ new F("zh_CN.gb18030") }.to_bytes(ws);
assert(out == "\xcb\xae");
}
来计算不同形状的区域。以下是我的代码:
enums
以下内容会产生错误:
using System;
class Area
{
public enum Shape{Circle,Square};
public void AreaShape(int x,Shape ob1)
{
double a;
switch(x)
{
case (int)Shape.Circle:
a=Math.PI*x*x;
Console.WriteLine("Circle "+a);
break;
case (int)Shape.Square:
a=x*x;
Console.WriteLine("Square "+a);
break;
default:
Console.WriteLine("Invalid");
break;
}
}
}
class MyTestOne
{
public static void Main(String[] a)
{
Area obj1=new Area();
obj1.AreaShape(15,Area.Shape.Circle);
obj1.AreaShape(15,Area.Shape.Square);
}
}
如何获得以下输出?
obj1.AreaShape(15,Shape.Circle);
obj1.AreaShape(15,Shape.Square);
当我使用Circle 225*pi
Square 225
和Area.Shape.Circle
时,输出为:
Area.Shape.Square
答案 0 :(得分:1)
传递形状枚举类而不是int x变量
switch (ob1)
{
case Shape.Circle:
a = Math.PI * x * x;
Console.WriteLine("Circle " + a);
break;
case Shape.Square:
a = x * x;
Console.WriteLine("Square " + a);
break;
default:
Console.WriteLine("Invalid");
break;
}
答案 1 :(得分:0)
您的switch
语句会启用x
,您始终将其作为15
传入。你应该打开ob1
枚举变量,你不需要演员。