switch-statement

Switch Case To/Between

て烟熏妆下的殇ゞ 提交于 2019-12-30 05:29:13
问题 Is there a way in Javascript to compare one integer with another through switch case structures without using if statements? E.g. switch(integer) { case 1 to 10: break; case 11 to 20: break; case 21 to 30: break; } 回答1: You can do some math manipulations. switch(Math.ceil(integer/10)) { case 1: // Integer is between 1-10 break; case 2: // Integer is between 11-20 break; case 3: // Integer is between 21-30 break; } 回答2: There is a way, yes. I'm pretty sure I'd use an if/else structure in my

How to save (switch) button state in android?

*爱你&永不变心* 提交于 2019-12-30 05:01:07
问题 I am using SWITCH (like android toggle button ) instead of normal buttons in my andorid app. The code works fine while enabling and disabling switches. But i want to store the state of the switch. Suppose i enable the switch and close my application the background code will run fine but the switch state will change to disabled. Every time when i close the application the switch state becomes disabled. Is there any way to store the switch State?? 回答1: Use shared preferences or a database to

PHP CASE statement not working with ZERO values

拟墨画扇 提交于 2019-12-30 04:50:34
问题 I don't understand what's happening here. Logically, it doesn't make any sense to me. <?php $level = 0; switch ($level) { case $level > 80: $answer = 'high'; break; case $level > 60: $answer = 'moderate-to-high'; break; case $level > 40: $answer = 'moderate'; break; case $level > 20: $answer = 'low-to-moderate'; break; default: $answer = 'low'; break; } echo $answer; ?> When $level == 0, it returns "high". This doesn't make any sense to me. Can someone explain what's happening here? 回答1:

Using string representations of enum values in switch-case

夙愿已清 提交于 2019-12-30 03:58:06
问题 Why is it not possible to use enum values as strings in a switch case? (Or what is wrong with this:) String argument; switch (argument) { case MyEnum.VALUE1.toString(): // Isn't this equal to "VALUE1" ? // something break; case MyEnum.VALUE2.toString(): // something else break; 回答1: You can only use strings which are known at compile time. The compiler cannot determine the result of that expression. Perhaps you can try String argument = ... switch(MyEnum.valueOf(argument)) { case VALUE1: case

if else vs switch performance in java

廉价感情. 提交于 2019-12-29 08:43:11
问题 I'd like to know if there is any efficiency difference between using if statement or switch. For example: if(){ //code } else if(){ //code } else{ //code } I believe that program needs to go and check all of the if statement even if the first if statement was true. switch(i){ case 1: //code break; case 2: //code break; But in the switch, there is a break command. Is my approaching right? If not, could you explain the efficiency difference between them? 回答1: Switch perf is better than if else

Swift: Switch case with multiple patterns cannot bind to variable

元气小坏坏 提交于 2019-12-29 06:27:09
问题 In the official Swift Programming Language guide, it has this to say about switch case: "...if the case contains multiple patterns that match the control expression, none of those patterns can contain constant or variable bindings." What does it mean by containing multiple patterns? 回答1: It means that case labels with multiple patterns cannot declare variables. This is allowed: let somePoint = (1, 1) switch somePoint { // Case with multiple patterns without binding case (0, _), (_, 0):

Swift - determine which button was pressed with switch

穿精又带淫゛_ 提交于 2019-12-29 06:23:09
问题 I have 4 buttons that call one function. Depending on which button was pressed i need to hide button inside of function that called after pressing.I dont know which button was pressed so i tried to assign tags for each of 4 buttons to identify them by tag and use switch I tried this switch sender.tag { case 1: self.button1.hidden = true case 2: self.button2.hidden = true case 3: self.button3.hidden = true case 4: self.button4.hidden = true } but this doesnt work compiler says about use of

final variable case in switch statement

廉价感情. 提交于 2019-12-29 05:46:31
问题 final int a = 1; final int b; b = 2; final int x = 0; switch (x) { case a:break; // ok case b:break; // compiler error: Constant expression required } /* COMPILER RESULT: constant expression required case b:break; ^ 1 error */ Why am I getting this sort of error? If I would have done final int b = 2 , everything works. 回答1: b may not have been initialized and it is possible to be assigned multiple values. In your example it is obviously initialized, but probably the compiler doesn't get to

C# how to use enum with switch

杀马特。学长 韩版系。学妹 提交于 2019-12-28 11:42:11
问题 I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator. public enum Operator { PLUS, MINUS, MULTIPLY, DIVIDE } public double Calculate(int left, int right, Operator op) { int i = (int) op; switch(i) { case 0: { return left + right; } case 1: { return left - right; } case 2: { return left * right; } case 3: { return left / right; } default: { return 0.0; } } } The end

Odd behaviour in a switch statement

我是研究僧i 提交于 2019-12-28 06:57:06
问题 I'm writing code for a sortable table, where clicking the links in the header change the ORDER BY executed when generating a set of search results (the case where there there is no valid order by causes the query to not be run with order by and just return the results in the order the database returns. this is as designed). The code is being written within a framework provided by my employer. To validate the ORDER BY part of the query I run the input through the following validation function.