【问题标题】:Is it possible to display an output with a scanner above the output?是否可以在输出上方使用扫描仪显示输出?
【发布时间】:2020-06-02 06:24:12
【问题描述】:

我这里有一个程序,它只包括扫描仪和输出。我希望程序显示的是显示具有以下输出的扫描仪。

public static void main(String[] args) {


    Scanner scan = new Scanner(System.in);

    System.out.print("Enter anything: ");
     scan.nextLine();

    //while the scanner's text cursor shows, the bottom texts below also shows.

    System.out.println("bottom text");
    System.out.println("bottom text");      
    System.out.println("bottom text");
    System.out.println("bottom text");
    System.out.println("bottom text");
    System.out.println("bottom text");
    System.out.println("bottom text");



/*  what I wanted the output to show is this:

Enter anything: | << text cursor
bottom text
bottom text
bottom text
bottom text
bottom text
bottom text
bottom text



but the output shows like this instead:

Enter anything: |


*/      




}//end of method



}

因为它只在使用扫描仪后显示下面的输出。是否有可能或有什么方法可以同时显示扫描仪和底部文本? TIA :)

【问题讨论】:

  • 我认为这应该可以使用 VT100 代码
  • @Sadap 感谢您的提示,我对其进行了研究,它可能对显示有所帮助。

标签: java output java.util.scanner


【解决方案1】:

我试过了……

CompletableFuture 将使用runAsync( () -&gt; { return null; } ) 衍生一个新线程,如果您想从线程中获取结果,也可以使用supplyAsync(() -&gt; { returns anObject })。然后,您可以链接其他线程,这些线程可以是消费者/供应商或两者兼而有之。

public static void main(String[] args) {
        CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(100); // delay start by 1/10 of a second.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println();
            System.out.println("bottom text");
            System.out.println("bottom text");
            System.out.println("bottom text");
            System.out.println("bottom text");
        });

        System.out.println("Enter anything: ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        System.out.println("You entered: " + input);
    }

不幸的是,当我将光标放在 Enter anything: 的末尾时,光标直接移到了底部,所以输出不是你想要的......

Enter anything: 

bottom text
bottom text
bottom text
bottom text
something            <<<<<< My input here
You entered: something

Process finished with exit code 0

我不能 100% 确定您尝试执行的操作是否可以通过如此简单的一组命令来实现。我想你需要更全面的东西来实现你想要的。

也许有人可以对此进行改进?

【讨论】:

  • 我确定它与用户界面有关。我认为除非用户提示输入,否则任何控制台都会显示文本光标而其下方没有输出。顺便说一句,感谢您测试我的脚本 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多