标签: switch-statement

C#switch variable initialization:为什么这段代码不会导致编译错误或运行时错误?

...
case 1:
   string x = "SomeString";
   ...
   break;
case 2:
   x = "SomeOtherString";
   ...
   break;
...
Run Code Online (Sandbox Code Playgroud)


有没有我不了解C#中的switch语句?为什么在使用案例2时这不是错误?
编辑:此代码有效,不会抛出错误.

c# switch-statement

8
推荐指数
2
解决办法
2418
查看次数

是否可以在Switch语句中执行以下操作 - C++?

我是第二个OOP课程的编程学生,我有一个简单的问题,我无法在互联网上找到答案,如果它在那里,我道歉.

我的问题是:

是否有可能在switch语句中有布尔条件?

例:

switch(userInputtedInt)
{
    case >= someNum && <= someOtherNum
    break;
    // Is this possible?
}
Run Code Online (Sandbox Code Playgroud)

c++ switch-statement

8
推荐指数
3
解决办法
2万
查看次数

开关盒中的0?

对不起可能是愚蠢的问题,但是当我有一些值为0(零)的int变量时,我遇到了一个巨大的问题.

            switch ($starost_vozila){
                case (0):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 485;
                        break;
                        case ("motor2"):
                            $eko_taksa = 243;
                        break;
                        case ("motor3"):
                            $eko_taksa = 121;
                        break;
                        case ("motor4"):
                            $eko_taksa = 194;
                        break;
                    }
                break;
                case ($starost_vozila < 6):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 485;
                        break;
                        case ("motor2"):
                            $eko_taksa = 243;
                        break;
                        case ("motor3"):
                            $eko_taksa = 121;
                        break;
                        case ("motor4"):
                            $eko_taksa = 194;
                        break;
                    }
                break;
                case ($starost_vozila > 5 && $starost_vozila < 11):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 667; …
Run Code Online (Sandbox Code Playgroud)

php switch-statement

8
推荐指数
1
解决办法
7776
查看次数

为什么我无法在switch-case块中实例化对象

我的代码有3个类n_hexa,n_octa,n_bin.代码在这里

switch(choice)
{
case 1: cin>>n; 
 n_hexa nx(n);
        break;
case 2: cin>>n; 
 n_octa no(n);
        break;
case 3: cin>>n;
 n_bin nb(n);
        break;
}
Run Code Online (Sandbox Code Playgroud)

在编译它时,为n_octa行提供消息" n_hexa的初始化 "

c++ block instantiation switch-statement

8
推荐指数
1
解决办法
2344
查看次数

为什么我不能在switch语句中使用NSInteger?

为什么这不起作用:

NSInteger sectionLocation = 0;
NSInteger sectionTitles = 1;
NSInteger sectionNotifications = 2;

switch (section) {
    case sectionLocation:
        //
        break;
    case sectionTitles:
        //
        break;
    case sectionNotifications:
        // 
        break;
    default:
        //
}
Run Code Online (Sandbox Code Playgroud)

我得到这个编译错误:

错误:case标签不会减少为整数常量

是不是可以像这样使用NSInteger?如果是这样,是否有另一种方法在switch语句中使用变量作为案例?sectionLocation等具有可变值.

iphone cocoa-touch objective-c switch-statement nsinteger

8
推荐指数
1
解决办法
6320
查看次数

如何避免长切换语句?C++

我正在为我班上的"字典"工作.我有一个调用的int数组NumOfWordsInFile[],NumOfWordsInFile[0]对应于A.txt中有多少个单词,NumOfWordsInFile[25]对应于Z.txt

就像现在一样,我有一个巨大的开关,用于26种不同的字母条件.我有一个叫做的函数AddWord(string word).AddWord获取传递给它的单词的第一个字母,并将其插入相应的.txt文件中.现在这是问题所在.每次在A.txt中添加一个单词时,我必须增加NumOfWordsInFile[0]1.我能想到的唯一方法是使用这些巨大的开关.我还有一个deleteWord函数,NumOfWordsInFile[]如果删除该单词,则相反递减.现在我不想要两个26箱开关,但问题是我不知道怎么做.现在我可以为删除功能做同样的事情,但我真的不希望有更多的代码行来完成.有一个更好的方法吗?

