如何将局部变量更改为全局变量?

时间:2015-10-03 19:30:46

标签: java string variables

所以我正在编辑游戏的源代码,我有一个阅读游戏聊天的事件。我需要游戏来阅读聊天,从聊天中复制一些单词,输入带有第一个单词的聊天消息,稍等一下,输入带有第二个单词的聊天消息等。 现在,代码看起来像这样:

private final long PERIOD = 4000L; //timer
private long lastTime = System.currentTimeMillis() - PERIOD; 
@SubscribeEvent
    public void onChat(final ClientChatReceivedEvent e) { //look at the chat
        final String chat = e.message.getUnformattedText(); 
          if(chat.startsWith("Something")) { //if the chat message starts with "Something"
                //Create the string chatp1 being "Something 123"
                String click = chatp1.replace("Something ", ""); //Creates a string "click", which is chatp1 without the "Something ", so it's "123"
//After it basically does the same thing multiple times, but instead of "Something" it's "Something1" and instead of "123" it's "124" etc.
//Then I need to run the commands
MyGame.getMyGame().thePlayer.sendChatMessage("/Command " + click); //runs the command "/Command 123"
          long thisTime12 = System.currentTimeMillis();
            if ((thisTime12 - lastTime) >= PERIOD) { //waits
                    lastTime = thisTime12;
MyGame.getMyGame().thePlayer.sendChatMessage("/Command " + click2); //Runs the command "/Command 124"
//etc.

现在出现了一个问题:“点击”和“click2”无法找到,这很明显:这些是局部变量,而不是全局变量。我的问题是:更改代码的最简单方法是什么,以便在代码中找到“click”和“click2”?我应该在开始时将它们声称为全局变量(如果是,如何?)或其他什么?

2 个答案:

答案 0 :(得分:0)

将他们的声明移到课堂上,这样他们就成了田野。

答案 1 :(得分:0)

以下是答案:

private final long PERIOD = 4000L; //timer
private long lastTime = System.currentTimeMillis() - PERIOD; 
String click = "";
String click2 = "";
@SubscribeEvent
    public void onChat(final ClientChatReceivedEvent e) { //look at the chat
        final String chat = e.message.getUnformattedText(); 
          if(chat.startsWith("Something")) { //if the chat message starts with "Something"
                //Create the string chatp1 being "Something 123"
                click = chatp1.replace("Something ", ""); //Creates a string "click", which is chatp1 without the "Something ", so it's "123"
//After it basically does the same thing multiple times, but instead of "Something" it's "Something1" and instead of "123" it's "124" etc.
//Then I need to run the commands
MyGame.getMyGame().thePlayer.sendChatMessage("/Command " + click); //runs the command "/Command 123"
          long thisTime12 = System.currentTimeMillis();
            if ((thisTime12 - lastTime) >= PERIOD) { //waits
                    lastTime = thisTime12;
MyGame.getMyGame().thePlayer.sendChatMessage("/Command " + click2); //Runs the command "/Command 124"
//etc.