为什么我的 java 函数没有从另一个 class 的输出中调用?

Why is my java function not calling from the output of another class?

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

我很困惑我应该如何从我创建的单独 class 中的特定方法中提取输出。

我正在尝试从名为 Color.java 的单独 class 中的 public 字符串方法中拉取字符串到名为 Commands.java 的文件中。该字符串旨在通过使用带有随机数生成器的数组列表来随机化。

这是我的 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

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

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

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

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

不应该是这样吗?

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

找到解决方案:在 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);

    }
}