我有以下代码:
package main
import (
"fmt"
)
func main() {
switch {
case 1 == 1:
fmt.Println("1 == 1")
fallthrough
case 2 == 1:
fmt.Println("2 == 1")
}
}
Run Code Online (Sandbox Code Playgroud)
在 go 操场上打印两行 -请参阅此处的示例。我本来希望 fallthrough 语句包括对下一个case语句的评估,但情况似乎并非如此。
当然,我总是可以使用一堆if语句,所以这不是一个真正的障碍,但我很好奇这里的意图是什么,因为在我看来这不是一个明显的结果。
有人愿意解释吗?例如:在这段代码中,我怎样才能让第 1 和第 3 种情况执行?
我接到了一项任务,我自己几乎已经解决了,但需要进行一些小的更正,我需要有人帮助我指出在哪里对我的代码进行一些外观更改:
这是任务:
将多条件 if 语句示例代码转换为 switch 示例代码。
var myAge = parseInt(prompt("Enter your age", 30), 10);
if (myAge >= 0 && myAge <= 10) {
document.write("myAge is between 0 and 10<br />");
}
if (!(myAge >= 0 && myAge <= 10)) {
document.write("myAge is NOT between 0 and 10<br />");
}
if (myAge >= 80 || myAge <= 10) {
document.write("myAge is 80 or above OR 10 or below<br />");
}
if ((myAge >= 30 && myAge <= 39) || (myAge …Run Code Online (Sandbox Code Playgroud)我正在尝试在 Postman 中创建一个测试,以根据 switch 语句的结果返回不同的文本响应。但是,我不确定为什么当我单击“运行”时它说没有可用的测试我正确设置了我的 Get 方法,因为当我自己执行它时,它返回例如 200 OK。我有以下内容:
var statMessages ;
switch (statMessages) {
case '500':
console.log('Internal Server Error.');
break;
case '200':
console.log('200 Ok');
break;
case '503':
console.log('Service unavailable.');
break;
case '502':
console.log('502 bad gateway.');
break;
case '599':
console.log('Network connect timeout error.');
break;
case '408':
console.log('Request timedout.');
break;
default:
console.log('Sorry, we are out of ' + statMessages + '.');
}
console.log("Try again?");
Run Code Online (Sandbox Code Playgroud) 我已经在 Windows 7 64 位操作系统上安装了 docker for windows toolbox。我无法使用 docker 菜单切换到 Windows 容器,因为 docker 图标在系统托盘中不可用。Docker 服务也不可用。Docker CLI 可执行文件在任何文件夹上均不可用。然而,docker 命令正在运行,并且正在创建简单的容器。
任何有关如何切换到 Windows 容器的建议将不胜感激。
我需要做一些类似于 CASE WHEN .. OR .. THEN 从 Python 中的 SQL for STRINGS 的事情。例如,如果我说“狗”或“猫”……我的翻译是“动物”。
我不想使用 IF ELIF ELIF ..
我能看到的唯一解决方案是:
l = ['cat','dog', 'turttle']
d = {'animal': ['cat','dog', 'turttle']}
word = 'cat'
if word in l:
for i, j in d.iteritems():
if word in j:
print i
else:
print word
animal
Run Code Online (Sandbox Code Playgroud)
它有效,但看起来很丑陋..
还有其他解决办法吗?
谢谢!
我正在使用较旧的 gcc 版本(如果我没记错的话是 7.something)在 LINUX 中开发一个应用程序。最近我尝试在 Windows 上运行相同的应用程序。在 Windows 上,我使用 MinGW 作为编译器(使用 gcc 8.1.0)。
我在 Windows 上编译我的应用程序时遇到了这个错误消息:
警告:控制到达非空函数的结尾 [-Wreturn-type]
代码类似于以下内容:
class myClass {
protected:
enum class myEnum{
a,
b,
};
int fun(myClass::myEnum e);
}
Run Code Online (Sandbox Code Playgroud)
和
int myClass::fun(myClass::myEnum e) {
switch (e){
case myEnum::a:{
return 0;
}
case myEnum::b:{
return 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我明白错误消息的意思,我只是想知道为什么它在 LINUX 中从来都不是问题。
这段代码真的有问题吗,我是否必须添加一些虚拟的 return 语句?
这个函数是否有一个分支会导致没有返回语句?
我有以下枚举:
enum ExampleEnum {
case one
case two
case three
case four
}
Run Code Online (Sandbox Code Playgroud)
以及以下属性定义:
var exampleProperty: ExampleEnum!
Run Code Online (Sandbox Code Playgroud)
在Swift 4.2之前,我使用以下switch语句:
switch self.exampleProperty {
case .one:
print("case one")
case .two:
print("case two")
case .three:
print("case three")
case .four:
print("case four")
default:
break
}
Run Code Online (Sandbox Code Playgroud)
自从切换到Swift 4.2之后,这个switch语句给了我错误:
Enum case 'one' not found in type 'ExampleEnum?'
Run Code Online (Sandbox Code Playgroud)
我觉得这很奇怪,因为我已经用感叹号清楚地定义了类型,以隐式解开可选内容。但是,似乎没有这样做。为了使错误消失,我需要执行以下切换:
switch self.exampleProperty! {
case .one:
print("case one")
case .two:
print("case two")
case .three:
print("case three")
case .four:
print("case four")
}
Run Code Online (Sandbox Code Playgroud)
我在上面所做的是再次解包了exampleProperty变量,即使定义是隐式解包的,也从开关中删除了默认值。
只是想知道为什么在4.2快速更改?是switch语句中的更改,还是为什么再次需要这种解包。看来多余吗?
假设我在编译时已知 N 个不同的整数值,V_1 到 V_N。考虑以下结构:
const int x = foo();
switch(x) {
case V_1: { /* commands for V_1 which don't change x */ } break;
case V_2: { /* commands for V_1 which don't change x */ } break;
/* ... */
case V_N: { /* commands for V_1 which don't change x */ } break;
}
Run Code Online (Sandbox Code Playgroud)
相对
const int x = foo();
if (x == V_1) { /* commands for V_1 which don't change x */ }
else …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 switch 语句来执行任务。我在第一种情况下使用 let 声明了一个变量。但是当我尝试在第二种情况下访问它时,它显示“未捕获的引用错误:t未定义”
x = "20";
switch (x) {
case '10':
let t = 15;
break;
case '20':
console.log(t);
}Run Code Online (Sandbox Code Playgroud)
我收到这个错误
未捕获的引用错误:t 未在 :6:21 定义
但如果我尝试在第二种情况下声明相同的变量
x = "20";
switch (x) {
case '10':
let t = 15;
break;
case '20':
let t = 150;
console.log(t);
}Run Code Online (Sandbox Code Playgroud)
我收到这个错误
未捕获的语法错误:标识符“t”已被声明
为什么会发生这种情况?如何在不使用 var 且不声明具有不同名称的变量的情况下解决此问题。提前致谢。
我通常使用字典实现 Switch/Case 进行相等比较。
dict = {0:'zero', 1:'one', 2:'two'};
a=1; res = dict[a]
Run Code Online (Sandbox Code Playgroud)
代替
if a==0 :
res = 'zero'
elif a == 1:
res = 'one'
elif a==2:
res = 'two'
Run Code Online (Sandbox Code Playgroud)
是否有策略来实施类似的非等比较方法?
if score <=10 :
cat = 'A'
elif score >10 and score <=30:
cat = 'B'
elif score >30 and score <=50 :
cat = 'C'
elif score >50 and score <=90 :
cat = 'D'
else:
cat = 'E'
Run Code Online (Sandbox Code Playgroud)
我知道使用 <、<=、>、>= 可能很棘手,但是是否有任何策略可以概括它或从列表中生成自动语句
{[10]:'A', [10,30]:'B', [30,50]:'C',[50,90]:'D',[90]:'E'}
Run Code Online (Sandbox Code Playgroud)
还有一些标志来说明它是 < …
switch-statement ×10
javascript ×3
c++ ×2
python ×2
case ×1
clang++ ×1
dictionary ×1
docker ×1
fall-through ×1
function ×1
g++ ×1
go ×1
if-statement ×1
ios ×1
postman ×1
return ×1
swift ×1
swift4.2 ×1
toolbox ×1
windows-7 ×1
xcode10 ×1