0

「 aab + bab = b 」という文字列があるとします。

  • aすべての文字を整数 0 に 置き換えます。
  • bすべての文字を整数 1 に置き換えます

したがって、次のようになります。

001 + 101 = 1

これを行う最も簡単な方法は何ですか?

これまでのところ、方程式を 3 つの部分に分割しました。

System.out.println("Enter an Equation of variables");
_inString = _in.nextLine();

    //find the three different parts of the equation
    String _noSpaces = _inString.replaceAll("\\s+","");
    String delims = "[+,=]";
    String[] _tokens = _noSpaces.split(delims);
4

4 に答える 4

0

単一の文字を他の単一の文字に置き換えたい場合は、使用
replace(char oldChar, char newChar)されている正規表現を回避するために 使用できます

お気に入り

_inString = _inString.replace('a', '0').replace('b', '1');
于 2013-09-07T13:19:22.643 に答える