标签: switch-statement

Powershell v1:是否可以将switch语句的结果赋给变量?

是否可以将switch语句的结果赋给变量.

例如,而不是:

switch ($Extension) 
    { 
        doc {$Location = "C:\Users\username\Documents\"; break} 
        exe {$Location = "C:\Users\username\Downloads\"; break}
        default {$Location = "C:\Users\username\Desktop\"}
    }
Run Code Online (Sandbox Code Playgroud)

是否可以做类似的事情:

$Location = 
{
    switch ($Extension) 
    { 
        doc {"C:\Users\username\Documents\"; break} 
        exe {"C:\Users\username\Downloads\"; break}
        default {"C:\Users\username\Desktop\"}
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试上述结果将$ location包含整个代码块作为String.

powershell switch-statement

5
推荐指数
1
解决办法
2751
查看次数

在处理间隔时,是否有一种优雅的方式来替换像switch这样的东西?

在.NET中有没有办法替换比较间隔的代码

if (compare < 10)
        {
            // Do one thing
        }
        else if (10 <= compare && compare < 20)
        {
            // Do another thing
        }
        else if (20 <= compare && compare < 30)
        {
            // Do yet another thing
        }
        else
        {
            // Do nothing
        }
Run Code Online (Sandbox Code Playgroud)

通过像switch语句更优雅的东西(我认为在Javascript"case(<10)"中有效,但在c#中)?有没有其他人发现这个代码也很难看?

.net c# switch-statement

5
推荐指数
1
解决办法
698
查看次数

优化大型switch语句

我有一个大的switch语句,我根据XElement的输入值创建UIElements:

public static UIElement CreateElement(XElement element) {
            var name = element.Attribute("Name").Value;
            var text = element.Attribute("Value").Value;
            var width = Convert.ToDouble(element.Attribute("Width").Value);
            var height = Convert.ToDouble(element.Attribute("Height").Value);
            //...
            switch (element.Attribute("Type").Value) {
                case "System.Windows.Forms.Label":
                    return new System.Windows.Controls.Label() {
                        Name = name,
                        Content = text,
                        Width = width,
                        Height = height
                    };
                case "System.Windows.Forms.Button":
                    return new System.Windows.Controls.Button() {
                        Name = name,
                        Content = text,
                        Width = width,
                        Height = height
                    };
                    //...
                default:
                    return null;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我正在创建这样的很多控件,正如你所看到的,重复过多.

有没有办法避免这种重复?提前感谢您的想法.

c# .net-4.0 switch-statement

5
推荐指数
2
解决办法
670
查看次数

获取switch语句中的案例列表

我使用PHP Switch语句来确定我的网站页面.所以这是一个例子:

switch($page) {
    case "about":
        $title_name = "About Us";
        $page_content = "includes/about-us.php";
        include("inner.php");
    break;
    case "services":
        $title_name = "Services";
        $page_content = "includes/services.php";
        include("inner.php");
    break;
}
Run Code Online (Sandbox Code Playgroud)

我的文件结构是index.php?page = about,它转换为/ about /使用htaccess.

我想要做的是将所有页面都放在switch语句中并自动获取它并将其放入列表中,这样我就可以自动将其写入我的页脚页面,在那里我将拥有所有链接.

因此,不要手动输入页脚中的所有链接,如:Home | 关于我们| 服务| 常见问题,它会根据我在Switch语句中提供的页面自动提取它.

有没有办法做到这一点?自动添加新页面也很好,它将为新页面添加新案例,并自动在包含文件夹中创建页面.

如果有人能指出我正确的方向,我会非常感激.根据我的理解,我不相信你可以用switch语句来做这件事,我必须以我调用页面的方式重新工作,对吧?

php dynamic switch-statement

5
推荐指数
2
解决办法
2363
查看次数

codeDom中的switch语句(跳转表样式)

我知道switch语句不可用CodeDom以及编译器如何处理switch语句.

因此,出于性能原因,当存在许多情况时,我不想使用If-else
为什么switch语句而不是if-else?

是否可以生成代码来模拟给定案例列表的Jump表.

switch(value) {
    case 0: return Method0();
    case 1: return Method1();
    case 4; return Method4();
}
Run Code Online (Sandbox Code Playgroud)

会产生:

    private delegate object Method();

    Method[] _jumpTable = new Method[] { Method0, Method1, null, null, Method4 };

    private object GetValue(int value)
    {
        if (value < 0 || value > 4) 
            return null;
        return _jumpTable[value]();
    }
Run Code Online (Sandbox Code Playgroud)

如果序列中有漏洞或列表稀疏,分析案例列表并生成数组的最佳方法是什么?

c# codedom switch-statement

5
推荐指数
1
解决办法
1252
查看次数

切换java 1.6的类型字符串

我有以下代码,我想知道是否有办法使用开关块而不是一堆if/else语句.我知道Java从Java 1.7开始支持交换机块中的字符串,但我仍在使用Java 1.6:

} else if (typeName.equals("Boolean")) {

            return new SwitchInputType<Boolean>(new Boolean((String) memberValue));

        } else if (typeName.equals("Double")) {

            return new SwitchInputType<Double>(new Double((String) memberValue));

        } else if (typeName.equals("Int32"))
Run Code Online (Sandbox Code Playgroud)

java switch-statement

5
推荐指数
2
解决办法
2万
查看次数

Swift switch case使用数组的内容,语法?

来自Apple的Swift Book有一个使用元音作为案例的开关示例.

题. 而不是有这个元音列表,是否可以使用包含此内容的数组?如果是这样,这样做的语法是什么?


〜来自Apple Swift Book~

以下示例从小写字符串中删除所有元音和空格,以创建一个神秘的拼图短语:

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
    switch character {
    case "a", "e", "i", "o", "u", " ":
        continue
    default:
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)
// Prints "grtmndsthnklk"
Run Code Online (Sandbox Code Playgroud)

arrays syntax switch-statement swift

5
推荐指数
1
解决办法
2672
查看次数

错误:类型'String'的值在Swift开关中没有成员'hasSuffix'

我正在关注Swift.org的教程

以下switch语句示例引发错误.

let vegetable = "red pepper"
switch vegetable {
case "celery":
    print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
    print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
    print("Is it a spicy \(x)?")
default:
    print("Everything tastes good in soup.")
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是: error: value of type 'String' has no member 'hasSuffix' case let x where x.hasSuffix("pepper"): 使用ubuntu 14.04和我的Swift版本swift --version Swift version 3.0 (swift-3.0-PREVIEW-2)

switch-statement swift3

5
推荐指数
1
解决办法
4055
查看次数

落在模式匹配中

目前在c#7(版本15.3.4)中,以下代码对编译有效,但两个变量都是合法不可用的.

switch(fruit)
{
    case Apple apple:
    case Orange orange:
    // impossible to use apple or orange
    break;
    case Banana banana:
    break;
}
Run Code Online (Sandbox Code Playgroud)

如果您尝试使用它们,则会得到熟悉的错误,在访问之前可能无法初始化变量.

有时在模式匹配中,您不关心确切的类型,只要该类型属于您想要的类别.这里只以苹果和橘子为例.

List<Fruit> applesAndOranges = new List<Fruit>();
switch(fruit)
{
    case Fruit X when X is Apple || X is Orange:
    applesAndOranges.Add(X);
    break;
    case Banana banana:
    break;
}
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?

c# pattern-matching switch-statement fall-through c#-7.0

5
推荐指数
1
解决办法
548
查看次数

工厂喜欢带有弹簧豆的图案,没有开关盒

我的目标是根据枚举返回一个方法.目前,我已经创建了一个工厂,但它使用的是我不喜欢的开关盒.代码工作正常但我想使用更好的模式并替换当前的开关案例.你如何设计这个没有任何开关案例或如果否则(实例)......

我还试图在枚举中实现一个Stategy模式.但是无法自动装配bean.

请参阅下面的我目前的代码.

public enum Car {
     AUDI, FORD;
}
Run Code Online (Sandbox Code Playgroud)
public class SuperCar{
     private Car car;
}
Run Code Online (Sandbox Code Playgroud)
public class Audi extends SuperCar{
     // some other properties
}
Run Code Online (Sandbox Code Playgroud)
public class Ford extends SuperCar{
     // some other properties
}
Run Code Online (Sandbox Code Playgroud)
@Service
public class AudiService{

     public void save(Audi audi){
         // some code
     }

}
Run Code Online (Sandbox Code Playgroud)
@Service
public class FordService{

     public void save(Ford ford){
         // some code
     }

}
Run Code Online (Sandbox Code Playgroud)
@Service
public class CarFactory{

     private FordService fordService;
     private AudiService audiService;

     public CarFactory(FordService fordService, AudiService audiService) {
           this.fordService …
Run Code Online (Sandbox Code Playgroud)

java spring factory case switch-statement

5
推荐指数
1
解决办法
221
查看次数