java - System.out.print("") 在控制台被 Scanner(System.in) 停止之前不打印,ei 光标只是在等待
问题描述
这是代码:
private boolean getPlayerAction() {
while (player.getHandValue() < 21) {
System.out.print("Do you want to Hit or Stand?: ");
char c = getInput();
switch (c) {
case 'h' -> {
player.setCard(dealer.dealCard());
System.out.println(player.toString());
}
case 's' -> {
return true;
}
default -> System.out.println("Invalid entry, try again.");
}
}
if (player.getHandValue() > 21) {
System.out.println("Sorry. You bust.");
return false;
}
return true;
}
private char getInput() {
String input = in.nextLine();
if (!input.isEmpty()) return input.toLowerCase().charAt(0);
return 'b';
}
这是输出:
Dealer is showing 10 with QS
You have 13 with 3H JC
它应该有提示:“Do you want to Hit or Stand?:”空白行在哪里。如果我点击输入,输出如下所示:
Dealer is showing 10 with QS
You have 13 with 3H JC
Do you want to Hit or Stand?: Invalid entry, try again.
附加说明,我尝试使用 println() 而不是 print() 运行它,它可以正常工作,但是使用 print() 我得到了时髦的怪异。
解决方案
您可能需要刷新输出才能看到它出现在您希望的位置。
尝试添加:
System.out.flush();
就在你之后System.out.print("Do you want to Hit or Stand?: ");
推荐阅读
- html - CSS - 打破网格元素并在列中拉伸到最宽
- linux - Docker(Linux)错误:即使我有足够的空间,设备上也没有剩余空间
- c# - Xunit 和 EFCore - 模拟一对多关系不起作用
- sql - SQL 为每个组选择最近的记录
- python-3.x - 这个程序的时间复杂度是多少
- c# - SendMessage 的会话十进制到十六进制值的问题
- yocto - STM32MP157C-DK2 上的 YOCTO (dunfell) 和 Mender
- python - 允许点击某个框
- azure - 有没有办法将实验从 azure ml 设计器工作区导出到另一个?
- node.js - 我无法使用 Socket.io 在我的页面中设置套接字