Java 迭代hashmap创建开关大小写

Java 迭代hashmap创建开关大小写,java,hashmap,Java,Hashmap,我做了一个HashMap​​这样: HashMap<M, Integer> t; 就像我有一张这样的桌子: public class M { int s; char c; } +-------+-------+---------------+ | s | c | Integer | +-------+-------+---------------+ +-------+-------+---------------+ | 0 |

我做了一个HashMap​​这样:

HashMap<M, Integer> t;
就像我有一张这样的桌子:

public class M {
    int s;
    char c;
}
+-------+-------+---------------+
|   s   |   c   |   Integer     |
+-------+-------+---------------+
+-------+-------+---------------+
|   0   |   0   |       1       |
+-------+-------+---------------+
|   0   |   1   |       0       |
+-------+-------+---------------+
|   0   |   3   |       5       |
+-------+-------+---------------+
|   1   |   0   |       2       |
+-------+-------+---------------+
|   1   |   1   |       0       |
+-------+-------+---------------+
|   2   |   0   |       3       |
+-------+-------+---------------+
|   2   |   1   |       0       |
+-------+-------+---------------+
|   3   |   0   |       3       |
+-------+-------+---------------+
|   3   |   1   |       3       |
+-------+-------+---------------+
价值观​​表中的数字是随机的

我想要的是创建一个字符串​​这样:

case 0:
    if(ch == '0')
        st = 1;
    if(ch == '1')
        st = 0; 
    if(ch == '3')
        st = 5;             
    else
        st = -1;
    break;
case 1:
    if(ch == '0')
        st = 2;
    if(ch == '1')
        st = 0; 
    else
        st = -1;
    break;
case 2:
    if(ch == '0')
        st = 3;
    if(ch == '1')
        st = 0; 
    else
        st = -1;
    break;
case 3:
    if(ch == '0')
        st = 3;
    if(ch == '1')
        st = 3; 
    else
        st = -1;
    break;
说明:

  • case
    的数量是它们之间不同的数量
  • 在任何
    情况下
    都有一个
    if
    。条件取决于列
    c
    ,主体取决于
    Integer
因此,在实践中:

案例(第“s”列)、如果(第“c”列)、正文(第“整数”列)

我写的这段代码非常有效:

String g  = new String("");

for(int i = 0; i < numberOfSt(); i++) {
    g = g + "\t\t\t\tcase " + i + ": \n";
    int j = 0;
    for(Map.Entry<Move, Integer> entry : t.entrySet()) {
        Move key = entry.getKey();
        Integer value = entry.getValue();
        if(key.s == j) {
            g = g + "\t\t\t\t\tif(ch == '" + key.c + "')\n";
            g = g + "\t\t\t\t\t\tstate = " + value + ";\n";
        }
        j++;
    }
    g = g + "\t\t\t\t\telse\n";
    g = g + "\t\t\t\t\t\tstate = -1;\n";
    g = g + "\t\t\t\t\tbreak;\n";
}
答案是错的

有人能帮我吗? 谢谢


(我希望我自己解释一下,否则我会补充你问我的细节。)

如果
st
是错误的。。。里面有什么?它应该包含什么?仅示例的
Integer
列的内容?顺便说一句:您在末尾使用了几个
if
块和一个
else
块。表示
else
块属于最后一个
if
块。要“连接”每个
if
块,请使用
else if
。(
if
->
else if
->
else if
->…->
else
)例如
案例1:if(ch='0')st=1
,但它应该是
案例1:if(ch='0')st=2
(如表中所示)…或者这不是正确的代码,或者您的结果不是来自此代码。我认为如果(key.s==j)条件只会被输入一次,我的测试证明了这一点。因此,在每种情况下,只有一个
if
块和一个紧跟其后的
else
块。请检查您发布的代码和结果。
case 0: 
    if(ch == '0')
        st = 1;
    if(ch == '1')
        st = 0;
    if(ch == '3')
        st = 0;
    else
        st = -1;
    break;
case 1: 
    if(ch == '0')
        st = 1;
    if(ch == '1')
        st = 0;
    else
        st = -1;
    break;
case 2: 
    if(ch == '0')
        st = 1;
    if(ch == '1')
        st = 0;
    else
        st = -1;
    break;
case 3: 
    if(ch == '0')
        st = 1;
    if(ch == '1')
        st = 0;
    else
        st = -1;
    break;