我今天开始学习Dart,而且我遇到了一些谷歌技能难以找到的东西.
如何在非空案例中进行翻版?
我的用例是这样的:我正在写一个sprintf实现(因为dart也没有这个),除了这个落空的东西之外它会工作.例如,在解析变量类型时,您可以使用"%x"与"%X",其中大写类型告诉格式化程序输出应该是大写的.
半伪代码看起来像:
bool is_upper = false;
switch (getType()) {
case 'X':
is_upper = true;
case 'x':
return formatHex(is_upper);
}
Run Code Online (Sandbox Code Playgroud)
我可以想到的其他方式,将是以下之一
1:
switch (getType()) {
case 'X': case 'x':
return formatHex('X' == getType());
}
Run Code Online (Sandbox Code Playgroud)
2:
var type = getType();
if (type in ['x', 'X']) {
return formatHex('X' == getType());
}
Run Code Online (Sandbox Code Playgroud)
现在,第二个选择几乎看起来不错,但是你必须记住,有11个案例,这意味着有11个if (type in []),这是我想要的更多打字.
那么,飞镖有一些// //$FALL-THROUGH$我不知道的吗?
谢谢.
代码示例:
int main(int argc, char **argv)
{
switch(argc)
{
case 0:
argc = 5;
__attribute__((fallthrough));
case 1:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
仅使用gcc 6.3.0,-std=c11此代码给出警告:
<source>: In function 'main':
7 : <source>:7:3: warning: empty declaration
__attribute__((fallthrough));
^~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
在不引起警告的情况下使用此方法的正确方法是什么?
这不是一个特别的C#问题,但它的C#版本switch让我问这个问题.
为什么我必须明确声明我不想从一个case陈述到下一个陈述而不是指示何时我想要通过?
在C#中,编译器不会让你从一个case语句落到下一个语句(除非case语句为空),但它强制你break在每个语句的末尾放置一个显式.switch我写的绝大多数陈述都没有落空.
int x = 4;
string result;
switch (x)
{
case 1:
result = "One";
break;
case 2:
result = "A couple";
break;
case 3:
case 4:
result = "Three or four";
break;
/*
case 5:
result = "Five"; // Here be an error! A common typo for me!!!
*/
default:
result = "Five or more";
break;
}
Run Code Online (Sandbox Code Playgroud)
上面的例子是为了表明可能性.实际落入的次数(如上面的3到4之间)是最小的.如果我从上面的案例5中删除了注释,C#编译器会将其作为错误捕获.如果编译器完全知道我想要做什么,为什么我必须手动修复它?
以下不会更好吗?
int x = 4;
string result; …Run Code Online (Sandbox Code Playgroud) 所以我遇到了一个有竞争力的问题(询问输出)如下:
#include <stdio.h>
int main()
{
int i = 0;
for(i = 0; i < 20; i++)
{
switch(i)
{
case 0: i+=5;
case 1: i+=2;
case 5: i+=5;
default: i+= 4;
break;
}
printf("%d ", i);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是16, 21。虽然我知道 switch case 是如何工作的,但我无法解释这个秋天是如何工作的。为什么要添加默认值?K&R C 书不是说只有在没有任何情况匹配时才执行默认值吗?
谢谢。
我有一个在 gcc 4.8.4 下构建良好的项目。我尝试使用 gcc 7 构建,并注意到很多-Wimplicit-fallthrough=警告。据我所知,这确实是在 gcc 版本 7 中添加的。我现在-Wno-implicit-fallthrough在构建时使用以抑制这些警告。移动回旧版本的gcc,保持-Wno-implicit-fallthrough编译器标志并不会导致任何错误,即使我不相信GCC 4识别此选项。怎么来的?特别是-Wno-允许不被识别的选项吗?
我在我的Raspberry Pi上做了一个基本的随机程序,它有点像这样.
import random
print ("Welcome to the PC Expo's new game, PC Dispenser, what will you win?")
WinorLose = random.randint(1, 1000)
if WinorLose <100:
print ("You won a Nintendo Wii.")
elif WinorLose >200:
print ("You won a Sony PSP.")
elif WinorLose > 300:
print ("You won a Nintendo Wii U.")
elif WinorLose > 400:
print ("You won a Sony PS Vita.")
else:
print ("Not your lucky day, Try again.")
print ("Thank you for the visit.")
Run Code Online (Sandbox Code Playgroud)
如果你不知道它做了什么,它有机会给你一个虚拟的PSP,Wii U等等.但它所做的只是打印"你赢得索尼PSP",或"不是你的幸运日,再试一次".怎么了?任何修复?
我有一个switch语句,如下所示:
switch (condition)
{
case 0:
case 1:
// Do Something
break;
case 2:
// Do Something
case 3:
// Do Something
break;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误告诉我 Control cannot fall through from one case label ('case 2:') to another
嗯......是的,你可以.因为你是从做它case 0:通过对case 1:.
事实上,如果我删除了我case 2:和它的相关任务,代码就会编译并从中case 0:进入case1:.
那么这里发生了什么,我怎样才能让我的case语句通过并执行一些中间代码?
下面的代码应该打印整数值是奇数还是偶数通过switch语句和for语句
for(int i=2; i<=10; i+=2)
{
switch(i)
{
case 1:
{System.out.printf("\nNot printing odd numbers");}
case 2:
System.out.printf("\n %d is an even number.", i);
//case 3:
//case 4:
}//end switch
}//end for
Run Code Online (Sandbox Code Playgroud) 众所周知,根据MSDN,c#中的switch case不允许你通过
在所选开关部分中执行语句列表以第一个语句开始,并继续执行语句列表,通常直到达到跳转语句,例如break,goto case,return或throw.此时,控制权转移到switch语句之外或转移到另一个case标签.
与C++不同,C#不允许从一个交换机部分继续执行到下一个交换机部分.以下代码导致错误.
如果是这样的话,为什么这会编译:
void Main()
{
int s = 3;
switch (s)
{
case 1:
case 2:
case 3:
Console.WriteLine("hit 3");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这不应该被识别为编译时错误吗?
我是Rust的新手,但作为Haskell的粉丝,我非常感谢matchRust的工作方式.现在我面临着一个罕见的情况,我确实需要堕落 - 从某种意义上说,我希望所有匹配的几个重叠的案例都能被执行.这有效:
fn options(stairs: i32) -> i32 {
if stairs == 0 {
return 1;
}
let mut count: i32 = 0;
if stairs >= 1 {
count += options(stairs - 1);
}
if stairs >= 2 {
count += options(stairs - 2);
}
if stairs >= 3 {
count += options(stairs - 3);
}
count
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是否是Rust中的惯用语或是否有更好的方法.
编辑:上下文是Cracking the Coding Interview的一个问题:"一个孩子正在爬楼梯,有n个步骤,可以一步跳1步,2步或3步.实施一种方法来计算孩子爬楼梯的可能方式."
我是Golang的新手,我发现switch case声明不需要break声明来停止评估案例.
那么,我想知道如何在go中实现这种穿透行为?