JAVA从键盘读取字符,进行统计

来源:互联网 发布:0信誉淘宝店铺转让 编辑:程序博客网 时间:2024/05/18 01:57
package test;import java.io.*;public class Input1 {public static void sumChar(byte b[]){int n=0;for(int i=0;i<b.length;i++){if(b[i]>='a'&&b[i]<='z') n++;}System.out.println("char count=" +n);}public static void main(String args[])throws IOException {System.out.println("Input:");byte buffer[]=new byte[512];int count=System.in.read(buffer);System.out.println("Output:");for(int i=0;i<count;i++){System.out.print(" "+buffer[i]);}System.out.println();for(int i=0;i<count;i++){System.out.print((char)buffer[i]);}System.out.println("count="+count);//buffer实际长度sumChar(buffer);}}

程序运行结果如下:

Input:
sdedv
Output:
 115 100 101 100 118 13 10
sdedv
count=7
char count=5

输入了5个字母,统计出来缓存区实际长度为7,因为输入是以回车换行未结束标志的,当按下Enter建时,程序当做字符读进去,

13是回车ASCII码,10是换行ASCII码,正验证了这个结果。

0 0
原创粉丝点击