首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

为什么小弟我的小程序每次读的值都是2,小弟我明明输入1

2012-01-20 
为什么我的小程序每次读的值都是2,我明明输入1Java codeimport java.io.*public class wren4255{public s

为什么我的小程序每次读的值都是2,我明明输入1

Java code
import java.io.*;public class wren4255{    public static void main(String args[]) throws IOException{        BufferedInputStream in = new BufferedInputStream(System.in);        BufferedOutputStream out = new BufferedOutputStream(System.out);        int y[] = {2,4,6,7,9,0};        int a, left,right,mid;        left = 0;        right = y.length;                byte b[] = new byte[2];        System.out.println("Input plz:");        a = in.read(b);    //就是这里,a不管我输入什么,都是2,为什么阿??        int l = 0;        System.out.println("a="+a);        while(left <= right){            mid = (left + right ) / 2;            if(y[mid] == a )    {    l=1;                    System.out.println("break----y[mid]="+y[mid]+"----a="+a);                break;    }            else if(y[mid]<a) {    left = mid+1;                System.out.println("y[mid]<a----"+"y["+mid+"]<"+a+"----y[mid]="+y[mid]);}            else {    right = mid -1;                    System.out.println("y[mid]>a----"+"y["+mid+"]>"+a+"----y[mid]="+y[mid]);}        }        if ( l==0){            out.write(b,0,1);            System.out.println("i cant find it,sorry!!");        }        else{            System.out.println("i find it!!   omg!!");                    }            out.flush();    }}


[解决办法]
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

请仔细阅读API

热点排行