我在过去的一个半小时里一直在研究整数的开关,我知道如何用 char 进行开关,但这对我来说似乎很难。任何建议将不胜感激。我遇到的问题是我不能接受成绩该开关当前执行的操作超过 100
\n\n int testScore; \n cout <<"Enter your test score and I will tell you \\n";\n cout <<"the letter grade you earned ";\n cin >> testScore;\n\n switch(testScore/10)\n{ \n case 10:\n case 9:\n cout <<"Your grade is A.\\n";\n break;\n case 8: \n cout <<"Your grade is B.\\n";\n break;\n case 7: \n cout <<"Your grade is C.\\n";\n break;\n case 6: \n cout << "Your grade is D.\\n";\n break;\n case 5: \n cout << "Your grade is F.\\n";\n break;\n\n default:\n …Run Code Online (Sandbox Code Playgroud) 看起来 Swift 中的枚举无法比较相等性。这是我期望工作的代码:
let areEqual = MyEnum.SomeEnumValue == MyEnum.SomeEnumValue
Run Code Online (Sandbox Code Playgroud)
但是,这不会编译并抛出错误:Cannot invoke '==' with an argument list of type '(MyEnum, MyEnum)'。
是否可以使用==运算符比较 2 个枚举值,或者我真的为此编写 switch-case 吗?
编辑
这是自包含的示例。只要将它传递到游乐场,您应该会看到编译错误。
import UIKit
enum MyEnum {
case SomeValue(Int)
case OtherValue(Double)
case ThirdValue
}
let areEqual = MyEnum.ThirdValue == MyEnum.ThirdValue
Run Code Online (Sandbox Code Playgroud)
看起来有点像 Swift bug,因为它可以让您比较没有关联值的枚举而不会出现任何抱怨。所以我希望这不是预期的行为(允许比较一些枚举,但不允许比较其他枚举)。
我如何将在此 switch 语句中找到的值返回给类的其余部分以供使用(我想在该 switch 语句之外使用 oneTotal、twoTotal 等)?下面是 switch 语句的代码:
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
return oneTotal;
case 2:
double twoTotal = 4.50 * userQuantity;
return twoTotal;
case 3:
double threeTotal = 9.98 * userQuantity;
return threeTotal;
case 4:
double fourTotal = 4.49 * userQuantity;
return fourTotal;
case 5:
double fiveTotal = 6.87 * userQuantity;
return fiveTotal;
}
Run Code Online (Sandbox Code Playgroud)
在 switch 语句的外面,我希望一旦用户停止使用 switch 语句(我在 while 循环中使用它)就将 FiveTotal 添加到一起,然后希望返回五个总计以在 main 方法中使用类(switch 语句位于其自己的双精度、非 void 类中,因此它可以返回值)。
这是我正在做的事情的超级简化版本,但想象一下需要对 switch 语句中的对象执行多次验证。在验证的任何时候,我们都可能决定不想继续使用该对象。
愚蠢的例子:
var yummies = [
{name: 'apples', quantity: 3},
{name: 'apples', quantity: 4}
];
var purchases = [];
for (var i = 0; i < yummies.length; i++) {
var yummy = yummies[i];
switch (yummy.name) {
case 'apples':
if (purchases.indexOf(yummy.name) > -1) {
break;
}
//A ton of other code including, possibly other breaks
purchases.push(yummy.name);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Areturn不起作用,因为我仍然想继续循环。我想我可以使用一个continue可以工作的语句,但是continue在switch/case.
无论如何,两个breaks 在一个case. 有什么理由不这样做?
我想知道您是否可以在 switch case 的情况下使用诸如 'contains()' 之类的方法。我正在尝试将以下 if 语句转换为 switch case:
String sentence;
if(sentence.contains("abcd")){
// do command a
}
else if(sentence.contains("efgh")){
// do command b
}
else if(sentence.contains("ijkl")){
// do command c
}
else{
//do command d
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助。
这是我第一次switch在 Javascript 中使用该语句。有没有一种方法可以在一个 switch 语句中评估多个条件,如下所示:
var i = 1;
switch(i && random(1)<0.3) {
case (1):
//code block
break;
case (2):
//code block
}
Run Code Online (Sandbox Code Playgroud)
那么如果两个条件都为真,代码块就会执行?
这与默认为 switch 语句中的第一个选项非常相似?,但关于 JS 而不是 PHP。
我有一个辅助函数,它为另一个函数包装了各种调用样式。被包装的函数至少需要一个,并且最多可以接受 4 个参数。这个最小代码示例很好地描述了我正在做的事情,它在我的 Chrome 和 FF 版本中运行良好。
function testSwitch(definition) {
var term, minLength, boundaryStart, boundaryEnd;
switch (definition.length) {
default:
case 4:
boundaryEnd = definition[3];
case 3:
boundaryStart = definition[2];
case 2:
minLength = definition[1];
case 1:
term = definition[0];
break;
case 0:
console.log('fail')
return;
}
console.log(term, minLength, boundaryStart, boundaryEnd);
}
Run Code Online (Sandbox Code Playgroud)
(在实际用例中,它们被传递给实际函数而不是 console.log)。所以这个想法是通过案例,允许提供太多参数(但忽略额外的参数),但如果提供的定义为空则失败。
所以我从我的测试中知道这似乎至少在某些浏览器中工作,我可以依靠它在最近的浏览器(例如 iOS/Android 设备、IE8+、Edge 等)上工作吗?我知道排除 break 语句是定义明确的行为并且使用相当广泛,但是我不太确定是否首先使用默认情况并且为空(例如,如果某些浏览器 JS 引擎会优化空默认情况,那么这不会对我有用,因为它在使用大小为 5 的数组调用时不起作用,例如)。
我的网站上有以下 JavaScript,以便在执行某些特定搜索时,答案会被硬编码到特定页面:
function redirect() {
var input = document.getElementById('searchBox').value.toLowerCase();
switch (input) {
case 'rectangular':
window.location.replace('http://www.Example.com/Rectangular/');
break;
case 'elephant':
window.location.replace('http://www.Example.com/Elephants/');
break;
case 'coils':
window.location.replace('http://www.Example.com/Parts/');
break;
default: // No keyword detected: submit the normal search form.
return true;
break;
}
return false; // Don't let the form submit
}
Run Code Online (Sandbox Code Playgroud)
我想知道 JavaScript 中的搜索语句是否与 case 语句的数量或恒定时间呈线性关系?如果是线性的,是否有更好的方法来编写此代码,以便无论我编码的特殊情况有多少,它都是恒定时间?
我有一系列嵌套if..else语句,我想用 case 语句替换它们。现在 R 有一个简单的 switch 运算符,如下所示:
switch (i) {
case 1:
// action 1
break;
case 2:
// action 2
break;
case 3:
// action 3
break;
default:
// action 4
break;
}
Run Code Online (Sandbox Code Playgroud)
然而,我的情况是更复杂的条件,而不是简单的文字值。
我需要类似的东西
switch {
case i %in% someList:
// action 1
break;
case i %in% someOtherList:
// action 2
break;
case i > 42:
// action 3
break;
default:
// action 4
break;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道R中是否可以实现这样的事情?这将使我正在编写的代码更易于阅读。
据我所知,这个问题没有在这里得到解答:How to use the switch statements …
#!/bin/bash
until [read command -eq "end"]
do
echo What command would like to run?
read command
if [$command -eq "my-tweets"]; then
node liri.js $command
fi
if [$command -eq "do-what-it-says"];then
node liri.js $command
fi
if [$command -eq "spotify-this-song"]; then
echo What item would like to query?
read item
node liri.js $command $item
fi
if [$command -eq "movie-this"]; then
echo What item would like to query?
read item
node liri.js $command $item
fi
done
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个 case/if 语句来在运行代码的下一部分之前检查变量的值。我想根据用户输入的值检查$command创建此 case/if 语句的值。我不断收到命令未找到错误。
switch-statement ×10
javascript ×4
if-statement ×2
java ×2
bash ×1
c++ ×1
double ×1
enums ×1
equality ×1
loops ×1
methods ×1
r ×1
return ×1
swift ×1
user-input ×1