标签: switch-statement

java:切换和反射哪个效率更高?

从 jsp 中我得到一个可以用于开关的字符串

switch(value)
case 0: method0(); break;
case 1: method1(); break;
...
Run Code Online (Sandbox Code Playgroud)

或者反思:

c.getMethod("method"+value, parameter);
...
Run Code Online (Sandbox Code Playgroud)

哪种方法更有效?

java reflection performance switch-statement

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

我可以在 switch 中多次使用相同的 case 值吗

我可以写这样的switch语句吗?

   switch ($mood) {

    case hungry : 
    case sad : 
    echo 'Eat a chocolate';


    case sad : 
    echo 'Call Up your friend';

    }
Run Code Online (Sandbox Code Playgroud)

这是一个好的做法吗?

编辑:根据评论删除了break语句。

php switch-statement

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

默认块位于顶部的 switch 语句的行为

我发现了一种在 Java 中使用switch语句的有趣方法,但我无法捕获所有逻辑。

有人可以帮助深入了解所有细节吗?

这是代码:

private static int counter = 0;

public static Shape randomFactory() {

    int xVal = rand.nextInt(100);
    int yVal = rand.nextInt(100);
    int dim = rand.nextInt(100);

    switch (counter++ % 3) {
    default:
    case 0:
        return new Circle(xVal, yVal, dim);
    case 1:
        return new Square(xVal, yVal, dim);
    case 2:
        return new Line(xVal, yVal, dim);
    }
}
Run Code Online (Sandbox Code Playgroud)

一般来说我理解这个逻辑,

这里的确切含义是default:

switch (counter++ % 3) {
        default:
Run Code Online (Sandbox Code Playgroud)

以及如何switch (counter++ % 3)查找等于大小写?这里没有任何brake介绍。

有什么建议么?

java switch-statement

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

for 循环内的 PHP switch 语句

有一个非常简单的问题这是我的代码:

$imax = 3;
$licenses = array('pub1','pub2','pub3');

for ($i=0; $i<=$imax; $i++) {

$pub = $licenses[$i];

switch ($pub){
case 'pub1': $pubtitle = "Pub title 1";
case 'pub2': $pubtitle = "Pub title 2";
case 'pub3': $pubtitle = "Pub title 3";
}

echo $pubtitle;

}
Run Code Online (Sandbox Code Playgroud)

输出是:

Pub title 3
Pub title 3
Pub title 3
Run Code Online (Sandbox Code Playgroud)

我试图放入$pubtitle一个数组,但它也不起作用:(

php for-loop switch-statement

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

PHP switch 语句在多种情况下具有相同的值

与使用多个 if else 相比,我真的很喜欢 switch 语句的结构。然而有时我想使用 switch 语句并在多种情况下具有相同的值。这可以以某种方式完成吗?

switch($fruit) {
  case 'apple':
  case 'orange':
    // do something for both apples and oranges
    break;

  case: 'apple':
    // do something for only apples
    break;

  case: 'orange':
    // do something for only oranges
    break;
}
Run Code Online (Sandbox Code Playgroud)

我希望我的例子表明我打算做什么......

php switch-statement

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

处理 switch 语句中的组合情况和重复情况

是否可以合并cases共享作业并重复case未共享的作业,还是最好将每个作业分开?


用一个简单的例子来说明..case 0并且180两者都包含w = 330,因此它们已合并;每个分配的值x都不同,因此它们会重复进行x分配。

switch(window.orientation) {
    case   0:
    case 180:
        w = 330;
        //break
    case   0:
        x = '-180px';
        //break
    case -90:
    case  90:
        w = 480;
        x = '0';
        break;
    case 180:
        x = '-80px';
        break;
}
Run Code Online (Sandbox Code Playgroud)

javascript optimization case-statement switch-statement

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

C++ — 使用 getch() 进行箭头键按下检测会莫名其妙地打印一些单词

我有一个简单的程序可以检测用户按下箭头键,但我有两个问题。但首先,这是代码:

#include <iostream>
#include <conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 77
#define KEY_RIGHT 75

using namespace std;

int main()
{
    while(1)
    {
        char c = getch();
        cout << "Hello";
        switch(c) {
        case KEY_UP:
            cout << endl << "Up" << endl;//key up
            break;
        case KEY_DOWN:
            cout << endl << "Down" << endl;   // key down
            break;
        case KEY_LEFT:
            cout << endl << "Right" << endl;  // key right
            break;
        case KEY_RIGHT:
            cout << endl << "Left" << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ getch switch-statement

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

如何在 UIImagePickerControllerMediaType 上使用 switch 语句?

如何使用 switch 语句UIImagePickerControllerMediaType?

以下示例引发奇怪的编译器错误:

“CFString”类型的表达式模式无法与“CFString”类型的值匹配。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    switch info[UIImagePickerControllerMediaType] as! CFString {

    case kUTTypeImage:
        break

    default:
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

switch-statement uiimagepickercontroller swift

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

switch语句中使用break和return哪个更好?

我想知道在 switch 语句中使用break或return时的一些区别。哪个更有效率?有人可以向我解释一下吗?非常感谢。代码如下:

public void foo(){
    int i = 10;
    switch (i) {
        case 1: {
            //code here
            break;  //or return;
        }
        case 2: {
            //code here
            break;  //or return;
        }
        // code  code
        // code  code
        // code  code
        case 10:{
            //code here
            break;  //or return;
        }
        default:{
            //code here
            break;  //or return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java switch-statement memory-efficient

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

带大括号的 case 语句

为什么case语句允许在大括号内声明,但不允许在没有大括号的情况下声明?

比如下面的就不行

switch (op->name) {
    case 0:
        int a = 2;
    case 1:  
        int b = 3;
}
Run Code Online (Sandbox Code Playgroud)

但以下是可以的:

switch (op->name) {
    case 0: 
       {int a = 2;}
    case 1:  
       {int b = 3;}
}
Run Code Online (Sandbox Code Playgroud)

大括号解决了什么问题,没有它们,声明对编译器来说是不明确的?对我(C 初学者)来说,似乎每个 case 语句都应该有一个隐含的括号,直到下一个 case/default/end-of-switch,但这显然是错误的!

c switch-statement

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