Let's say we have a function that changes a password for a user in a system in an MVC app.:
public JsonResult ChangePassword
(string username, string currentPassword, string newPassword)
{
switch (this.membershipService.ValidateLogin(username, currentPassword))
{
case UserValidationResult.BasUsername:
case UserValidationResult.BadPassword:
// abort: return JsonResult with localized error message
// for invalid username/pass combo.
case UserValidationResult.TrialExpired
// abort: return JsonResult with localized error message
// that user cannot login because their trial period has expired
case UserValidationResult.Success:
break;
}
// NOW change password …Run Code Online (Sandbox Code Playgroud) 我想用switch,但我有很多情况,有没有捷径?到目前为止,我所知道和尝试的唯一解决方案是:
switch (number)
{
case 1: something; break;
case 2: other thing; break;
...
case 9: .........; break;
}
Run Code Online (Sandbox Code Playgroud)
我希望我能做的是:
switch (number)
{
case (1 to 4): do the same for all of them; break;
case (5 to 9): again, same thing for these numbers; break;
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
我想我会失明,因为我无法弄清楚此代码中语法错误的位置:
if( cell == nil ) {
titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier ] autorelease
];
switch( cellNumber ) {
case 1:
NSString *viewDataKey = @"Name";
etc...
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我在最后一行的'*'标记之前收到错误:语法错误.
对不起这个基本问题,但我错过了什么?
我需要将以下内容更改if为switch- case同时检查a String,以提高圈复杂度.
String value = some methodx;
if ("apple".equals(value)) {
method1;
}
if ("carrot".equals(value)) {
method2;
}
if ("mango".equals(value)) {
method3;
}
if ("orange".equals(value)) {
method4;
}
Run Code Online (Sandbox Code Playgroud)
但我不确定我会得到什么价值.
我对这件事很好奇......见例子:
switch(x)
{
case(a):
{
//do stuff
}
break;
case(b):
//do stuff
break;
}
Run Code Online (Sandbox Code Playgroud)
我的一生都像case b那样做了,但是因为C#允许我使用它,而Visual Studio允许我崩溃那个东西,我很好奇 - 案例a(带括号)和案例b之间的真正区别是什么?
是否有一种CTRL +类似空格的方式在Eclipse中围绕给定的Java Enum"自动构建"一个switch案例?我想要一个包含所有Enum案例的存根...
我有一些不同的按钮调用相同的功能,我希望将它们包装在一个switch语句中,而不是使用一堆其他条件.任何帮助都会很棒!!!
events:
"click .red, .blue, #black, #yellow" : "openOverlay"
openOverlay: (e) ->
e.preventDefault()
e.stopPropagation()
target = $(e.currentTarget)
# the view should be opened
view =
if target.hasClass 'red' then new App.RedView
else if target.hasClass 'blue' then new App.BlueView
else if target.is '#black' then new App.BlackView
else
null
# Open the view
App.router.overlays.add view: view if view?
Run Code Online (Sandbox Code Playgroud) 我正在使用列号运行switch case,我的列号可以是0到50.在循环中运行时 - 它们会得到验证.现在每个案例都支持离散列号,我发现它失败了.示例:我的列号是10,这是代码 -
i=10
a=1
b=0.65
if [ "$a" != "$b" ]; then
case $i in
[1]|[2]|[5]) echo "Not OK"; ;;
[9-10]|[12]) echo "may be ok"; ;;
*) echo "no clue - $i"; ;;
esac
fi
Run Code Online (Sandbox Code Playgroud)
我希望"可能没问题",但没有任何线索 - 10
Kotlin中的模式匹配很好,并且在90%的用例中,它不执行下一个模式匹配的事实很好.
在Android中,当数据库更新时,我们使用Java开关属性继续下一个案例,如果我们不休息让代码看起来像这样:
switch (oldVersion) {
case 1: upgradeFromV1();
case 2: upgradeFromV2();
case 3: upgradeFromV3();
}
Run Code Online (Sandbox Code Playgroud)
因此,如果有人拥有数据库版本1的应用程序而错过了使用DB v2的应用程序版本,他将获得执行所需的所有升级代码.
转换为Kotlin,我们得到一个混乱:
when (oldVersion) {
1 -> {
upgradeFromV1()
upgradeFromV2()
upgradeFromV3()
}
2 -> {
upgradeFromV2()
upgradeFromV3()
}
3 -> {
upgradeFromV3()
}
}
Run Code Online (Sandbox Code Playgroud)
这里我们只有3个版本,想象当DB达到版本19时:/
无论如何,当以与开关相同的方式行动时?我试着继续没有运气.
C#switch语句的默认标签如何处理可以为空的枚举?
默认标签是否会捕获空值和任何未处理的案例?
switch-statement ×10
c# ×4
java ×2
autocomplete ×1
bash ×1
coffeescript ×1
eclipse ×1
exception ×1
if-statement ×1
kotlin ×1
null ×1
nullable ×1
objective-c ×1
string ×1