Yul*_*lek 5 java eclipse error-handling output
这不是一个主要问题,但我不明白为什么会发生这种情况,所以我想我会在这里发布.这是我的代码:
do{
printMenu();//method to print menu
try{
user=input.nextInt();
}
catch(InputMismatchException imme)
{
System.err.println("Make sure to enter a number.");
input.next();
continue;
}
switchMenu(user);//method with switch method for user input
}while(1<2);
Run Code Online (Sandbox Code Playgroud)
代码运行正常,除了一件事.错误消息确保输入一个数字.有时会在菜单后显示,有时在菜单中间,有时在菜单中间显示.这是该程序的输出:
1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit
a
1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit
Make sure to enter a number.//Error message after menu
asd//wrong input
Make sure to enter a number.//now error message displays before menu
1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
4. Print seat map
5. Check price
6. Print ticket
7. Exit
asd
1. Book a ticket
2. Cancel a ticket
3. Check how many seats left
Make sure to enter a number.//in the middle now???
4. Print seat map
5. Check price
6. Print ticket
7. Exit
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我正在使用Eclipse.我知道这不是一个大问题,但我很好奇为什么会这样.
这是因为System.out和System.err是两个不同的流,它们可能会缓冲写入操作并在不同时间刷新(即打印出来)。
System.out和 之间的事情可能会混淆System.err,因为出于性能原因,我假设System.out使用缓冲输出流来实现,并System.err使用非缓冲流来实现以更快地打印错误消息。
当您显式调用流时,您可能会观察到更接近您期望的行为flush(),尽管我不确定是否不会留下竞争条件,因为两者最终都会出现在同一个 Eclipse 控制台中。
话虽这么说,我还假设 Eclipse 控制台具有混合字符串的方法(即,即使您的代码保证订单,控制台也可能会给出System.err更高的优先级),所以恕我直言,获得保证订单的唯一可靠方法是对这两个消息使用相同的输出流。
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |