在 Haskell 中使用 case 表达式时遇到问题。
filterSth::[a]->(b,c)->[a]
filterSth (x:xs) (b, c) =
case (b,c) of
(1,0) -> ...
(1,2) -> ...
...
Run Code Online (Sandbox Code Playgroud)
我可以做类似上面的事情吗?
我有一台扫描仪,询问一些偏好。它创建一个介于 0 和 3 之间的int变量choice。然后我执行以下操作:
String location;
switch(choice){
case 0:
System.out.println("Please type the zip codes you would like to search in a comma separated list");
location = input.nextLine();
break;
case 1:
System.out.println("Please type the street names you would like to search in a comma separated list");
location = input.nextLine().toUpperCase();
break;
case 2:
System.out.println("Please type the boroughs you would like to search in a comma separated list");
location = input.nextLine().toUpperCase();
break;
default:
location = "none";
break;
} …Run Code Online (Sandbox Code Playgroud) 我有5个案例的切换案例陈述。这5种情况之一是:
switch (Number)
{
case 1:
CallSomeFunction1();
break;
case 3:
CallSomeFunction3();
break;
case 4:
CallSomeFunction4();
break;
case (2 || 7 || 8 || 9 || 12 || 14 || 18 || 19 || 22 || 23 || 25):
CallSomeFunction();
break;
case 6:
CallSomeFunction6();
break;
}
Run Code Online (Sandbox Code Playgroud)
切换情况每10毫秒在实时系统上执行一次。将有哪些(或没有任何一种)最快的方式来对照这些案例陈述?
我正在尝试使用python开关编写代码,如在dict中所建议的那样模拟:替换Python中的switch语句?[第二答案,尼克]
当我测试时,出于某种原因,内部的所有函数都被调用,尝试了一个简单的代码来查看出了什么问题并重复了问题。看到:
def switch(f):
print('Switch got: ', f)
var = {
1: func1(),
2: func2(),
3: func3()
}.get(f,False)
return var
def func3():
fb = 'Func3 called'
print(fb)
return fb
def func1():
rsp = 'Func1 called'
print(rsp)
return rsp
def func2():
rsp = 'Func2 called'
print(rsp)
return rsp
var = switch(1)
print(var)
Run Code Online (Sandbox Code Playgroud)
我的预期回报是:
Switch got: 1
Func1 called
Run Code Online (Sandbox Code Playgroud)
相反,我得到了:
Switch got: 1
Func1 called
Func2 called
Func3 called
Func1 called
Run Code Online (Sandbox Code Playgroud)
我从中得到的是,python似乎在调用适当的键(示例中为1)之前在dict中运行了所有func。
这是Python中的预期行为吗?
有没有办法解决,仅调用与键对应的功能?
我遇到了这个基本问题,其中将开关大小写与字符串一起使用。
在案例之间不使用Break语句,但是为什么即使不匹配case字符串也要在所有案例中使用break语句?
所以我很好奇 为什么输出是3而不是1?
public static void main(String [] args)
{
int wd=0;
String days[]={"sun","mon","wed","sat"};
for(String s:days)
{
switch (s)
{
case "sat":
case "sun":
wd-=1;
break;
case "mon":
wd++;
case "wed":
wd+=2;
}
}
System.out.println(wd);
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法,它使用switch语句根据输入参数调用其他方法,如下所示:
switch (reportName)
{
case A:
return GenerateReportA(reportName, model, language);
case B:
return GenerateReportB(reportName, model, language);
case C:
return GenerateReportC(reportName, model, language);
case D:
return GenerateReportD(reportName, model, language);
case E:
return GenerateReportE(reportName, model, language);
and so on...
}
Run Code Online (Sandbox Code Playgroud)
我知道随着越来越多的报告添加到系统中,该switch语句将继续增加。是否有其他方法可以实现这一目标?...代表?... Lambda?我所知道的是,我所有的GenerateReport方法都将具有相同的签名。
我是python的新手,我想创建一个switch case,其中case可以将间隔作为条件,例如:
switch = {
1..<21: do one stuff,
21...31: do another
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能达到这个结果?
我在尝试将参数从一个函数发送到另一个函数时遇到问题,接收参数的函数使用switch语句对它求值并返回它,但它只会返回我输入的变量,而不是“例如“高级硕士”。这是我没有弄错的代码,请记住我对编码非常陌生,也许有人可以给我一些指针。提前致谢。
function calculatexxx(x) {
let calculatexxx= x;
switch (x) {
case (x >= 2400):
console.log("Senior Master");
break;
case (2399 > x > 2200):
console.log("National Master");
break;
case (2199 > x> 2000):
console.log("Expert");
break;
case (1999 > x> 1800):
console.log("Class A");
break;
case (1799 > x> 1600):
console.log("Class B");
break;
default:
console.log("Error input not valid");
return(x);
}
}
function displayxxx() {
console.log("Your Rank is: " + calculatexxx(2400)); //
}
displayxxx();
Run Code Online (Sandbox Code Playgroud) 我有将这些代码重构为使用开关的代码,但看起来仍然很笨拙。现在,我正在寻找一种简化方法。
public async Task CheckAvailability()
{
switch (Settings.Mode)
{
case Enums.MO.Learn:
if (await IsLearn())
{
await ShowFirstMessageAsync();
return;
}
break;
case Enums.MO.Practice:
if (await IsPractice())
{
await ShowFirstMessageAsync();
return;
}
break;
case Enums.MO.Quiz:
if (await IsQuiz())
{
await ShowFirstMessageAsync();
return;
}
break;
default:
break;
}
await PickCard();
}
Run Code Online (Sandbox Code Playgroud)
有没有人能想到一种更简单的方法来实现此目的,而无需多次调用来等待ShowFirstMessageAsync?
当我们有5个切换用例时,switch语句非常好,但是在我们的情况下,我们有15个切换用例。
我想知道什么是Java中switch语句的最佳替代选择
private OpwaardernId getOpwaardernId(String opId) {
OpwaardernId opwaardernId;
switch (opId) {
case "ID001":
opwaardernId = opwaardernId.of("xxx");
break;
case "ID002":
opwaardernId = opwaardernId.of("123");
break;
case "ID002":
opwaardernId = opwaardernId.of("abc");
break;
case "ID003":
opwaardernId = opwaardernId.of("asd");
break;
case "ID004":
opwaardernId = opwaardernId.of("rrr");
break;
case "ID005":
opwaardernId = opwaardernId.of("ttt");
break;
...
case "ID015":
opwaardernId = opwaardernId.of("aaa");
break;
default:
opwaardernId = null;
}
return opwaardernId;
}
Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×3
c# ×2
python ×2
arrays ×1
break ×1
c ×1
delegates ×1
function ×1
haskell ×1
input ×1
javascript ×1
lambda ×1
python-3.x ×1
tuples ×1