为什么编译器不会在交换机中的每个代码块之后自动放置break语句?这是出于历史原因吗?您何时需要执行多个代码块?
当我switch在VS2008 C#中创建一个声明时(如此)(人为):
switch (state) {
case '1':
state = '2';
case '2':
state = '1';
}
Run Code Online (Sandbox Code Playgroud)
它抱怨说我不被允许通过:
控制不能从一个案例标签('case'1'(0x31):')转到另一个案例标签
如果您不被允许通过,那么该break声明的目的是什么?为什么语言设计师不会将其排除在外并自动跳到switch语句的末尾而不是强迫我们输入不必要的结构?
我想我不明白示波器如何在开关盒中工作.
有人可以向我解释为什么第一个代码不编译但第二个代码没有编译?
代码1:
int key = 2;
switch (key) {
case 1:
String str = "1";
return str;
case 2:
String str = "2"; // duplicate declaration of "str" according to Eclipse.
return str;
}
Run Code Online (Sandbox Code Playgroud)
代码2:
int key = 2;
if (key == 1) {
String str = "1";
return str;
} else if (key == 2) {
String str = "2";
return str;
}
Run Code Online (Sandbox Code Playgroud)
为什么变量"str"的范围不包含在案例1中?
如果我跳过案例1的声明,则永远不会声明变量"str"...
给出以下代码:
string someString = null;
switch (someString)
{
case string s:
Console.WriteLine("string s");
break;
case var o:
Console.WriteLine("var o");
break;
default:
Console.WriteLine("default");
break;
}
Run Code Online (Sandbox Code Playgroud)
为什么switch语句匹配case var o?
因为(有效地)评估为false case string s,s == null所以我的理解不匹配(null as string) != null.VS Code上的IntelliSense告诉我这o也是一个string.有什么想法吗?
类似于:C#7切换案例,带有空检查
在Java中是否可以编写一个switch语句,其中每个case包含多个值?例如(虽然以下代码显然不起作用):
switch (num) {
case 1 .. 5:
System.out.println("testing case 1 to 5");
break;
case 6 .. 10:
System.out.println("testing case 6 to 10");
break;
}
Run Code Online (Sandbox Code Playgroud)
我认为这可以在Objective C中完成,Java中有类似的东西吗?或者我应该只使用if,else if语句呢?
我正在为学校制作一个简单的视频游戏程序,我已经创建了一种方法,如果调用该方法,玩家将获得15个健康点.我必须保持最高100的健康状态,并且我现在有限的编程能力,我正在做这样的事情.
public void getHealed(){
if(health <= 85)
health += 15;
else if(health == 86)
health += 14;
else if(health == 87)
health += 13;
}// this would continue so that I would never go over 100
Run Code Online (Sandbox Code Playgroud)
我理解我的语法并不完美,但我的问题是,这可能是一个更好的方法,因为我还必须对损伤点做类似的事情,而不是低于0.
这称为饱和算术.
得到以下xcode:但是当我尝试在案例1(或空)中放置一些东西时,它给了我一个错误?
奇怪的问题,因为我不知道受保护的交换机是什么以及我应该如何解决它.有没有人有解决方案或线索来解决这个问题?奇怪的..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller;
switch(indexPath.row) {
case 0:
NSLog(@"0");
//create instance of EKEventStore
EKEventStore *eventStore = [[EKEventStore alloc] init];
//creating instance of EKEvent
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
//setting the appropriate properties of the new event
event.title = @"Woow";
//event.startDate = [[NSDate alloc] init];
NSDateComponents *myDate2 = [[NSDateComponents alloc] init];
[myDate2 setDay:13];
[myDate2 setMonth:12];
[myDate2 setYear:2011];
[myDate2 setHour:00];
[myDate2 setMinute:34];
event.startDate = [[NSCalendar currentCalendar] dateFromComponents:myDate2];
event.endDate = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:event.startDate]; …Run Code Online (Sandbox Code Playgroud) 在Swift中,如何在switch语句中编写一个case来测试根据可选内容切换的值,如果可选包含,则跳过case nil?
以下是我的想象:
let someValue = 5
let someOptional: Int? = nil
switch someValue {
case someOptional:
// someOptional is non-nil, and someValue equals the unwrapped contents of someOptional
default:
// either, someOptional is nil, or someOptional is non-nil but someValue does not equal the unwrapped contents of someOptional
}
Run Code Online (Sandbox Code Playgroud)
如果我只是这样编写它,编译器抱怨someOptional没有解开,但如果我通过添加!到结尾显式解包,我当然会在任何时候someOptional包含运行时错误nil.添加?而不是!对我有意义(在可选链接的精神,我想),但不会使编译器错误消失(即实际上不打开可选的).
假设我在C中有大约这个结构的代码:
switch (something)
{
case 0:
return "blah";
break;
case 1:
case 4:
return "foo";
break;
case 2:
case 3:
return "bar";
break;
default:
return "foobar";
break;
}
Run Code Online (Sandbox Code Playgroud)
现在很明显,"破发" S是没有必要的代码正常运行,但它有点样子不好的做法,如果我不把它们放在那里给我.
你怎么看?删除它们可以吗?或者你会保持他们增加"正确性"?
在下面的代码中,我使用[[fallthrough]]C++ 1z中的标准属性来记录需要的漏洞:
#include <iostream>
int main() {
switch (0) {
case 0:
std::cout << "a\n";
[[fallthrough]]
case 1:
std::cout << "b\n";
break;
}
}
Run Code Online (Sandbox Code Playgroud)
使用GCC 7.1,代码编译时没有错误.但是,编译器仍然警告我一个问题:
warning: this statement may fall through [-Wimplicit-fallthrough=]
std::cout << "a\n";
~~~~~~~~~~^~~~~~~~
Run Code Online (Sandbox Code Playgroud)
为什么?
switch-statement ×10
java ×4
break ×2
c# ×2
if-statement ×2
c ×1
c#-7.0 ×1
c++ ×1
c++17 ×1
case ×1
correctness ×1
enums ×1
fall-through ×1
null ×1
objective-c ×1
optional ×1
swift ×1
var ×1