当前位置: 代码迷 >> J2SE >> 为什么小弟我的小程序每次读的值都是2,小弟我明明输入1
  详细解决方案

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

热度:86   发布时间:2016-04-24 02:16:48.0
为什么我的小程序每次读的值都是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
  相关解决方案