switch语句是否case在同一执行时间内为任一可能的输入值确定适当的语句?当找到它正在寻找的值时,它是否将输入值与case块进行比较并跳转到适当的值?case
考虑下面的例子。语句是否switch在同一时间执行input = 1或input = 256对于后一个值执行速度较慢?
int output, input = 256;
switch( input )
{
case 1:
output = 1;
break;
case 2:
output = 2;
break;
case 4:
output = 3;
break;
case 8:
output = 4;
break;
case 16:
output = 5;
break;
case 32:
output = 6;
break;
case 64:
output = 7;
break;
case 128:
output = 8;
break;
case 256:
output = 9; …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个具有多个开关的 PowerShell 菜单,但无法弄清楚为什么先前运行的命令将在退出时再次执行。任何帮助将不胜感激。到目前为止我的代码如下:
function Show-Menu {
param (
[string]$Title = 'Menu'
)
Clear-Host
Write-Host "`n============= $Title =============`n"
Write-Host "Press 'A' to run all commands"
Write-Host "Press '1' to run foo"
Write-Host "Press '2' to run bar"
Write-Host "Press 'Q' to quit"
}
do {
Show-Menu
$Selection = Read-Host "`nPlease make a selection"
switch ($Selection) {
'A' {
$Actions = @('foo', 'bar')
}
'1' {
$Actions = "foo"
}
'2' {
$Actions = "bar"
}
}
switch ( $Actions ) { …Run Code Online (Sandbox Code Playgroud) 我可以以某种方式简化这个 switch 语句,因为这两种情况都只使用另一个函数参数做同样的事情吗?
\nswitch (data.Subscriber.Protocol)\n{\n case "email json":\n builder.Attachments.Add("O\xc4\x8ditanje.json", CraftAttachment(data));\n break;\n case "email text":\n builder.Attachments.Add("O\xc4\x8ditanje.txt", CraftAttachment(data));\n break;\n default:\n break;\n}\nRun Code Online (Sandbox Code Playgroud)\n 我有以下代码:
String fullName = "";
switch(name) {
case "name1":
fullName = String.format("%s %s", name, "surname1");
break;
case "name2":
fullName = String.format("%s %s", name, "surname2");
break;
case "name3":
fullName = String.format("%s %s", name, "surname3");
break;
...
}
Run Code Online (Sandbox Code Playgroud)
(case后面有一个break语句,我只是没有写在这里)
所以问题是这样的:
有 16 个不同的 case 语句,除了 的参数之外,它们都具有完全相同的代码String.format()。
有什么办法可以简化这段代码吗?
注意:Java 8 是必须的
我完全知道 switch 必须与 int 一起使用,但我的作业要求我在用户输入(将是字符串)方面使用 switch。
我看过并且看到一些提到stoi,但我不确定这是否是我的教授所期望的,因为我们还没有被介绍到它。我对 C++ 完全陌生,所以我还在学习,这段代码还没有完成,但我该怎么做呢?
int main()
{
// Declare Constant variables
const float DISC_GOLF_RETAIL = 14.96;
const float ULTIMATE_RETAIL = 20.96;
const double DISCOUNT1 = 8;
const float DISCOUNT2 = .16;
const float DISCOUNT3 = .24;
const float DISCOUNT4 = .32;
const double DEC = 100;
// Declare variables
double quantity;
double pricePerDisc;
double totalSavings;
double total;
char userInput;
float discount;
string discType;
string disc1 = "Ultimate Disc";
string disc2 = "Disc-Golf Disc"; …Run Code Online (Sandbox Code Playgroud) 大家好,我想知道有人可以帮助我,否则我想做的是再次询问我正确的数字,即 1 到 7 并“强迫”用户输入 1 到 7 的数字。
\n#include <stdio.h>\n#include <conio.h>\n\nint main()\n{\n int dia;\n printf("Escriba el n\xc3\xbamero de d\xc3\xada: \\n");\n scanf("%d",&dia);\n if(dia>=1 && dia<=8)\n switch(dia)\n {\n case 1: printf("El d\xc3\xada es Lunes");\n break;\n case 2: printf("El d\xc3\xada %d es Martes",dia);\n break;\n case 3: printf("El d\xc3\xada %d es Mi\xc3\xa9rcoles",dia);\n break;\n case 4: printf("El d\xc3\xada %d es Jueves",dia);\n break;\n case 5: printf("El d\xc3\xada %d es Viernes",dia);\n break;\n case 6: printf("El d\xc3\xada %d es S\xc3\xa1bado",dia);\n break;\n case 7: printf("El d\xc3\xada %d …Run Code Online (Sandbox Code Playgroud) 我想在 Dart 中做这样的事情
enum Result {
dns,
dnf,
dnq,
finish,
active;
get description {
switch(this){
case dns: return 'Not Started';
case dnf: return 'Disqualified';
case dnq: return 'Disqualified';
case finish: return 'Finished';
case active: return 'Underway';
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但我对开关感到困扰。我有一种直觉,应该有一种方法可以在没有开关的情况下做到这一点——也许可以用地图?但我不知道怎么做。
我试过这个
enum Result {
dns,
dnf,
dnq,
finish,
active;
final Map _description = {
dns: 'Not Started',
dnf: 'Disqualified',
dnq: 'Disqualified',
finish: 'Finished',
active: 'Underway'
};
get description => _description[this];
}
Run Code Online (Sandbox Code Playgroud)
但这显然行不通,因为 dart 说 _description “取决于它自己”。我不明白它如何比带有开关的版本更依赖于自身 - 但是,嘿,编译器是这么说的。这是一个飞镖错误,还是我错过了什么?有没有更好的方法来创建具有辅助吸气剂的枚举?
如果有一种方法不需要输入枚举标识符两次并且必须保持副本对齐,那就太好了——就像面向对象的方法一样。
最近,我们将代码库从 Java 8 升级到 Java 17。从那时起,IntelliJ IDEA 就会对旧的 switch-case 语句发出警告:
switch 语句可以替换为增强的“switch”
除了可读性之外,增强型开关还有哪些好处?
示例:java 8 开关:
switch (foo) {
case "bla": return 1;
case "bla_bla": return 2;
}
Run Code Online (Sandbox Code Playgroud)
接受后Switch statement can be replaced with enhanced 'switch'看起来像这样:
switch (foo) {
case "bla" -> { return 1; }
case "bla_bla" -> { return 2; }
}
Run Code Online (Sandbox Code Playgroud) 我有一段代码,其中对枚举类的所有值执行 switch case 语句。所有分支都有 return 语句,但 gcc(至少 10.5 和 trunk)仍然认为控件可以到达函数末尾。相同的代码可以在 clang 16 中正常编译。
这是正确的还是 gcc 中的错误?
#include <cassert>
enum class C
{
A, B
};
int foo()
{
C c;
switch (c) {
case C::A:
assert(false);
case C::B:
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
function FM_log(level, text) {
// caso não seja log total escolhe o que loga
var log = false;
switch (level) {
case "addtoprio()":log = true;
case "alternaTropas()":log = false;
case "sendtroops()":log = false;
defalt: log = false;
}
if ((logTotal == false) && (log == true))
GM_log(horaAtual() + " - "+level+", "+text);
else if (logTotal == true)
GM_log(horaAtual() + " - "+level+", "+text);
}
Run Code Online (Sandbox Code Playgroud)
怎么做那个开关是一种工作方式?
switch-statement ×10
c ×2
c++ ×2
enums ×2
java ×2
string ×2
.net-6.0 ×1
c# ×1
c#-10.0 ×1
dart ×1
hashmap ×1
if-statement ×1
java-8 ×1
javascript ×1
oop ×1
powershell ×1