【问题标题】:Why is my java function not calling from the output of another class?为什么我的 java 函数没有从另一个类的输出调用?
【发布时间】:2021-05-03 21:24:45
【问题描述】:

我正在编写一个不和谐的机器人,以便在私人服务器中使用,只是为了搞乱。这是我第一次接触java。我正在使用 Discord JDA 库对机器人进行编码。不过,我认为这不是主要问题。

我对如何从我创建的单独类中的特定方法提取输出感到困惑。

我试图从一个名为 Color.java 的单独类中的公共 String 方法中提取一个 String 到一个名为 Commands.java 的文件中。该字符串旨在通过使用带有随机数生成器的 Array List 进行随机化。

这是我的 Commands.java 代码。这不是主文件,而是与问题相关的文件,更具体地说是此代码的最后一个 else if {}。

public class Commands extends ListenerAdapter {
    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {

        String[] args = event.getMessage().getContentRaw().split(" ");

        if (args[0].equalsIgnoreCase(bot.prefix + "info")) {
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage("This is a test info description").queue();
        }

        else if (args [0].equalsIgnoreCase(bot.prefix + "ping")) {
            long ping = event.getJDA().getGatewayPing();

            event.getChannel().sendMessage(ping + "ms").queue();
        }

        else if (args [0-100].equalsIgnoreCase("white")){
            
            Race newColorobj = new Color();
            String white_test = newColorobj.white();
            event.getChannel().sendMessage(white_test + ".").queue();
        }
    }
}

我打算从这个文件中提取最后的“else if”,Color.java,从数组列表“white”中挑选出一个随机的文本字符串,并将其输出到不和谐的聊天频道中。

public class Color {

    Random rand = new Random();
    int upperbound = 1;

    int int_random = rand.nextInt(upperbound);

    public String white() {

        ArrayList<String> white = new ArrayList<String>();

        white.add("This is a test");

        return white.get(int_random);

    }
}

我的终端在编译的时候输出这个错误,但是还是成功运行了:

white : The term 'white' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try      
again.
At line:1 char:1
+ white c:; cd 'c:\Users\Colin\Dylan and Colin'; & 'c:\Users\Colin\.vsc ...
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (white:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果用户试图对机器人说“白色”,它不会以我想要它响应的文本字符串响应。

我还是 Java 新手。我做错了什么?

【问题讨论】:

  • 错误信息与问题中的代码不相关。请出示相关的类和代码。

标签: java discord discord-jda


【解决方案1】:

我要假设命令 ping 和 info 正常工作?无论如何,我认为你的问题就在这里

 else if (args [0-100].equalsIgnoreCase("white"))

不应该就这样吗?

else if (args [0].equalsIgnoreCase("white"))

【讨论】:

  • 如果执行的 OP 会得到 ArrayIndexOutOfBoundsException,但没有,所以这是一个问题,但不是 OP 问题的答案
  • 我认为拥有 [0-100] 的关键字最多可以查看 100 个参数。因为 [0] 意味着它只查看消息中的第一个单词。
  • @colinkehoe 没有。 args[0-100]args[-100] 相同,任何负数索引都会抛出索引越界异常。
  • 很高兴知道。我通过使用“i”循环并使用 args[i] 读取接收到的消息中的每个单词来解决这个问题。
【解决方案2】:

找到的解决方案:在 Color.java 中,我需要将 public String white(){} 更改为 public String white(String... args){}。

public class Color {

    Random rand = new Random();
    int upperbound = 1;

    int int_random = rand.nextInt(upperbound);

    public String white(String... args) {

        ArrayList<String> white = new ArrayList<String>();

        white.add("This is a test");

        return white.get(int_random);

    }
}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-08-26
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多