标签: switch-statement

使用枚举的JavaScript切换案例

我有一个"枚举"声明如下:

var PlaceType = {
        PASSABLE_TERRAIN: 1,
        IMPASSABLE_TERRAIN: 0,
        SOMEWHAT_PASSABLE_TERRAIN: 2,
        PATH: 3
    };
Run Code Online (Sandbox Code Playgroud)

和一个声明如下的函数:

setPlaceType(placeType) {
        this.clear = false;
        this.placeType = placeType;

        alert("before switch "+(PlaceType.SOMEWHAT_PASSABLE_TERRAIN==this.placeType));
        switch(this.placeType) {
        case PlaceType.PASSABLE_TERRAIN: {
            alert("Case PASSABLE");
            break;
        }
        case PlaceType.IMPASSABLE_TERRAIN: {
            alert("Case IMPASSABLE");
            break;
        }
        case PlaceType.SOMEWHAT_PASSABLE_TERRAIN: {
            alert("Case SOMEWHAT_PASSABLE");
            break;
        }
        case PlaceType.PATH: {
            alert("Case PATH");
            break;
        }
        default: {
            alert("case default");
        }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我这样称呼它:

setPlaceType(1);
Run Code Online (Sandbox Code Playgroud)

我收到以下警告:"在切换为true之前","case default"

如果我这样称呼它:

setPlaceType(2);
Run Code Online (Sandbox Code Playgroud)

我收到以下警告:"在切换错误之前","大小写默认"

换句话说,使用正确的参数调用函数,当执行(在我看来)与开关相同的比较时,通过"=="我得到正确的行为,但是开关永远不会匹配值在适当的情况下.有人知道为什么吗?

javascript enums case switch-statement

6
推荐指数
1
解决办法
1万
查看次数

switch case 不带变量

是否可以编写 switch case 代码而无需在 switch 中指定变量?假设我有两个变量 int x 和 int y。我想根据 x 和 y 的不同组合有条件地运行代码。例如

switch
{
    case (x==1 && y==1): //do something.
        break;
    case (x==3 && y==-1): //do something.
        break;
    case (x==0 && y==3): //do something.
        break;
    default: //do something.
        break;
}
Run Code Online (Sandbox Code Playgroud)

这段代码给我错误。

java case switch-statement

6
推荐指数
1
解决办法
5906
查看次数

切换两个INT变量的情况

请考虑以下代码:

if (xPoint > 0 && yPoint > 0) {
    m_navigations = Directions.SouthEast;
}
else if (xPoint > 0 && yPoint < 0) {
    m_navigations = Directions.NorthEast;
}
else if (xPoint < 0 && yPoint > 0) {
    m_navigations = Directions.SouthWest;
}
else if (xPoint < 0 && yPoint < 0) {
    m_navigations = Directions.NorthWest;
}
else if (xPoint == 0 && yPoint < 0) {
    m_navigations = Directions.North;
}
else if (xPoint == 0 && yPoint > 0) { …
Run Code Online (Sandbox Code Playgroud)

java switch-statement

6
推荐指数
1
解决办法
5311
查看次数

切换案例的这种语法在C中意味着什么?

我看到了一些像这样的c代码:int check = 10:

switch(check) {
            case 1...9: printf("It is 2 to 9");break;
            case 10: printf("It is 10");break;
} 
Run Code Online (Sandbox Code Playgroud)

这case 1...9:是什么意思?它是标准的吗?

c switch-statement

6
推荐指数
1
解决办法
646
查看次数

Java错误:需要常量字符串表达式

我有2个Java类:

public abstract class IconNames {
/**
 *
 */
public static final String ButtonFett = java.util.ResourceBundle.getBundle("recources/buttonproperties").getString("fett");
}
Run Code Online (Sandbox Code Playgroud)

和

public class EditorPanelActionListener implements ActionListener{
.
.
.
String buttonText = e.getActionCommand();
switch(buttonText)
    {
        case IconNames.ButtonFett: //Error: constant string expression required
            replace(XmlTags.BOLD);
            break;
    }
 .
 .
 .
 }
Run Code Online (Sandbox Code Playgroud)

EditorPanelActionListener触发错误"需要常量字符串表达式",问题是什么?

谢谢!

java constants switch-statement

6
推荐指数
1
解决办法
3184
查看次数

Java - 嵌套While循环

Greetings Stack Overflow用户,今晚我来找你,帮助我创建一个Java程序.我对Java比较新,所以请原谅我对这个主题的无知.我制作了一个Java程序,它是一个"Rock""Paper""Scissors"游戏,其中一个语句中似乎有错误.

import java.util.Scanner;

public class TheAntlers {
public static void main(String[] args) {

    int playerHumanWins = 0;
    int playerComputerWins = 0;
    int numberOfTies = 0;
    int computerResult;

    Scanner input = new Scanner(System.in);

    while(true) {

        String startGame;
        String playerHuman;
        String playerComputer = " ";

        System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
        startGame = input.nextLine();

        startGame = startGame.toUpperCase();

        if(startGame.equals("N")) {
            System.out.println("NO!");
            break;
        }
        else if(! startGame.equals("Y")) {
            startGame = startGame.toLowerCase();
            System.out.println("Sorry, " + startGame + " is …
Run Code Online (Sandbox Code Playgroud)

java while-loop switch-statement

6
推荐指数
1
解决办法
5342
查看次数

Swift WKWebView 在 didFailProvisionalNavigation 上崩溃

我们遇到了间歇性崩溃(有时会在某些设备上发生),我们很难确定该崩溃,并且无法按需重现。这与 Swift 3 和 WKWebView 组件有关,特别是当尝试通过 switch 语句获取错误代码时,其回调协议崩溃。见下文:

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {

  if let err = error as? URLError {

    switch(err.code) {  //  Exception occurs on this line
    case .cancelled:
      Hint(hide: true)

    case .cannotFindHost:
      Hint(hide: false, hint:.CannotFindHost)

    case .notConnectedToInternet:
      Hint(hide: false, hint: .NoInternet)

    case .resourceUnavailable:
      Hint(hide: false)

    case .timedOut:
      Hint(hide: false)

    default:
      Hint(hide: false)
      print("error code: " + String(describing: err.code) + "  does not fall under known failures")
    }
  }
}

func Hint(hide: …
Run Code Online (Sandbox Code Playgroud)

webkit exception switch-statement ios swift

6
推荐指数
1
解决办法
7199
查看次数

在 TypeScript 中推断泛型函数中的映射类型

以这篇 StackOverflow 文章为例,我实现了一个类型化事件系统。简化后,它看起来像:

interface MyTypeMap {
  FOO: string;
  BAR: number;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用此地图创建一个事件处理程序:

function handleEvent<T extends keyof MyTypeMap>(eventKey: T, eventMsg: MyTypeMap[T]) {
  switch(eventKey) {
    case('FOO'):
      // TS believes that eventKey is of type 'never'
      // TS believes that eventMsg is 'string|number'
      break;
    case('BAR'):
      // TS believes that eventKey is of type 'never'
      // TS believes that eventMsg is 'string|number'
      break;
  }
}
Run Code Online (Sandbox Code Playgroud)

当外部调用此函数时,TypeScript 的行为与预期一致。例如:

// These work as desired
handleEvent('FOO', 'asdf');
handleEvent('BAR', 5);

// These throw compile errors as …
Run Code Online (Sandbox Code Playgroud)

generics inference typing switch-statement typescript

6
推荐指数
1
解决办法
5130
查看次数

Java 枚举中的强制切换默认值

假设我创建了一个枚举,例如下面的:

public enum MyEnum {
    A,
    B,
    C;
}
Run Code Online (Sandbox Code Playgroud)

并且有一个 switch 语句(如下所示),其中每个枚举值都有一个 case 语句,但我还添加了一个默认值来抛出 IllegalArgumentException。(如果添加了一个新的枚举值,那么如果新值被传递,那么有人更有可能会注意到该 case 语句出了问题。我的想法是异常比日志或尝试处理新值更引人注目案件。)

switch(myEnum) {
    case A:
        //do something
        break;
    case B:
        //do something
        break;
    case C:
        //do something
        break;
    default:
        throw new IllegalArgumentException("Unrecognized enum type!");
}
Run Code Online (Sandbox Code Playgroud)

除非我遗漏了某些东西,否则从理论上讲,如果不添加新的枚举值或更改 switch 语句,默认值应该是无法访问的。

所以我想知道的是,如果我正在编写代码覆盖率的 JUnit 测试,是否有办法强制切换到默认语句而不更新应用程序代码?

java enums switch-statement

6
推荐指数
1
解决办法
3030
查看次数

在 `switch` 语句中正确使用 C++20 `[likely]]`/`[[unlikely]]`

C++20 具有指导代码生成的方便[[likely]]/[[unlikely]]属性。例如,您可以指定一个分支可能被以下人员采用:

if (b) [[likely]] { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

同样,可以在switchstatements 中使用这些属性。. . 不知何故? 文档建议使用以下示例(稍微格式化):

if (b) [[likely]] { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

其含义显然是[[likely]]/[[unlikely]]在case语句之前。互联网似乎几乎普遍宣传这种用法。

但是,请考虑以下类似的代码(我所做的只是将 the[[likely]]移到 other case):

switch (i) {
    case 1:
        [[fallthrough]];
    [[likely]] case 2:
        return 1;
}
Run Code Online (Sandbox Code Playgroud)

这无法在 clang 上编译!虽然这可能与编译器错误有关[[fallthrough]],但它让我查看了标准。在相关的标准有如下的例子(见§VII):

鼓励实现针对正在执行的情况进行优化(例如,以下代码中的值为 1):

switch (i) {
    [[likely]] case 1:
        [[fallthrough]];
    case 2:
        return 1;
}
Run Code Online (Sandbox Code Playgroud)

也就是说,该属性出现在案例标签之后,而不是之前。

所以 。. . …

c++ attributes switch-statement branch-prediction c++20

6
推荐指数
1
解决办法
187
查看次数