Java屏幕如何使输入行始终位于下方

时间:2018-12-12 19:18:42

标签: java

我的问题是: 当我用这种方法扫描用户输入时

public static void listenForCommand() {
    @SuppressWarnings("resource")
    final Scanner s = new Scanner(System.in);
    System.out.print(">");
    final String line = s.nextLine();

    String[] args = new String[line.split(" ").length-1];
    for (int i = 1; i < line.split(" ").length; i++)
        args[i-1] = line.split(" ")[i];

    commandEntered(line.split(" ")[0], args);
}

然后有“>” 但是如果某些东西被打印或记录下来,则看起来像这样  https://imgur.com/a/M5TE51b(我不能使用图片格式,因为它说:“您至少需要10个信誉才能发布图片。”并且我没有10个声望。)

那我该如何解决呢?是否有一些API或库?我希望它看起来像钉子或bukkit。

我的命令侦听器类: 控制台获得输入后,它将把第一个单词拆分为命令,其他所有参数均为args。然后,它会询问主机中每个已注册的命令,是否存在已使用输入的命令名注册的命令。

package at.gebes.utils.command;

import java.util.Scanner;

import at.gebes.bot.Bot;

public final class CommandListener {

public static String[] commandNames = new String[1000];
public static String[] commandDescriptions = new String[1000];
private static CommandExecutor[] CommandClasses = new CommandExecutor[1000];
public static int counter = -1;

public static void registerCommand(final String CommandName, final String CommandDescription, final CommandExecutor CommandClass) {

    counter++;
    commandNames[counter] = (CommandName);
    commandDescriptions[counter] = (CommandDescription);
    CommandClasses[counter] = CommandClass;

}

public static void listenForCommand() {
    @SuppressWarnings("resource")
    final Scanner s = new Scanner(System.in);
    System.out.print(">\n");
    final String line = s.nextLine();
    //final String line = System.console().readLine();

    String[] args = new String[line.split(" ").length-1];
    for (int i = 1; i < line.split(" ").length; i++)
        args[i-1] = line.split(" ")[i];

    commandEntered(line.split(" ")[0], args);
}

public static void commandEntered(final String cmd, final String[] args) {

    if (counter < 0) {
        return;
    }

    boolean commandExists = false;
    try {

        for (int i = 0; i <= counter; i++) {

            if (commandNames[i].equalsIgnoreCase(cmd)) {
                CommandClasses[i].onCommand(commandNames[i], args);
                commandExists = true;
                break;
            }
        }

    } catch (final NullPointerException e) {
        e.printStackTrace();
    }

    if (!commandExists) {
        Bot.getLogger().info("Unknown Command. Try \"help\" for a list of commands.");
    }
}

}

1 个答案:

答案 0 :(得分:0)

在这种情况下,您必须在任何String类型变量中记录当前屏幕,然后每次用户输入时都必须清除屏幕,然后显示记录的屏幕,然后显示输入行...

我想你明白我的意思。