2

プログラムの実行中に文字列を文字列に置き換える方法がわかりません。これは可能ですか?プログラムの実行中に単語を別の単語に置き換えて、その行から実行を続けたいと考えています。

どうすればこれを行うことができますか? 皆さんありがとう

hstring.replace("picstart", "up1");
g.drawPixmap(Assets.picstart , 128, 160);
4

3 に答える 3

3

コードは次のようになります。

if (condition) {
    g.drawPixmap(Assets.picstart , 128, 160);
} else {
    g.drawPixmap(Assets.up1 , 128, 160);
}
于 2013-01-24T20:13:15.977 に答える
1

リフレクションは使用しないでください。アセットコードが画像やテクスチャなどを処理すると仮定すると、次のような場合があります。

String hstring = "picstart";
// ... stuff happens
// that forces us to change hstring! ...
hstring = hstring.replace("up1"); // or you could just say hstring = "up1";
g.drawPixmap(Assets.getAssetFor(hstring), 128, 160);

次に、静的Assetクラスで次のことができます。

public PixelMap getAssetFor(String identifier) {
     if (identifier.equals("picstart") {
         return new PicStartPixelMap();
     }
     else if (identifier.equals("up1")) {
         return new UpOnePixelMap();
     }
}
于 2013-01-24T20:16:08.013 に答える
1

Stringつまり、結果を変数に代入する必要があります。

hstring = hstring.replace(...);
于 2013-01-24T20:11:26.950 に答える