echo $state; //debug
switch($state){
case "Sleeping":
$label = "Is Sleeping";
case "Running":
$label = "Is Running";
default:
$label = "Could not retrieve state";
}
Run Code Online (Sandbox Code Playgroud)
$state从SQL数据库(Enum类型)填充,回显消息打印“Sleeping”,但$label填充默认值
这就是我的意思。
enum Device {
case iPhone_13Pro, iPhone_12Pro
case iPadPro_129
case iPadPro_11
}
// Hypothetical Scenario
enum Device_Group {
case iPhones
case iPads
}
Run Code Online (Sandbox Code Playgroud)
有没有办法按照上面的方法来表示像这样的一组特定情况(它可以是另一个枚举或同一枚举中的不同情况 - 这样我就可以执行以下操作?
// DO THIS
switch device {
case iPhones:
print("These are iPhones")
case iPads:
print("These are iPads")
}
// INSTEAD OF THIS
switch device {
case .iPhone_13Pro, .iPhone_12Pro:
print("These are iPhones")
case .iPadPro_129, .iPadPro_11:
print("These are iPads")
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是一个奇怪的问题,但我发现在使用 switch 语句时对多个案例进行分组有一定的好处。对此有任何建议将不胜感激。提前致谢。
是否可以在 switch case 中使用 strings.Contains ?就像是:
func function(str string){
switch str {
case "str1":
...
case strings.Contains("test"):
...
default:
...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:它是一个例子,这不是我需要它的真实情况。我正在过滤许多结果,我想要包含 X 的所有结果,并且我也有需要完全匹配的案例。
int main(){
char c='a';
switch(c){
case 'a' && 1: printf("Hello");
case 'b' && 1: printf("hey");
break;
default : printf("Goodbye");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译此代码时,结果是“编译错误”,这(根据我的说法)是因为在内部两个表达式都是 true,因此对于我们为“c”采用的任何字符,两种情况下的常量表达式都将始终为 true 。
但现在出现的疑问是我无法理解,内部如何解释代码以及编译器实际上如何解释该代码?
我记得从大学回来时使用带有'二分搜索'或'二进制切换'的开关.类似的东西,我的谷歌foo今天破了.无论如何,它会像这样下降:你定义了一个可能的选项数组(通常是字符串),一些魔法发生,并且数组中的那些选项成为切换中发生的情况.我确实记得他们必须按字母顺序才能工作.你能帮我记住魔法吗?即使是我应该关注的名字也会很棒.
有没有办法可以为每个"Case"执行相同的代码行,但只需要输入一次而不是为所有Case指定相同的代码?
switch (SomeTest)
{
case "test1":
{
// Do something for test 1
break;
}
case "test2":
{
// Do something for test 2
break;
}
case "test3":
{
// Do something for test 3
break;
}
// =====> Then do something generic here for example if case is test1, test2 or test3
}
Run Code Online (Sandbox Code Playgroud) Cyclomatic Complexity提供了一个粗略的指标,用于了解给定函数的难易程度,或者包含错误的可能性.在我已经阅读过的实现中,通常所有基本的控制流构造(if,case,while,for等)都会使函数的复杂度增加1.在我看来,圈复杂度旨在确定"通过程序的源代码的线性独立路径的数量",虚函数调用也应该增加函数的圈复杂度,因为在运行时将调用哪个实现的模糊性(调用在路径中创建另一个分支)执行).
但是,如果它包含一个等效的switch语句(每个'case'关键字一个点,对于实现所讨论的虚函数的层次结构中的每个类都有一个case关键字),那么惩罚函数的量就会过于苛刻,因为虚函数调用通常被认为是更好的编程实践.
虚拟函数调用的圈复杂度的成本应该是多少?我不确定我的推理是否反对将圈复杂度作为衡量虚拟函数或不同东西的度量的指标.
编辑:在人们的回答之后,我意识到它不应该增加圈复杂性,因为我们可以认为虚函数调用等同于对包含大量switch语句的全局函数的调用.即使该函数得分较差,它只在程序中存在一次,而用switch语句直接替换每个虚函数调用会导致成本很多次.
language-agnostic virtual-functions cyclomatic-complexity code-metrics switch-statement
我对PIC汇编程序中的银行切换感到困惑......这适用于在usart上放置'Q':
bsf PORTB,1 ;Set Transmit DIR (PORTB (0x6) not mirrored in other banks)
movlw 'Q' ;'Q' to work reg
movwf TXREG ;work reg to TXREG (TXREG (0x19) not mirrored in other banks)
clrwdt ;Clear watchdog
btfss TXSTA,TRMT ;Wait until 'Q' is shifted (TXSTA is 0x18, not mirrored)
goto $-2
bcf PORTB,1 ;Set Recive DIR
Run Code Online (Sandbox Code Playgroud)
这同样有效:
BCF 0x3, 0x5 ;Switch to bank 0
BCF 0x3, 0x6
bsf PORTB,1 ;Set Transmit DIR
movlw 'Q' ;'Q' to work reg
movwf TXREG ;work reg …Run Code Online (Sandbox Code Playgroud) 我正在使用visual studio 2005.我记得在脑海中有一种方法可以输出构建给定项目所需的时间.
谁能记住那个开关是什么?
谢谢Rich
compiler-construction time visual-studio-2005 build switch-statement
我在C#/ Winforms中有一个应用程序,它允许用户在网格上放置对象以创建游戏关卡.它有几个工具用于放置瓷砖/灯/门/实体等.目前我只使用枚举来存储当前选定的工具,并有一个switch语句来运行每个工具代码.因为我一直在为应用程序添加更多工具,所以它开始像意大利面一样,有很多重复的代码.
这是我的编辑器类中的鼠标按下功能的缩减版本:
public void OnEditorViewMouseDown(Point mousePos)
{
// Check if the click is out of bounds.
if (IsLocationOOB(mousePos)) return;
if (CurrentTool == ToolType.GroundTile)
{
// Allow drags along whole tiles only.
m_DragManager.DragType = DragManager.DragTypeEnum.Tile;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.WallTile)
{
// Allow drags along grid edges only.
m_DragManager.DragType = DragManager.DragTypeEnum.Edge;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.PostTile)
{
// Allow drags along grid points only.
m_DragManager.DragType = DragManager.DragTypeEnum.Point;
m_DragManager.StartDrag(mousePos);
}
else if (CurrentTool == ToolType.AreaLight)
{
// …Run Code Online (Sandbox Code Playgroud)