gpt4 book ai didi

java - 是否可以在输出上方使用扫描仪显示输出?

转载 作者:行者123 更新时间:2023-12-01 16:26:02 25 4
gpt4 key购买 nike

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

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:)

最佳答案

我试过这个...

CompletableFuture 将使用 runAsync( () -> { return null; } ) 分拆一个新线程,您也可以使用 supplyAsync(() -> { 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: 

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

Process finished with exit code 0

我不能 100% 确定使用如此简单的一组命令可以实现您想要做的事情。我想您需要更全面的东西来实现您想要的。

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

关于java - 是否可以在输出上方使用扫描仪显示输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62146057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com