您的位置:首页 > 其它

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

2017-12-02 15:35 447 查看
public class MainActivity extends AppCompatActivity {
private EditText tv;
private Button bt;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (EditText) findViewById(R.id.editText);
bt = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.info);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int digital = 0;
int character = 0;
int blank = 0;
int other = 0;
char[] ch = null;
String s = tv.getText().toString();
ch =s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(ch[i]>='0'&&ch[i]<='9'){
digital ++;
}else if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')){
character ++;
}else if(ch[i]==' '){
blank ++;
}else{
other ++;
}
}
text.setText("数字个数是:"+digital+"英文字母个数是:"+character+"空格个数是:"+blank+"其它字符个数是:"+other);
}

});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