java适当的输入输出
你好,我一直在想实现一个函数的正确方法,这个函数有以下功能
1程序启动时,显示文本以输入键或将字段留空
2如果输入了文本,则执行A
3如果未输入文本,则执行B
这就是我到目前为止所做的
System.out.println("Enter any key to get data or leave empty");
//Just give some value
int value = 0;
try {
for (int i = 5; i > 0; i--) {
System.out.println("Starting in " + i);
Thread.sleep(1000);
//If enter was pressed then theoretically value
//should be 1(Not working)
value = System.in.read();
}
if (value != 0) {
Database.getInstance().getAllStamps();
} else {
start();
}
} catch (Exception e) {
e.printStackTrace();
}
# 1 楼答案
或者
# 2 楼答案
获取原始输入的一种方法是使用^{}
用这样的方式: