java switch statements using label value -
can refer selected case in switch statements within case-block?
as clarification:
assume have function output(string)
outputs given string.
can old code transformed in new code (which not know correct syntax for)?
old code:
public void switchtest(string string) { case "car": output("car: car"); break; case "cat": output("animal: cat"); break; case "dog": output("animal: dog"); break; case "mouse": output("animal: mouse"); break; default: output("unknown"); break; }
proposed code, x
being our magical element:
public void switchtest(string string) { case "car": output("car: " + x); break; case "cat": case "dog": case "mouse": output("animal: " + x); break; default: output("unknown"); break; }
ps. using hypothetical example, don't worry if makes no sense.
i hope makes sense now, basicaly want refer value behind case tag.
regards.
what about
switch(string) { case "car": output("car: " + string); break; case "cat": case "dog": case "mouse": output("animal: " + string); break; default: output("unknown"); break; }
Comments
Post a Comment