我正在使用这个switch语句在404页面上加载不同的图像和文本,它工作得很好,但我只是想知道是否有更好的方法来写这个?速记呢?
<script>
var funFacts = Math.floor(Math.random() * 7) + 1;
switch (funFacts) {
case 1:
document.getElementById("funFactText").innerHTML = "In Florida, it is against the law to put livestock in a school bus.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff1.jpg")";
break;
case 2:
document.getElementById("funFactText").innerHTML = "In Texas, it's against the law for anyone to have a pair of pliers in his or her possession.";
document.getElementById("funFactImg").src = "@Url.Content("~/themes/PG/Content/Images/ff2.jpg")";
break;
case 3:
document.getElementById("funFactText").innerHTML = "In Alaska, it is illegal to look at a moose from the window of …Run Code Online (Sandbox Code Playgroud) 我的印象是这段代码可以通过某种方式利用多态来更清晰,但我似乎无法找到一种正确的方法.我尝试使用访问者模式,但没有设法使用它.
具有开关的"Hero"类:
public class Hero {
private Equipment equipment = new Equipment();
// other fields
public void equipArmor(Armor armor) {
findCorrespondingArmorSlot(armor).equipItem(armor);
}
private ItemSlot findCorrespondingArmorSlot(Armor armor) {
switch (armor.getArmorType()) {
case SHIELD:
return equipment.offHand;
case BODY:
return equipment.body;
case HEAD:
return equipment.head;
case GLOVES:
return equipment.hands;
case BOOTS:
return equipment.feet;
case BELT:
return equipment.waist;
case AMULET:
return equipment.neck;
case RING:
return equipment.finger;
case TRINKET:
return equipment.special;
}
throw new NullPointerException();
}
public Equipment getEquipment() {
return equipment;
}
// other …Run Code Online (Sandbox Code Playgroud) 我试图做一个智能的switch语句,而不是使用20+ if语句。我试过了
private int num;
switch(num)
{
case 1-10:
Return "number is 1 through 10"
break;
default:
Return "number is not 1 through 10"
}
Run Code Online (Sandbox Code Playgroud)
它说,案件不能相互解决。
谢谢你的帮助!
目前我有这个:
let somePoint = (1, 0)
switch somePoint {
case (0,0):
print("origin") // does not print
fallthrough
case (_, 0):
print("y-axis") // prints y-axis. this makes sense
fallthrough
case(0, _):
print("x-axis") // prints x-axis (because of fallthrough? this should not print)
fallthrough
case(-2...2, -2...2):
print("in 5x5 box about the origin") // this prints and makes sense
default:
print("somewhere else") // does not print
}
Run Code Online (Sandbox Code Playgroud)
我的这个switch语句的目标是让每个case打印如果它是真的而不是只有第一个匹配print的case.我以为我可以通过这个演绎声明来做到这一点.但是,这让我怀疑它是如何工作的.即使案例不匹配,为什么fallthrough会自动打印下一个案例?我怎么能按照我想要的方式使这个switch语句工作?
我在使用Java 8v60.我试图在catch块中嵌入一个关于异常组的开关.显然,案件得到了认可,但一旦他们进入交换机,他们就会继续经历所有可能的情况.这是一个Java错误吗?
它看起来像这样:
try {
...
} catch (DateTimeParseException exc) {
...
} catch (myException exc) {
switch (exc.getEvent()) {
case EVENT_ONE :
//once EVENT_ONE gets here;
case EVENT_TWO : case EVENT_THREE :
//it keeps going everywhere;
case EVENT_FOUR :
//and so on;
default :
//and here of course too.
//but if it's not one of the above, it just appears here only
}
...
Run Code Online (Sandbox Code Playgroud)
很奇怪,不是吗.任何的想法?
我正在尝试在python中编写一个"switch"字典.我希望能够从文本文件中读取数据并根据其数据类型执行不同的操作.因此,例如,如果我读入一个字符串,我想将它与另一个字符串进行比较.或者,如果我在浮点数中读取,我想用它做一些操作.这是机器学习程序的数据清理操作.
我可以使用If ... Else语句来做到这一点,但是因为我可以想象每种数据类型都有一些东西,所以我宁愿做得更干净.
我正在使用以下代码:
varX = 2.0
switchDict = {"bool": "boolean", "int": "integer","float": "floatType",
"str": "string"}
switchDict[str(type(varX))]()
def boolean():
print("You have a boolean" )
def integer():
print("You have an integer")
def floatType():
print("You have a float")
def string():
print("You have a string”)
Run Code Online (Sandbox Code Playgroud)
它返回:
Traceback (most recent call last):
File "/Gower71/Switch.py", line 5, in <module>
switchDict[str(type(varX))]()
KeyError: "<class ‘float'>"
Run Code Online (Sandbox Code Playgroud)
如果我将switchDict行更改为:
switchDict = {bool: "boolean", int: "integer", float: "floatType", str: "string"}
switchDict[type(varX)]()
Run Code Online (Sandbox Code Playgroud)
它返回:
Traceback (most recent call last):
File "/Gower71/Switch.py", line …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我希望在枚举类型(SearchFilterOptionType)上做一个switch-case ,但由于某种原因,Swift/Xcode编译器无法识别该成员.
我有以下枚举:
enum SearchFilterOptionType {
case Checkbox
case Selection
case Interval
case Text
}
Run Code Online (Sandbox Code Playgroud)
我能够做到以下几点:
if dataElements[0].type == SearchFilterOptionType.Checkbox {
return
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想用switch-case复制相同的内容,我会得到错误:
"Enum case'Checkbox'不是'SearchFilterOptionType!'类型的成员."
switch dataElements[0].type {
case SearchFilterOptionType.Checkbox:
break
default:
break
}
Run Code Online (Sandbox Code Playgroud)
任何想法都表示赞赏!
var dataElements: Array<SearchFilterOption>
Run Code Online (Sandbox Code Playgroud)
struct SearchFilterOption {
var title: String!
var type: SearchFilterOptionType!
init(title: String, type: SearchFilterOptionType) {
self.title = title
self.type = type
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
class Base {}
class A: Base {}
class B: Base {}
class C: Base {}
func next(obj: Base) -> Base {
if obj is A { return B() }
else if obj is B { return C() }
else if obj is C { return A() }
else { return A() }
}
Run Code Online (Sandbox Code Playgroud)
如何将chained ifs 表示为单个switch语句?
我是C#和单元测试的新手。我需要为switch语句编写一个单元测试,并且我不得不承认我已经经历了很多尝试寻找内容的页面。有谁能给我一些如何创建的提示?请在下面我的代码:
public static Message create(String body)
{
Message result = null;
switch (body.ToArray()[0])
{
case 'S':
//checking the regex for a sms message
Match matchS = Regex.Match(body, @"[S][0-9]{9}\+[0-9]{12}");
if (matchS.Success)
{
MessageBox.Show("This is SMS message");
//if the regex will match a sms_message class will be started
result = new Sms_Message(body.Substring(1));
}
//if the regex doesn't match the message should be displayed
else throw new Exception("I don't like content!!!!!!!!!!");
break;
case 'T':
//checking the regex for a tweet message
Match matchT …Run Code Online (Sandbox Code Playgroud) 我目前正在用C编写并使用switch和getch,如下所示:
c=getch();
switch(c)
{
case 17:
//something
break;
case 19:
//something else
break;
default: //loop to get the right "case"
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,我想在我们进入"默认"的情况下循环我的开关.有什么事吗?或者我应该做一个"while(x!= 10)" - 例如 - 循环我的整个开关,如果我进入默认值,x == 10 ??
非常感谢提前!
switch-statement ×10
swift ×3
c# ×2
enums ×2
java ×2
c ×1
dictionary ×1
getch ×1
ios ×1
javascript ×1
python ×1
unit-testing ×1