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

时间:2021-01-29 23:57:49

标签: java discord discord-jda

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

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

我正在尝试从名为 Color.java 的单独类中的公共字符串方法中将字符串提取到名为 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();
        }
    }
}

我打算从这个文件 Color.java 中提取最后一个“else if”,从数组列表“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 新手。我做错了什么?

2 个答案:

答案 0 :(得分:0)

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

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

不应该是这样吗?

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

答案 1 :(得分:-1)

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

    }
}
相关问题