程序放在一个死循环中,read()不但读了你输入的字符,而且将最后的回车符也作为一个字符读入(回车符占两个字节)。所以每次你输入的数据最后总有10和13。
package help;
import java.io.*;
public class Test32 {
static BufferedReader buff;
public static void main(String[] args) {
while(true){
buff = new BufferedReader(new InputStreamReader(System.in));
try{
String s = (String)buff.readLine();
int i = s.length();
for(int j=0;j<i;j++){
double d = Double.parseDouble(s.substring(j,j+1));
System.out.println(d + "%" + 2 + "=" + d%2);
}
}catch(NumberFormatException ee){
System.out.println("Please input numeric character!");
}
catch(IOException e){
System.err.println(e);
}
}
}
}
//这样写,缺点在于当你输入非数字字符的时候,会抛出NumberFormatException异常,只能处理数字字符。