switch-statement

go tutorial select statement

大兔子大兔子 提交于 2019-12-22 04:42:37
问题 I'm working through the examples at tour.golang.org, and I've encountered this code I don't really understand: package main import "fmt" func fibonacci(c, quit chan int) { x, y := 0, 1 for { select { case c <- x: // case: send x to channel c? x, y = y, x+y case <-quit: // case: receive from channel quit? fmt.Println("quit") return } } } func main() { c := make(chan int) quit := make(chan int) go func() { // when does this get called? for i := 0; i < 10; i++ { fmt.Println(<-c) } quit <- 0 }()

switch over value of enum: case expressions must be constant expressions

江枫思渺然 提交于 2019-12-22 04:03:46
问题 I have an enum with the following structure: public enum Friends { Peter("Peter von Reus", "Engineer"), Ian("Ian de Villiers", "Developer"), Sarah("Sarah Roos", "Sandwich-maker"); private String fullName; private String occupation; private Person(String fullName, String occupation) { this.fullName = fullName; this.occupation = occupation; } public String getFullName() { return this.fullName; } public String getOccupation() { return this.occupation; } } I would now like to use switch to

Java 7 switch statement with strings not working

﹥>﹥吖頭↗ 提交于 2019-12-22 03:48:51
问题 According to The Java Tutorials, in Java SE 7 and later, you can use a String object in the switch statement's expression. String s = ... switch(s){ //do stuff } But is this true? I've installed the JRE and added it to the build path of my Eclipse project, but I'm getting the following compile-time error: Cannot switch on a value of type String. Only convertible int values or enum constants are permitted Also, I think I've got it configured correctly since I was able to use its java.nio.file

Why java switch statement can't handle null, since it has a “default” clause? [duplicate]

懵懂的女人 提交于 2019-12-22 02:27:07
问题 This question already has answers here : Why doesn't String switch statement support a null case? (9 answers) Closed 2 years ago . Why java switch statement can't handle null, since it has a "default" clause? For example, if you have something like switch(value){ case VAL1: do_something1(); break; case VAL2: do_something2(); break; default: do_something3(); } shouldn't "default" deal with any other value, such as null? 回答1: As always, it's in the JLS: 14.11. The switch Statement All of the

C# grammar and switch wildcard

帅比萌擦擦* 提交于 2019-12-22 01:32:11
问题 I would like to add, that whenever it recognizes 'search X' it is going to search for 'X', but i don't know how i have to add that to the grammar, or how to do such a thing with my switch statement. private void Form1_Load(object sender, EventArgs e) { Choices commands = new Choices(); commands.Add(new string[] { "hello", "start chrome", "search" }); GrammarBuilder gBuilder = new GrammarBuilder(); gBuilder.Append(commands); gBuilder.Culture = new System.Globalization.CultureInfo("en-GB");

How switch cases are executed for strings?

徘徊边缘 提交于 2019-12-22 01:21:06
问题 I have a doubt in switch-case statement. Here is my code : String month = "April"; switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; and so on.. I have 3 questions in this context: 1) While comparing month with the case values i.e case "January", case "February" .. What is exactly used from the following by compiler ?? - month.equals("case-value") ? - month

UIButton as switch

天涯浪子 提交于 2019-12-22 01:17:24
问题 I'm trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS. The code I'm using is - (IBAction)btnAll:(id)sender { UIButton *button = (UIButton *)sender; button.selected = !button.selected; } That works fine for now. But my problem is, that when I'm toggling on, I press it on, then it is popping off again and then finally on. The app works, but that is really ugly, though. I firstly set the "highlighted" image to on. So when I highlight the button, it is on and

Finite State Machine In C

放肆的年华 提交于 2019-12-22 00:21:20
问题 I am trying to create a simple finite state machine in C and I'm quite confused on how to get started. I tried looking online but nothing has really cleared this up for me. My goal is to check if a string is octal, hex or a decimal. To be octal the string must start with a 0, followed by digits 0-7. To be hex the string must start with 0x or OX, followed by (a-f, A-F, 0-9) My attempt at creating the states would be: typedef enum { ERROR, OCTAL, HEX, DECIMAL } stringStates; Now, I would then

How to use ellipsis in c's case statement?

岁酱吖の 提交于 2019-12-21 17:27:26
问题 CASE expr_no_commas ELLIPSIS expr_no_commas ':' I saw such a rule in c's syntax rule,but when I try to reproduce it: int test(float i) { switch(i) { case 1.3: printf("hi"); } } It fails... 回答1: OK, this involves a bit of guesswork on my part, but it would appear that you're talking about a gcc extension to C that allows one to specify ranges in switch cases. The following compiles for me: int test(int i) { switch(i) { case 1 ... 3: printf("hi"); } } Note the ... and also note that you can't

indexed switch statement, or equivalent? .net, C#

一世执手 提交于 2019-12-21 13:04:10
问题 I want to build a method which accepts a string param, and an object which I would like to return a particular member of based on the param. So, the easiest method is to build a switch statement: public GetMemberByName(MyObject myobj, string name) { switch(name){ case "PropOne": return myobj.prop1; case "PropTwo": return myobj.prop2; } } This works fine, but I may wind up with a rather large list... So I was curious if there's a way, without writing a bunch of nested if-else structures, to