下面是声明两个int变量并尝试在switch语句中使用它们的代码.这是C++中的合法操作吗?如果没有,为什么不呢?
int i = 0;
int x = 3;
switch (i)
{
case x:
// stuff
break;
case 0:
// other stuff
break;
}
Run Code Online (Sandbox Code Playgroud) 我决定使用Swift的case语句和元组。它看起来像是该语言的较酷功能之一。
我决定玩月/日/年元组。令我惊讶的是,我不能在switch语句中使用常量元组值作为案例。这是一个示例(可以粘贴到Playground中并运行)
import UIKit
typealias mdyTuple = (month: Int, day: Int, year: Int)
let joesBirthday: mdyTuple = (month: 6, day: 7, year: 1978)
let someday: mdyTuple = (6, 7, 1978)
switch someday
{
//---------
//The line "case joesBirthday" won't compile.
//case joesBirthday:
// println("Joe was born on this day"
//---------
case (joesBirthday.month, joesBirthday.day, joesBirthday.year):
println("Joe was born on this day")
case (joesBirthday.month, joesBirthday.day, let year):
println("Joe is \(year-joesBirthday.year) today")
default:
println("Some other day")
}
Run Code Online (Sandbox Code Playgroud)
注释掉的代码case joesBirthday:不会编译(如果重要的话,请在Xcode 6.3中)。下面的情况(我分别列出了joesBirthday元组的所有元素)很难输入,也很难阅读,确实可以工作)
键入此命令时,我的Playground崩溃了Xcode,再次尝试重新启动Xcode崩溃了,所以我在报告错误代码时遇到了麻烦。 …
有没有一种方法可以在Java中实现内联switch语句?
现在,我正在使用以下内容:
private static String BaseURL = (lifeCycle == LifeCycle.Production)
? prodUrl
: ( (lifeCycle == LifeCycle.Development)
? devUrl
: ( (lifeCycle == LifeCycle.LocalDevelopment)
? localDevUrl
: null
)
);
Run Code Online (Sandbox Code Playgroud)
如果可以执行以下操作,我将更喜欢它:
private static String BaseURL = switch (lifeCycle) {
case Production: return prodUrl;
case Development: return devUrl;
case LocalDevelopment: return localDevUrl;
}
Run Code Online (Sandbox Code Playgroud)
我知道您可以通过将
BaseURL变量移动到GetBaseURL发生切换的函数中来实现此目的(请参阅下文),但是我只是很好奇这个功能是否甚至存在于Java中。
static String GetBaseURL() {
switch(lifeCycle) {
case Production: return prodUrl;
case Development: return devUrl;
case LocalDevelopment: return localDevUrl;
}
return null; …Run Code Online (Sandbox Code Playgroud) 我想在我的专栏中放置一个switch 语句来决定在我的FLUTTER 应用程序中构建哪些子组件组合。
在我的具体情况下,我创建了一个具有 4 种不同可能状态(AnimationEnum.None、AnimationEnum.etc)的枚举,每种状态都会触发不同的子级构建组合。
我可以通过在每个可能的小部件上方编写 if 语句来使其正常工作,但这显然是一种低效的做事方式,并且希望简化我的代码。
我觉得我已经很接近了,但还不能完全到达那里。这是带有占位符小部件的代码的简化版本:
谢谢!
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
switch(widget._cardAnimType)
case AnimationEnum.None: {
Widget1('args'),
Widget2('args'),
Widget3('args'),
//etc
break;
}
case AnimationEnum._card1Correct: {
Widget1('args'),
Widget2('args'),
Widget3('args'),
//etc
break;
}
///more switch statements here...
]
),
),
),
Run Code Online (Sandbox Code Playgroud) 我想使用switch caseSvelte 中的语句有条件地渲染组件,如下所示:
// index.svelte
<script>
import TextContent from './components/text-content.svelte'
import { someData } from './api/some-data.js'
const ContentSwitch = (data) => {
switch (data._type) {
case 'block':
return data.children.map((child) => ContentSwitch(child));
case 'span':
return (
<TextContent>
<span slot="text-content">{data.text}</span>
</TextContent>
);
}
for (let index = 0; index < data.length; index++) {
return data.map((item) => ContentSwitch(item));
}
};
</script>
<div>
{#each someData as data}
{ContentSwitch(data)}
{/each}
</div>
Run Code Online (Sandbox Code Playgroud)
文本内容组件:
// components/text-content.svelte
<slot name="text-content">
<span />
</slot>
Run Code Online (Sandbox Code Playgroud)
看来这种方法在 Svelte 中不起作用,因为我收到了错误Unexpected …
如图所示,它抛出错误。请帮助我找到解决方案。
这里我在开关中使用浮点数,应该打印10。
这个错误的原因是什么?为什么?
#include <stdio.h>
void main() {
float a = 1.0;
switch (a) {
case 1:
printf ("1");
break;
case 1.0:
printf ("10");
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我继承了一个非常古老(超过 15 年)的 C++ 程序,该程序当前使用 IBM 的 xlc 编译器在 AIX 上运行。我遇到了一个 switch 语句,但我不明白它是如何工作的。
\n下面是一个显示这种情况的最小示例。
\n#include <iostream>\nusing namespace std;\n\nint main()\n{\n int i=5;\n\n switch( i ) {\n case 1:\n cout << "case " << i << endl;\n break;\n case 2:\n cout << "case " << i << endl;\n break;\n Otherwise:\n cout << "case " << i << endl;\n break;\n }\n cout << "bye\\n";\n}\nRun Code Online (Sandbox Code Playgroud)\n我在 Amazon Linux 2 上使用 GCC 7.3.1。该程序编译良好并显示以下输出:
\nbye\nRun Code Online (Sandbox Code Playgroud)\n如果我添加“-Wall”,那么它会告诉我以下内容:
\nminex.C: In function \xe2\x80\x98int …Run Code Online (Sandbox Code Playgroud) 这是OrderStatus枚举:
public enum OrderStatus\n {\n [Display(Name = "G\xc3\xb6zl\xc9\x99m\xc9\x99d\xc9\x99")]\n Pending,\n [Display(Name = "Q\xc9\x99bul olundu")]\n Accepted,\n [Display(Name = "L\xc9\x99\xc4\x9fv edildi")]\n Rejected,\n [Display(Name = "\xc3\x87atd\xc4\xb1r\xc4\xb1ld\xc4\xb1")]\n Delivered\n }\nRun Code Online (Sandbox Code Playgroud)\n这是Order模型:
public class Order : BaseEntity\n {\n public string AppUserId { get; set; }\n public AppUser AppUser { get; set; }\n [StringLength(255), Required]\n public string StoreName { get; set; }\n [StringLength(255), Required]\n public string StoreAddress { get; set; }\n public double TotalPrice { get; set; }\n public double RemainingBalance …Run Code Online (Sandbox Code Playgroud) 我不明白为什么第一个片段编译得很好:
var res = getResult(s);
public static double getResult(Shape s) {
switch(s) {
case Rectangle r : return 2* r.largeur() + 2* r.longueur();
case Circle c : return 2*c.radius();
default : throw new RuntimeException("not a shape");
}
}
Run Code Online (Sandbox Code Playgroud)
但不是这个:
var res = switch(s) {
case Rectangle r : return 2* r.largeur() + 2* r.longueur();
case Circle c : return 2*c.radius();
default : throw new RuntimeException("not a shape");
};
Run Code Online (Sandbox Code Playgroud)
对我来说看起来是一样的。
我最近发现这switch (...) while (0) {}在 C 中是合法的(此处),但我找不到关于这件事的解释。
我在互联网上唯一一次看到这个是在混淆的 GitHub 存储库中,没有任何解释。
我所有的研究显然都给了我关于 while 或 switch 循环的结果,甚至没有提到这种语法,所以我想这更多是合法的,但非常罕见且可能无用,是对标准的滥用。谁能帮助我理解这一点?
编辑:正如@phuclv答案中所解释的,switch语句需要一个选择语句,它可以是括号内的一些代码(在这种情况下......可能是case语句)或一个带有自己的括号等的循环,这意味着这个在 C 中是合法的:
switch (...) while (...) switch (...) {}
Run Code Online (Sandbox Code Playgroud)
switch根本不关心后面的语句,它似乎只寻找 case(s) 和/或默认值。
switch (1) while (0) {
puts("Not executed");
}
Run Code Online (Sandbox Code Playgroud)
该puts语句不会被执行,因为没有 case/default,所以 switch 在这里基本上没有用。你可以在Compiler Explorer上看到它,GCC 给出警告并删除了开关。
但是,请注意:
#include <stdio.h>
int main(void) {
switch (1) while (1) switch (0) {
case 1:
puts("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
没有显示任何内容,程序立即退出,因为 switch (1) 没有 case 1 或 default 语句。如果我们添加一个: …