public class MyCat { public static void main(String[] args) { byte[] read = new byte[5]; int j = 0; try{ j = System.in.read(read); }catch(java.io.IOException e){
niuji() 程序跑过的,没问题~只是打出来的是ASII码!且还包括回车和换行的ASII码(输入不夠3个时)还有后面你说的,我看不懂~手册有说: public int read(byte[] b) The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected. Returns: the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.效果来看,RamboAndGates(盖茨VS史太龙) 的是最好的了,不过还有一问题想问:"InputStreamReader(System.in)",System.in可以改成别的吗?
public static void main(String[] args)
{
byte[] read = new byte[5];
int j = 0;
try{
j = System.in.read(read);
}catch(java.io.IOException e){
}
if (j <0){
j = read.length;
}
for(int i=0;i<j;i++){
System.out.print( read[i] + " " );
}
}
} 前辈们,这样对否?!(翻docs\api\做出来的)
题是出自java_sl275(中)第一章练习
public static void main(String[] args)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println(br.ReadLine());
}catch(java.io.IOException e){
}
}
}
这个本来不应该放在第一单元第一节的,可恶的SL275中文教程~~
你的程序有没有跑过?好像不大对吧。
byte数组可能不够大,输入字数比byte数组大时System.in.read(read)会抛错。用System.in.read()读入String或StringBuffer中比较好。
windows中:回车"\n",换行"\r"
程序跑过的,没问题~只是打出来的是ASII码!且还包括回车和换行的ASII码(输入不夠3个时)还有后面你说的,我看不懂~手册有说:
public int read(byte[] b)
The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected. Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.效果来看,RamboAndGates(盖茨VS史太龙) 的是最好的了,不过还有一问题想问:"InputStreamReader(System.in)",System.in可以改成别的吗?
2.包括回车和换行,可能是你的程序没作判断
3.我说的“byte数组可能不够大,输入字数比byte数组大时System.in.read(read)会抛错”是错了。不是抛错,是出错。多出的是读不到的
4.RamboAndGates(盖茨VS史太龙)的方法的确是标准的方法。但BufferedReader和InputStreamReader是属于java.io包中的