我如果直接回车,执行的结果是这样:请输入需要查询的字符>请重新输入字符请重新输入字符
s
s字符在字符数组中的位置是0疑问在于:怎么会多输出一次"请重新输入字符"?这个地方如何修改,才能合理化的?
while(exit){
try {
target =(char)System.in.read();
System.out.print(target);
if(target=='\r'||target=='\n'||target==' '){
System.out.println("请重新输入字符");
continue;
}
}catch(Exception ex){
System.out.println("输入的字符串非法!请重新输入");
continue;
}
exit=false;
}
s
s字符在字符数组中的位置是0疑问在于:怎么会多输出一次"请重新输入字符"?这个地方如何修改,才能合理化的?
while(exit){
try {
target =(char)System.in.read();
System.out.print(target);
if(target=='\r'||target=='\n'||target==' '){
System.out.println("请重新输入字符");
continue;
}
}catch(Exception ex){
System.out.println("输入的字符串非法!请重新输入");
continue;
}
exit=false;
}
建议你用Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
从键盘读取数据
这个能解释下吗?我不是很理解呀!
与操作系统相关的,在windows里是\r\n,在UNIX里是\r