标签: switch-statement

为什么我无法在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
查看次数

从main.xml布局切换到另一个布局

我在这里有一个基本的Android问题:

我有一个main.xml布局,在应用程序启动时加载.这个页面有一个菜单按钮,我想(当点击时)将用户发送到另一个布局(about.xml).

我怀疑这是对的.单击此命令时,将启动:

setContentView(R.layout.about);
Run Code Online (Sandbox Code Playgroud)

它似乎工作,我确实看到about.xml页面,但是当我点击Android设备上的BACK按钮时,我无法导航回main.xml布局,应用程序才关闭.

我怀疑这是在xml布局文件之间导航的正确方法.你可以帮忙或指点一个网页,为像我这样的计算机程序员初学者解决这个问题吗?

非常感谢你,

拍

编辑:感谢您帮助我找到正确方向的所有答案.为了帮助像我这样的未来的noob程序员了解活动,这里有一个很棒的简单教程,我在网上找到它为我们的初学者绘制出来!

http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/

xml layout android switch-statement

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

有没有办法简化这种案例陈述?

我有这个PHP案例声明

switch ($parts[count($parts) - 1]) {
    case 'restaurant_pos':
        include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
        break;
    case 'retail_pos':
    include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
        break;  
    .....

}
Run Code Online (Sandbox Code Playgroud)

哪个效果很好,但我有很多文件(比如190),我很想知道是否有办法让这个案例陈述很多可以用于任何事情,所以我不需要做190个案例.我想我可以在案件中使用condtion并且可能看看该文件是否存在,如果是,那么显示,如果没有那么可能是404页但我不确定这样做的好方法...任何想法都会有帮助很多

php directory loops switch-statement

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

Java开关:变量声明和范围

Java编译器如何处理以下交换机块?'b'变量的范围是什么?

请注意,'b'变量仅在switch语句的第一个分支中声明.尝试在第二个分支中声明它也会导致"重复的局部变量"编译错误.

    int a = 3;
    switch( a ) {
    case 0:
        int b = 1;
        System.out.println("case 0: b = " + b);
        break;
    case 1:
        // the following line does not compile: b may not have been initialized
        // System.out.println("case 1 before: b = " + b);
        b = 2;
        System.out.println("case 1 after: b = " + b);
        break;
    default:
        b = 7;
        System.out.println("default: b = " + b);
    }
Run Code Online (Sandbox Code Playgroud)

注意:上面的代码用java 1.6编译器编译.

java scope initialization switch-statement variable-declaration

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

具有大量案例的switch语句

如果switch超过5000,会发生什么case.有什么缺点以及如何用更快的东西替换它?

注意:我不希望使用数组来存储案例,因为它是相同的.

c c++ switch-statement

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

以下两行java代码有什么区别?

请原谅我的无知.我无法理解以下看似相似的代码行之间的区别.

  1. final int num1 = 1;
  2. final int num2; num2 = 2;

什么使num2不符合开关案件的条件不变?

    switch (expression) {
        case num1:
            System.out.println("Case A");
        case num2:
            System.out.println("Case B");
    }
Run Code Online (Sandbox Code Playgroud)

java switch-statement

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

如何在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万
查看次数