//do-while 循环示例
class dw {
public static void main(String args[])
throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed the key of ENTER: ");
//读入一个字符
ch = (char) System.in.read();
} while(ch!='p'); }}
输出:D:\code>java dw
Press a key followed the key of ENTER: a
Press a key followed the key of ENTER: Press a key followed the key of ENTER: Pr
ess a key followed the key of ENTER: p
请问键入a后怎么会有怎么多"Press a key followed the key of Enter:"
我怎么感觉输出一个"Press a key followed the key of Enter:"
呢?请高手解惑!
class dw {
public static void main(String args[])
throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed the key of ENTER: ");
//读入一个字符
ch = (char) System.in.read();
} while(ch!='p'); }}
输出:D:\code>java dw
Press a key followed the key of ENTER: a
Press a key followed the key of ENTER: Press a key followed the key of ENTER: Pr
ess a key followed the key of ENTER: p
请问键入a后怎么会有怎么多"Press a key followed the key of Enter:"
我怎么感觉输出一个"Press a key followed the key of Enter:"
呢?请高手解惑!
解决方案 »
- 在JTable中如何设置两个表格中只能选中其中一个表格中的一行
- swing线程问题
- 关于用JTable做为数据录入界面的问题,请大家帮帮忙!
- 哪位大虾用过JavaMail,来帮小弟找找错了
- 如何将BigDecimal格式化为科学计数法的字符串(没有精度的损失)?
- Jlist有没有哪个方法可以判断List中的一条记录是否被选定? 而不是返回被选定记录的index!急,在线等!先谢过大家!
- j2se如何访问windows下代有权限的网络文件
- 怎样给IntelliJ IDEA加上翻编译功能
- 高分!!急寻几本中间件方面电子书的下载地址
- jbuider 中dbSwing的FontChooser在哪里?
- System.in.read()从键盘上读入的字符放在那了?
- 点击添加按钮后,在当前页面出来一个div层该怎么实现。
class dw {
public static void main(String args[]) throws java.io.IOException {
char ch;
byte[] buffer = new byte[1024];//定义一个字节数组,用来存储输入的字符后的换行和回车等字符
do {
System.out.print("Press a key followed the key of ENTER: ");
//读入一个字符
ch = (char) System.in.read();
System.in.read(buffer);//读取字符后,读取换行和回车等字符
} while(ch!='p'); }
}
你的System.in.read()忘记考虑 回车符了~
回车在 控制台中是两个字符\r\n
所以你应该在
ch = (char)System.in.read();
后面再加上过滤回车的 代码ch = (char)System.in.read();
System.in.read();
System.in.read();
太有才了,感谢