- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- char[] chars = new char[1024];
- //获取键盘输入的字符,存储到字符数组中
- int len = br.read(chars);
- //循环字符数组,考虑忽视最后的回车\r\n,所以-2
- for (int i = 0; i < len-2; i++) {
- //将字符转型成Ascall码对应的int
- int t = chars[i]+0;
- System.out.println("十进制数值:");
- System.out.println(t);
- StringBuffer result = new StringBuffer();
- //十进制转换成二进制
- while(t>0){
- result.insert(0, t%2);
- t = t/2;
- }
- System.out.println("二进制数值:");
- System.out.println(result.toString());
- }
-
- }
复制代码
异常情况不太清楚,等大侠解决了。。。 |