这是我现在正在做的一个例子:
return
shpc == 0 ? "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck." :
shpc == 1 ? "Currently, based on your selection below, you have one hidden card in your card deck." :
$"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible.";
Run Code Online (Sandbox Code Playgroud)
代码字但不太了解添加到 switch 的内容 我想知道这是否也可以用 switch 表达式完成?
我在 JAVA 中有一个 enum 和 POJO 类。在枚举类中,每个枚举值都与 POJO 类的变量匹配......然后我想在两个类之间创建关系。
枚举类:
public enum MyEnum
{
FIELD1,
FIELD2,
FIELD3,
...
}
Run Code Online (Sandbox Code Playgroud)
POJO类:
public class MyPojo
{
private String field1;
private String field2_not_ref;
private String field3;
...
}
Run Code Online (Sandbox Code Playgroud)
然后,当我尝试匹配两个类的这些字段时,我实现了如下代码:
public String matchMethod(MyEnum myenum)
{
switch(myenum)
{
case FIELD1:
return field1;
case FIELD2:
return field2_not_ref;
...
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这不是一个好的/明确的解决方案。有什么建议吗?
我已经尝试实现 c# 8.0 switch-case 但不幸的是它没有工作,我想如果 case 满足,则在 switch 表达式中为满足的 case 返回一个特定的字符串。
这是我的代码:
public static void GetErrMsg(Exception ex) =>
ex switch
{
ex is UserNotFoundException => "User is not found.",
ex is NotAuthorizedException => "You'r not authorized."
};
Run Code Online (Sandbox Code Playgroud)
但是我收到了以下消息:
错误 CS0201 只能将赋值、调用、递增、递减、等待和新对象表达式用作语句。
错误 CS0029 无法将类型“bool”隐式转换为“System.Exception”
我有一个学校项目,我正在制作一个菜单,用户可以在其中选择他想做的事情。我希望选择变量是 a char,而不是int. 我可以在一个switch案例中添加多个字符吗?我搜索了一个解决方案,但我只在值为 anint而不是 a时找到了一个char。我试过这个,但没有用:
char choice;
scanf("%c", &choice);
switch(choice)
{
case 'S', 's':
// do something
break;
case 'I', 'i':
// do another thing
break;
default:
printf("\nUnknown choice!");
break;
}
Run Code Online (Sandbox Code Playgroud) Visual Studio 2019 建议将我编写的 switch 语句转换为 switch表达式(两者都包含在下面用于上下文)。
对于这样的简单示例,将其编写为表达式是否有任何技术或性能优势?例如,这两个版本的编译方式是否不同?
陈述
switch(reason)
{
case Reasons.Case1: return "string1";
case Reasons.Case2: return "string2";
default: throw new ArgumentException("Invalid argument");
}
Run Code Online (Sandbox Code Playgroud)
表达
return reason switch {
Reasons.Case1 => "string1",
Reasons.Case2 => "string2",
_ => throw new ArgumentException("Invalid argument")
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据另一个字段的一组值来验证对象中的字段。假设我的对象必须字段,field1并且field2. 字段一可以取值 A、B、C、D、E、F、H、I。现在说如果 field1 是任何值;A、B、C、H,则 field2 应为空。我该怎么做呢。我认为它应该有下面的合成器,但我不知道如何完全实现它。
const mySchema = Joi.object({
field1: Joi.string().valid('A','B','C','D','E','F','H','I').required(),
field2: Joi.when('field1', is: <A or B or C or H> then: <field2 should be null> otherwise: Joi.date().required())
});
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用"@hapi/joi": "^17.1.1"
可以这样转换:
int x = 1;
string xString;
switch (x)
{
case 1:
xString = "1";
break;
case 2:
xString = "2";
break;
default:
xString = "default";
break;
}
Console.WriteLine(xString);
Run Code Online (Sandbox Code Playgroud)
进入这个:
int x = 1;
string xString = x switch
{
1 => "1",
2 => "2",
_ => "default",
};
Console.WriteLine(xString);
Run Code Online (Sandbox Code Playgroud)
但是,在多个情况下将 xString 的值设置为相同值而不为每种情况创建 lambda 行的语法是什么?
int x = 1;
string xString;
switch (x)
{
case 1:
xString = "1";
break;
case 2:
case 4:
xString = "even numbers";
break; …Run Code Online (Sandbox Code Playgroud) 可以重构这段代码以避免复制 switch 语句吗?
enum class Animal
{ Cat, Dog, Fish};
float GetMaxSpeed(Animal a)
{
switch (a)
{
case Animal::Cat:
return 30;
case Animal::Dog:
return 40;
case Animal::Fish:
return 15;
}
}
string GetGermanTranslation(Animal a)
{
switch (a)
{
case Animal::Cat:
return "Katze";
case Animal::Dog:
return "Hund";
case Animal::Fish:
return "Fische";
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这是一个玩具示例,我真正的枚举类要大得多。
我有一个函数可以根据输入的值返回响应。代码是这样的
$a = 5;
switch ($a) {
case 1: $b = 1;
break;
case 2 || 3 || 4: $b = 2;
break;
case 5: $b = 3;
break;
}
return $b;
Run Code Online (Sandbox Code Playgroud)
这将返回值2而不是3在代码中可以清楚地看到的值。这是怎么发生的?
使用逗号编写 switch case 语句是否被认为是不好的做法,例如:
switch(name)
{
case 'a', 'A' :
break;
}
Run Code Online (Sandbox Code Playgroud)
而不是
switch(name)
{
case 'a':
case 'A':
break;
}
Run Code Online (Sandbox Code Playgroud)
只是好奇,因为我的代码似乎无论哪种方式都运行良好,但我想养成使用正确/最接受的方式的习惯。