Java switch语句处理两个变量?

时间:2022-02-15 14:20:53

I'm looking for a way to handle two strings using a single switch, I'm thinking this impossible within Java.

我正在寻找一种方法来使用一个开关来处理两个字符串,我认为这在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;
    }

4 个解决方案

#1


8  

Using a single switch per your requirement is not possible. This is as close as your going to get using Java.

根据您的要求使用单个开关是不可能的。这与您使用Java的方法非常接近。

    switch (s1)  {
        case "a":  doAC(); break;
        case "b":  doBD(); break;
        default:
            switch(s2) {
                case "c":  doAC(); break;
                case "d":  doBD(); break;
            }
   }        

#2


4  

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.

唯一的方法是使用单个开关,首先合并两个值,找到具有最高优先级的值,并将转换应用到结果中。在您的示例中,最小的s1和s2,并切换结果。

Since "a" through "g" are an example, you'll need to have some meaningful way to rank the real values first.

因为“a”通过“g”是一个例子,你需要有一些有意义的方法来首先对实际值进行排序。

#3


4  

Have you considered not using the switch statement but instead using lookup tables?

您是否考虑过不使用switch语句,而是使用查找表?

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.

如果您需要一个不同的s2Value值集合,那么您可以构造第二个映射来提取这些值。

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


1  

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.

在进入交换机之前,s1="g"和s2="g",因此,唯一会对true进行评估的情况是case "g",因此s1Value和s2Value都将变成6,然后退出开关。

So, i think you're saying that s1 and s2 are initialized with the same value, so:

我想你是说s1和s2的初始值是相同的,所以:

  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

更新2

with s1="a" and s2="d" we consider case "a", with s1="c" and s2="b" we consider case "b"

s1="a"和s2="d"我们考虑"a", s1="c"和s2="b"我们考虑"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

它应该做这个魔术,测试它,让我知道它是否有效。

#1


8  

Using a single switch per your requirement is not possible. This is as close as your going to get using Java.

根据您的要求使用单个开关是不可能的。这与您使用Java的方法非常接近。

    switch (s1)  {
        case "a":  doAC(); break;
        case "b":  doBD(); break;
        default:
            switch(s2) {
                case "c":  doAC(); break;
                case "d":  doBD(); break;
            }
   }        

#2


4  

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.

唯一的方法是使用单个开关,首先合并两个值,找到具有最高优先级的值,并将转换应用到结果中。在您的示例中,最小的s1和s2,并切换结果。

Since "a" through "g" are an example, you'll need to have some meaningful way to rank the real values first.

因为“a”通过“g”是一个例子,你需要有一些有意义的方法来首先对实际值进行排序。

#3


4  

Have you considered not using the switch statement but instead using lookup tables?

您是否考虑过不使用switch语句,而是使用查找表?

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.

如果您需要一个不同的s2Value值集合,那么您可以构造第二个映射来提取这些值。

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


1  

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.

在进入交换机之前,s1="g"和s2="g",因此,唯一会对true进行评估的情况是case "g",因此s1Value和s2Value都将变成6,然后退出开关。

So, i think you're saying that s1 and s2 are initialized with the same value, so:

我想你是说s1和s2的初始值是相同的,所以:

  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

更新2

with s1="a" and s2="d" we consider case "a", with s1="c" and s2="b" we consider case "b"

s1="a"和s2="d"我们考虑"a", s1="c"和s2="b"我们考虑"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

它应该做这个魔术,测试它,让我知道它是否有效。