标签: switch-statement

如何在 Mustache 模板中使用 switch case?

我在 Core PHP 中使用 Mustache 模板将 PHP 页面转换为模板。现在我想在模板中使用 switch case,例如:

<?php
    switch ($gift_card['FlagStatus']) {
        case 'P': 
            echo "Pending";
            break;
        case 'A':
            echo "Active";
            break;
        case 'I':
            echo "Inactive";
            break;
    }
Run Code Online (Sandbox Code Playgroud)

?>

类似的 Mustache 翻译应该是什么?提前致谢

php switch-statement mustache

4
推荐指数
1
解决办法
4679
查看次数

当换行符位于格式字符串中时 scanf 的行为

下面是我的代码的副本。基本上,我需要创建一个程序,根据“工资代码”(例如工人的职位)计算工资。我已经创建了我的 switch 语句,除了我输入第一个付款码时的一开始,一切正常。我输入第一个付款代码,它会转到下一行,将其留空。我输入另一个数字,它会按照预期的方式运行该数字和前一个数字。然后,一切正常。我确信这是一个简单的解决方案,但对我来说有点棘手。另外,我不确定应该如何在这里格式化我的代码,使其看起来像在服务器上一样,所以我对它看起来令人困惑的方式表示歉意。

#include <stdio.h> //precompiled header
int main(void)
{
    //declare variables
    unsigned int counter = 0; //counters to calculate amount of workers paid
    unsigned int counter2 = 0;
    unsigned int counter3 = 0;
    unsigned int counter4 = 0;

    int paycode;
    float overtime; //amount of overtime hours
    float salary; //weekly salary
    float hoursWorked;
    float hourlyRate;
    float grossWeeklySales; //weekly sales for commissioned workers
    int itemsProduced;
    float fixedAmount; //money given per item produced

    //prompt for input
    printf("Please enter the employee's paycode.\n");
    printf("1: …
Run Code Online (Sandbox Code Playgroud)

c counter while-loop switch-statement

4
推荐指数
1
解决办法
1193
查看次数

具有相同名称的静态变量的 switch case 的单独作用域

更新:我现在重新测试了简化的测试用例和恢复共享名称的完整代码,并且它可以正常工作。我的代码中的其他地方可能还有一些其他错误,现在已修复。抱歉浪费了您的时间和精力;将来永远不会在没有工作测试用例的情况下发布。我的罪过。


我有一个我反复调用的 C++ 函数。它有以下片段

     switch(c)
     {
     case 1:
        {
          static int i = 0;
          if ( ... )  { i = 0; }
          .... 
          break;
        }
     case 2:
        {
          static int i = 0;
          if ( ... )  { i = 0; }
          .... 
          break;
        }
     case 3:
        {
          static int i = 0;
          if ( ... )  { i = 0; }
          .... 
          break;
        }
     }
Run Code Online (Sandbox Code Playgroud)

这个想法是它必须记住每个案例的状态,有时它必须重置它。

它没有正常工作。当我调试它(​​MSVC++ 2010 Express Edition)时,我注意到每个i人的行为都不是独立的,他们的值似乎在自己改变;此外,当重设条件被击中时,相应的if输入 OK 但 …

c c++ static scope switch-statement

4
推荐指数
1
解决办法
1225
查看次数

Switch Case 失败场景

所以我遇到了一个有竞争力的问题(询问输出)如下:

#include <stdio.h>
int main()
{
    int i = 0;
    for(i = 0; i < 20; i++)
    {
        switch(i)
        {
            case 0: i+=5;
            case 1: i+=2;
            case 5: i+=5;
            default: i+= 4;
            break;
        }
        printf("%d ", i);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是16, 21。虽然我知道 switch case 是如何工作的,但我无法解释这个秋天是如何工作的。为什么要添加默认值?K&R C 书不是说只有在没有任何情况匹配时才执行默认值吗?
谢谢。

c switch-statement fall-through

4
推荐指数
1
解决办法
2851
查看次数

如何在 switch case 中使用枚举类

出现错误 - case 表达式必须是常量表达式当我尝试在 switch case 语句中使用 enum 类时,:

我的枚举类是,

public enum TestEnumClass {

    TEST1("TEST1"),
    TEST2("TEST2"),
    TEST3("TEST3");

    private String enumConstant;

    private TestEnumClass(String algoConstant) {
        this.enumConstant = algoConstant;
    }

    public String getEnumConstant() {
        return enumConstant;
    } 
}
Run Code Online (Sandbox Code Playgroud)

我试图在另一个类文件中使用 enum TestEnumClass 如下,

public class TestIndexOf {

    public static void main(String[] args) {


        String str = args[0];

        switch(str){

        case TestEnumClass.Test1.getEnumConstant() : System.out.println("test1"); break;

        case TestEnumClass.Test2.getEnumConstant() : System.out.println("test2"); break;

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给了我编译时错误:

case 表达式必须是常量表达式

请建议我,我哪里出错了。

java enums compiler-errors switch-statement java-7

4
推荐指数
1
解决办法
4611
查看次数

为什么我不能在 switch 中使用非整数类型

因为如果我定义operator==然后比较将是可能的:

class My
{
    int x;
    // ...
public:
    My(int);

    bool operator==(const My & y);
        // ...
};

//...

My one = 1;
switch (one)
{
    case 1 : // anything

    default : // ...
}
Run Code Online (Sandbox Code Playgroud)

但它仅适用于整数类型。为什么?

c++ class switch-statement implicit-conversion

4
推荐指数
1
解决办法
820
查看次数

获取一串对象标签

我有一个与标签相关的疑问。例如,如果我有一个带有“ButtonTag”标签的按钮。Tag 是一个对象,但我想捕获一个字符串“ButtonTag”并在开关内使用。总结一下,获取一个String of Object标签并在Switch中使用。有可能吗?

public void ArtGeneralButton(View view){
         selsub = view.getId();
         tagsub = view.getTag(); \\ Object -> String How???
        // String myString = getString(null,tagsub,);
         UpdateAnsList myUpdate = new UpdateAnsList(this);      
         myUpdate.StartUpdateAnsList(selsub,tagsub);

    }
Run Code Online (Sandbox Code Playgroud)

tags android object switch-statement

4
推荐指数
1
解决办法
3370
查看次数

在 switch 语句中检查 Dart 运行时类型

我在尝试检查 Dart 中某些对象的运行时类型时发现了一些奇怪的行为。我们举一个简单的例子:

main(List<String> args) {
  List<Map> l1 = new List<Map>();
  l1.add(new Map<String, int>());
  List<int> l2 = ['Start', 'Stop'];
  checkType(l1.runtimeType);
  checkType(l1[0].runtimeType);
  checkType(l2.runtimeType);
}

checkType(Type type) {

  switch(type) {
    case List:
      print('it is a List!');
      break;
    case Map:
      print('it is a List!');
      break;
    default:
      print('Unmanaged type $type');
      break;
  }
}
Run Code Online (Sandbox Code Playgroud)

这个程序有以下输出:

非托管类型List<Map> 非托管类型_InternalLinkedHashMap<String, int> 非托管类型List

第一种情况无法在 switch 语句中检查,因为如果我尝试设置“ List<Map>”情况,我会收到错误:在常量表达式中,此运算符的操作数必须是“num”类型第二个无法匹配,因为使用case 中的 _InternalLinkedHashMap 得到以下错误:Case expression must be constant。

在最后一种情况下,我将 List 定义为一个整数列表 ( List<int>),但系统忽略了它并将其视为一个简单的 List。我认为这是一种误导,应该禁止这样的声明。

任何帮助/建议?

types switch-statement dart

4
推荐指数
1
解决办法
2000
查看次数

C# Switch If 条件来扩展 switch case

我有一些数据需要转换,为此我需要一个超过 50 个案例的切换条件,我需要 3 次相同的案例,但在第三次我需要 50 个案例和一些更多的案例,我不想写两次相同的代码。也许有可能做这样的事情。

switch (example)
{
    case "1":
        //do something
    case "2":
        //do something
    case "50":
        //do something
    //now maybe something like this
    if (condition == true)
    {
        case "1":
            //do something else than above at case "1", and so on 
            //now its i little bit illogical, but i neet to do the first 50 cases and then
            //use the cases 1 to 50 again but with other actions 
    }
}
Run Code Online (Sandbox Code Playgroud)

c# if-statement case switch-statement

4
推荐指数
1
解决办法
787
查看次数

Is it possible to check for null values in switch case statement in C#

I have an object with the property Value. Value is a nullable number. I have some scenarios for the value of value. Usually, for null cases, I use the default case but this time, it is not correct logically. I want to do "X" in case of 100, "Y" in case of no value (the value is null), otherwise, I want to do "Z".

switch (p.Value)
{
    case 100:
        // DO X
        break;
    default:
        // Do Z
        break;
} …
Run Code Online (Sandbox Code Playgroud)

c# switch-statement

4
推荐指数
1
解决办法
87
查看次数