当前位置: 代码迷 >> J2SE >> 控制台读取数字,该怎么解决
  详细解决方案

控制台读取数字,该怎么解决

热度:118   发布时间:2016-04-24 12:11:36.0
控制台读取数字
Java code
public  void input(){                 int command;        while(true){            try {                command = System.in.read();                if(command==1){                    break;                }            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }


我输入的是1,可是读进来的却是49。该怎么样才能正确的读取我输入的数字?

------解决方案--------------------
For Example:

Java code
public  void input(){                 int command;        while(true){            try {                command = System.in.read();                if((char)command=='1'){//注意这行                    break;                }            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }
------解决方案--------------------
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
  相关解决方案