问题
I'm looking for a way to handle two strings using a single switch, I'm thinking this impossible within Java.
Here is some pseudo code of the kind of thing I want to achieve with a switch only.
int s1Value = 0;
int s2Value = 0;
String s1 = "a";
String s2 = "g";
switch (s1 || s2) {
case "a": s1value = 0; s2value = 0; break;
case "b": s1value = 1; s2value = 1; break;
case "c": s1value = 2; s2value = 2; break;
case "d": s1value = 3; s2value = 3; break;
case "e": s1value = 4; s2value = 4; break;
case "f": s1value = 5; s2value = 5; break;
case "g": s1value = 6; s2value = 6; break;
}
回答1:
Using a single switch per your requirement is not possible. This is as close as your going to get using Java.
switch (s1) {
case "a": doAC(); break;
case "b": doBD(); break;
default:
switch(s2) {
case "c": doAC(); break;
case "d": doBD(); break;
}
}
回答2:
The only way to do that with a single switch is to first merge the two values, finding the one with the highest precedence, and applying the switch to the result. In your example, the minimum of s1 and s2, and switching on the result.
Since "a" through "g" are an example, you'll need to have some meaningful way to rank the real values first.
回答3:
Have you considered not using the switch statement but instead using lookup tables?
public class MyClass {
private static final Map<String, Integer> valuesMap;
static {
Map<String,Integer> aMap = new HashMap<>();
aMap.put("a", 0);
aMap.put("b", 1);
..... rest .....
valuesMap = Collections.unmodifiableMap(aMap);
}
public void foo()
{
int s1Value = 0;
int s2Value = 0;
String s1 = "a";
String s2 = "g";
if( valuesMap.containsKey(s1) )
{
s1Value = valuesMap.get(s1);
s2Value = s1Value;
}
else if( valuesMap.contansKey(s2) )
{
s1Value = valuesMap(s2);
s2Value = s1Value;
}
}
}
If you needed a different set of values of the s2Value then you could construct a second map to pull those from.
Since you wanted to use a switch I take that to mean the possible candidates and values is a fixed, known at development time list, so using a statically initialized map like this shouldn't be an issue.
回答4:
You can do what you want without using a switch, simply by writing that:
s1value = s1.charAt(0)-'a'; //returns 0 if s1value="a", 1 if s1value="b", etc.
What i don't completely understand is what do you mean with
(s1 || s2)
Do you want to do something like this?
int s1Value = 0;
int s2Value = 0;
String s1 = "g";
String s2 = "a";
char s1char = s1.charAt(0);
char s2char = s2.charAt(0);
int myChar = Math.max(s1char, s2char);
s1Value = s2Value = myChar - 'a';
UPDATE
In comments, you wrote:
s1="g" and s2="g" before entering the switch, then the only case that will evaluate to true is case "g" and so both s1Value and s2Value will become 6 and then exit the switch.
So, i think you're saying that s1 and s2 are initialized with the same value, so:
int s1Value = 0;
int s2Value = 0;
String s1 = "g";
String s2 = "g";
char s1char = s1.charAt(0);
s1Value = s2Value = s1char - 'a'; //s1Value = s2Value = 6
This should do what you want
UPDATE 2
with s1="a" and s2="d" we consider case "a", with s1="c" and s2="b" we consider case "b"
Try this:
int s1Value = 0;
int s2Value = 0;
String s1 = "g";
String s2 = "a";
char s1char = s1.charAt(0);
char s2char = s2.charAt(0);
int myChar = Math.min(s1char, s2char);
s1Value = s2Value = myChar - 'a';
It should do the trick, test it and let me know if it works
来源:https://stackoverflow.com/questions/15222918/java-switch-statement-to-handle-two-variables