AddWord功能中的开关示例:

case 'w':
    if (numOfWordsInFile[22] < maxWordsPerFile) {
        fout.open(fileName.data(), ios::app);
        fout << word << " " << endl;
        numOfWordsInFile[22]++;
        if (totalWordsInDict < maxWordsInDict) {
            totalWordsInDict++;
        }
        return(Dictionary::success);
    } else {
        return(Dictionary::failure);
    }

case 'x':
    if (numOfWordsInFile[23] < maxWordsPerFile) {
        fout.open(fileName.data(),ios::app);
        fout << word << " " << endl;
        numOfWordsInFile[23]++;
        if (totalWordsInDict < maxWordsInDict) {
            totalWordsInDict++;
        }
        return(Dictionary::success);
    } else {
        return(Dictionary::failure);
    }
Run Code Online (Sandbox Code Playgroud)

删除功能.

bool …
Run Code Online (Sandbox Code Playgroud)

c++ performance switch-statement

8
推荐指数
4
解决办法
2080
查看次数

Ruby:有没有正确的方法来缩进case语句?

许多博客和教程在实现案例陈述时都有不同的缩进.每个都有不同的缩进参考'when'行.使用案例循环时是否有缩进的最佳做法?

ruby indentation switch-statement

8
推荐指数
1
解决办法
2573
查看次数

如何在Swift的范围内使用负数?

我有以下代码:

switch self.score
        {
        case 1:
            self.score = self.score - 2
        case -10...-10000: // ! Expected expression after unary operator
            println("lowest score")
            self.score = -10
        default:
            self.score = self.score - 1
        }
Run Code Online (Sandbox Code Playgroud)

我也试过了case -1000...-10:.两者都得到同样的错误! Expected expression after unary operator.

我真正想做的是case <= -10:,但如果没有得到这个错误,我无法弄清楚如何做到这一点Unary operator cannot be separated from its operand.

我不明白的是什么?

operators switch-statement negative-number swift

8
推荐指数
1
解决办法
4514
查看次数

如何在Pharo Smalltalk中实现开关

我正在尝试解析一个命令和一个int来在板上做一个"乌龟"移动.我有点不知所措,因为它没有抛出异常,我甚至无法弄清楚如何在没有它的情况下打开调试器.

我的代码:

"only should begin parsing on new line"
endsWithNewLine:= aTurtleProgram endsWith: String cr.
endsWithNewLine ifTrue:[
    "splits commands based on new lines"
    commands := aTurtleProgram splitOn: String cr.
    commands do:[:com |
        "should split into command and value for command"
        i := com splitOn: ' '.
        "command"
        bo := ci at: 1.
        val := ci at: 2.
        "value for command"
        valInt := val asInteger.
        ^ bo = 'down' "attempted switch"
            ifTrue: [ turtle down: valInt ]
            ifFalse: [
              bo = 'left' …
Run Code Online (Sandbox Code Playgroud)

smalltalk switch-statement pharo

8
推荐指数
2
解决办法
1168
查看次数

带有逻辑和的打字稿开关案例

我正在尝试编写一个switch语句,但它似乎没有按我的意愿工作.

getExerciseDescription(exerciseId, intensity_level){

    alert(exerciseId + " " + intensity_level)

    switch (exerciseId && intensity_level) {
        case (1 && 1):
        this.header="Exercise 1 Level 1";
        this.instructions="Exercise 1 Level 1";
        break;
        case (1 && 2):
        this.header="Exercise 1 Level 2";
        this.instructions="Exercise 1 Level 2";
        break;  


        case (2 && 1):
        this.header="Exercise 2 Level 1";
        this.instructions="Exercise 2 Level 1";
        break;  
        case (2 && 2):
        this.header="Exercise 2 Level 2";
        this.instructions="Exercise 2 Level 2";
        break;

        default:
        this.header="Default";
        this.instructions="Default";
        break;
    }

    return new Popup(this.header, this.instructions);
} 
Run Code Online (Sandbox Code Playgroud)

警报给出2和1,但返回值为(1 && 1).为什么会这样?我怎样才能解决这个问题?

javascript switch-statement typescript

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