从 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)
哪种方法更有效?
我可以写这样的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语句。
我发现了一种在 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介绍。
有什么建议么?
有一个非常简单的问题这是我的代码:
$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一个数组,但它也不起作用:(
与使用多个 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)
我希望我的例子表明我打算做什么......
是否可以合并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) 我有一个简单的程序可以检测用户按下箭头键,但我有两个问题。但首先,这是代码:
#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) 如何使用 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 语句中使用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) 为什么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,但这显然是错误的!
switch-statement ×10
java ×3
php ×3
c ×1
c++ ×1
for-loop ×1
getch ×1
javascript ×1
optimization ×1
performance ×1
reflection ×1
swift ×1