我有一个"枚举"声明如下:
var PlaceType = {
PASSABLE_TERRAIN: 1,
IMPASSABLE_TERRAIN: 0,
SOMEWHAT_PASSABLE_TERRAIN: 2,
PATH: 3
};
Run Code Online (Sandbox Code Playgroud)
和一个声明如下的函数:
setPlaceType(placeType) {
this.clear = false;
this.placeType = placeType;
alert("before switch "+(PlaceType.SOMEWHAT_PASSABLE_TERRAIN==this.placeType));
switch(this.placeType) {
case PlaceType.PASSABLE_TERRAIN: {
alert("Case PASSABLE");
break;
}
case PlaceType.IMPASSABLE_TERRAIN: {
alert("Case IMPASSABLE");
break;
}
case PlaceType.SOMEWHAT_PASSABLE_TERRAIN: {
alert("Case SOMEWHAT_PASSABLE");
break;
}
case PlaceType.PATH: {
alert("Case PATH");
break;
}
default: {
alert("case default");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我这样称呼它:
setPlaceType(1);
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:"在切换为true之前","case default"
如果我这样称呼它:
setPlaceType(2);
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:"在切换错误之前","大小写默认"
换句话说,使用正确的参数调用函数,当执行(在我看来)与开关相同的比较时,通过"=="我得到正确的行为,但是开关永远不会匹配值在适当的情况下.有人知道为什么吗?
是否可以编写 switch case 代码而无需在 switch 中指定变量?假设我有两个变量 int x 和 int y。我想根据 x 和 y 的不同组合有条件地运行代码。例如
switch
{
case (x==1 && y==1): //do something.
break;
case (x==3 && y==-1): //do something.
break;
case (x==0 && y==3): //do something.
break;
default: //do something.
break;
}
Run Code Online (Sandbox Code Playgroud)
这段代码给我错误。
请考虑以下代码:
if (xPoint > 0 && yPoint > 0) {
m_navigations = Directions.SouthEast;
}
else if (xPoint > 0 && yPoint < 0) {
m_navigations = Directions.NorthEast;
}
else if (xPoint < 0 && yPoint > 0) {
m_navigations = Directions.SouthWest;
}
else if (xPoint < 0 && yPoint < 0) {
m_navigations = Directions.NorthWest;
}
else if (xPoint == 0 && yPoint < 0) {
m_navigations = Directions.North;
}
else if (xPoint == 0 && yPoint > 0) { …Run Code Online (Sandbox Code Playgroud) 我看到了一些像这样的c代码:int check = 10:
switch(check) {
case 1...9: printf("It is 2 to 9");break;
case 10: printf("It is 10");break;
}
Run Code Online (Sandbox Code Playgroud)
这case 1...9:是什么意思?它是标准的吗?
我有2个Java类:
public abstract class IconNames {
/**
*
*/
public static final String ButtonFett = java.util.ResourceBundle.getBundle("recources/buttonproperties").getString("fett");
}
Run Code Online (Sandbox Code Playgroud)
和
public class EditorPanelActionListener implements ActionListener{
.
.
.
String buttonText = e.getActionCommand();
switch(buttonText)
{
case IconNames.ButtonFett: //Error: constant string expression required
replace(XmlTags.BOLD);
break;
}
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
EditorPanelActionListener触发错误"需要常量字符串表达式",问题是什么?
谢谢!
Greetings Stack Overflow用户,今晚我来找你,帮助我创建一个Java程序.我对Java比较新,所以请原谅我对这个主题的无知.我制作了一个Java程序,它是一个"Rock""Paper""Scissors"游戏,其中一个语句中似乎有错误.
import java.util.Scanner;
public class TheAntlers {
public static void main(String[] args) {
int playerHumanWins = 0;
int playerComputerWins = 0;
int numberOfTies = 0;
int computerResult;
Scanner input = new Scanner(System.in);
while(true) {
String startGame;
String playerHuman;
String playerComputer = " ";
System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
startGame = input.nextLine();
startGame = startGame.toUpperCase();
if(startGame.equals("N")) {
System.out.println("NO!");
break;
}
else if(! startGame.equals("Y")) {
startGame = startGame.toLowerCase();
System.out.println("Sorry, " + startGame + " is …Run Code Online (Sandbox Code Playgroud) 我们遇到了间歇性崩溃(有时会在某些设备上发生),我们很难确定该崩溃,并且无法按需重现。这与 Swift 3 和 WKWebView 组件有关,特别是当尝试通过 switch 语句获取错误代码时,其回调协议崩溃。见下文:
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
if let err = error as? URLError {
switch(err.code) { // Exception occurs on this line
case .cancelled:
Hint(hide: true)
case .cannotFindHost:
Hint(hide: false, hint:.CannotFindHost)
case .notConnectedToInternet:
Hint(hide: false, hint: .NoInternet)
case .resourceUnavailable:
Hint(hide: false)
case .timedOut:
Hint(hide: false)
default:
Hint(hide: false)
print("error code: " + String(describing: err.code) + " does not fall under known failures")
}
}
}
func Hint(hide: …Run Code Online (Sandbox Code Playgroud) 以这篇 StackOverflow 文章为例,我实现了一个类型化事件系统。简化后,它看起来像:
interface MyTypeMap {
FOO: string;
BAR: number;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用此地图创建一个事件处理程序:
function handleEvent<T extends keyof MyTypeMap>(eventKey: T, eventMsg: MyTypeMap[T]) {
switch(eventKey) {
case('FOO'):
// TS believes that eventKey is of type 'never'
// TS believes that eventMsg is 'string|number'
break;
case('BAR'):
// TS believes that eventKey is of type 'never'
// TS believes that eventMsg is 'string|number'
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当外部调用此函数时,TypeScript 的行为与预期一致。例如:
// These work as desired
handleEvent('FOO', 'asdf');
handleEvent('BAR', 5);
// These throw compile errors as …Run Code Online (Sandbox Code Playgroud) 假设我创建了一个枚举,例如下面的:
public enum MyEnum {
A,
B,
C;
}
Run Code Online (Sandbox Code Playgroud)
并且有一个 switch 语句(如下所示),其中每个枚举值都有一个 case 语句,但我还添加了一个默认值来抛出 IllegalArgumentException。(如果添加了一个新的枚举值,那么如果新值被传递,那么有人更有可能会注意到该 case 语句出了问题。我的想法是异常比日志或尝试处理新值更引人注目案件。)
switch(myEnum) {
case A:
//do something
break;
case B:
//do something
break;
case C:
//do something
break;
default:
throw new IllegalArgumentException("Unrecognized enum type!");
}
Run Code Online (Sandbox Code Playgroud)
除非我遗漏了某些东西,否则从理论上讲,如果不添加新的枚举值或更改 switch 语句,默认值应该是无法访问的。
所以我想知道的是,如果我正在编写代码覆盖率的 JUnit 测试,是否有办法强制切换到默认语句而不更新应用程序代码?
C++20 具有指导代码生成的方便[[likely]]/[[unlikely]]属性。例如,您可以指定一个分支可能被以下人员采用:
if (b) [[likely]] { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
同样,可以在switchstatements 中使用这些属性。. . 不知何故? 文档建议使用以下示例(稍微格式化):
if (b) [[likely]] { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
其含义显然是[[likely]]/[[unlikely]]在case语句之前。互联网似乎几乎普遍宣传这种用法。
但是,请考虑以下类似的代码(我所做的只是将 the[[likely]]移到 other case):
switch (i) {
case 1:
[[fallthrough]];
[[likely]] case 2:
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这无法在 clang 上编译!虽然这可能与编译器错误有关[[fallthrough]],但它让我查看了标准。在相关的标准有如下的例子(见§VII):
鼓励实现针对正在执行的情况进行优化(例如,以下代码中的值为 1):
switch (i) {
[[likely]] case 1:
[[fallthrough]];
case 2:
return 1;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,该属性出现在案例标签之后,而不是之前。
所以 。. . …
switch-statement ×10
java ×5
case ×2
enums ×2
attributes ×1
c ×1
c++ ×1
c++20 ×1
constants ×1
exception ×1
generics ×1
inference ×1
ios ×1
javascript ×1
swift ×1
typescript ×1
typing ×1
webkit ×1
while-loop ×1