Java - 错误消息显示不正确
Java - Error-message displaying incorrectly
这不是主要问题,但我不明白为什么会这样,所以我想我应该 post 在这里。这是我的代码:
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);
除一件事外,代码运行良好。错误消息 确保输入数字。 有时显示在菜单之后,有时显示在菜单之前,有时显示在菜单中间。 这是程序的输出:
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
如果重要的话,我正在使用 Eclipse。
我知道这不是什么大问题,但我很好奇为什么会这样。
System.err
和 System.out
不同 PrintStream
并且都是异步的。无法预测哪一个会在另一个之后显示(如果您快速调用它们)。
如果您真的希望消息按顺序显示,您应该使用相同的 PrintStream
(System.err
、System.out
或您自己的 PrintStream
)。
这是因为 System.out
和 System.err
是两个不同的流,它们可能会缓冲写入操作并在不同时间刷新(即打印出来)。
事情可能会在 System.out
和 System.err
之间混淆,因为我假设 System.out
出于性能原因使用缓冲输出流来实现,而 System.err
将使用非缓冲流来实现以更快地打印错误消息。
当您在流上显式调用 flush()
时,您可能会观察到更接近预期的行为,尽管我不确定是否不会留下竞争条件,因为两者都会结束在同一个 Eclipse 控制台中。
也就是说,我还假设 Eclipse 控制台有混合字符串的方法(即,即使您的代码保证顺序,控制台也可能给予 System.err
更高的优先级),所以恕我直言,获得保证订单的唯一可靠方法是对两条消息使用相同的输出流。
这不是主要问题,但我不明白为什么会这样,所以我想我应该 post 在这里。这是我的代码:
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);
除一件事外,代码运行良好。错误消息 确保输入数字。 有时显示在菜单之后,有时显示在菜单之前,有时显示在菜单中间。 这是程序的输出:
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
如果重要的话,我正在使用 Eclipse。 我知道这不是什么大问题,但我很好奇为什么会这样。
System.err
和 System.out
不同 PrintStream
并且都是异步的。无法预测哪一个会在另一个之后显示(如果您快速调用它们)。
如果您真的希望消息按顺序显示,您应该使用相同的 PrintStream
(System.err
、System.out
或您自己的 PrintStream
)。
这是因为 System.out
和 System.err
是两个不同的流,它们可能会缓冲写入操作并在不同时间刷新(即打印出来)。
事情可能会在 System.out
和 System.err
之间混淆,因为我假设 System.out
出于性能原因使用缓冲输出流来实现,而 System.err
将使用非缓冲流来实现以更快地打印错误消息。
当您在流上显式调用 flush()
时,您可能会观察到更接近预期的行为,尽管我不确定是否不会留下竞争条件,因为两者都会结束在同一个 Eclipse 控制台中。
也就是说,我还假设 Eclipse 控制台有混合字符串的方法(即,即使您的代码保证顺序,控制台也可能给予 System.err
更高的优先级),所以恕我直言,获得保证订单的唯一可靠方法是对两条消息使用相同的输出流。