sách gpt4 ai đã đi

java - JLine2 中的错误? ConsoleReader.clearScreen

In lại 作者:行者123 更新时间:2023-12-02 08:20:34 30 4
mua khóa gpt4 Nike

<小时>编辑:这个 feature仅当我调用 ConsoleReader của clearScreen 方法时才会发生!任何其他更改都不会产生影响。那么这是 JLine2 中的错误吗?

<小时>

JLine2:

为什么当我运行这个程序时,我会得到两个紧随其后的控制台提示符 (----> ---->)?是因为正在创建两个控制台吗?我不明白怎么办。
我在这里看不到什么?

import java.io.IOException;
import jline.console.ConsoleReader;

class TextUi implements Ui {
private static final String prompt1 = "----> ";
public void homeScreen() {
thử {
ConsoleReader con = new ConsoleReader();
con.setPrompt(prompt1);

con.clearScreen();
System.out.println("Press any key to continue...");
con.readCharacter();
con.clearScreen();

System.out.println("Here is a prompt. Do something and press enter to continue...");
String line = con.readLine();
con.clearScreen();

System.out.println("You typed: ");
System.out.println(line);
System.out.println("Press any key to exit. ");
con.readCharacter();
con.clearScreen();
} bắt (IOException e) {
e.printStackTrace();

}
}
public void exitSplash() {
System.out.println("Thank You. Goodbye.");
System.out.println("");
}
public void creditsScreen() {
}
public static void main (String argv[]) {
TextUi ui = new TextUi();
ui.homeScreen();
ui.exitSplash();
}
}

1 Câu trả lời

这不是一个错误,您只需在每次调用 con.clearScreen() 后调用 con.flush() 即可。

clearScreen 方法不会自动调用 flush()(在某些情况下可能无需刷新即可工作),但 readLine 方法会自动调用 flush() ,因此只有当您调用 con.readLine() 时,屏幕实际上才会被清除。这导致最后一个 System.out.println (在 readLine 之前)被清除,即使它是在 con.clearScreen() 之后调用的.

thử block 内的代码应更改为:

ConsoleReader con = new ConsoleReader();
con.setPrompt(prompt1);

con.clearScreen();
con.flush();
System.out.println("Press any key to continue...");
con.readCharacter();
con.clearScreen();
con.flush();

System.out.println("Here is a prompt. Do something and press enter to continue...");
String line = con.readLine();
con.clearScreen();
con.flush();

System.out.println("You typed: ");
System.out.println(line);
System.out.println("Press any key to exit. ");
con.readCharacter();
con.clearScreen();
con.flush();

关于java - JLine2 中的错误? ConsoleReader.clearScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710787/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com