日期:2014-05-20  浏览次数:20757 次

为什么我的小程序每次读的值都是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