java计算字符串中不同字符或数字个数的方法-创新互联
问题:

根据提示输入一行字符,这行字符可以是任意的,可以包含字母数字标点符号,特殊符号等,java程序会输出你输入字符串中各个类别字符的个数。
结果展示:
代码展示:
package com.one;
import java.util.*;
public class Flqgs {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入一行字符串:");
String num = input.nextLine();
int digital = 0, character = 0, other = 0, blank = 0;
char [] ch = num.toCharArray();
for(int i=0;i= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'z'){
character++;
}else if(ch[i] >= '0' && ch[i] <= '9'){
digital++;
}else if(ch[i] == ' '){
blank++;
}else{
other++;
}
}
System.out.println("字母个数:"+character);
System.out.println("数字个数:"+digital);
System.out.println("空格个数:"+blank);
System.out.println("其他个数:"+other);
}
} 以上就是java实现计算字符串中不同字符或数字的个数的详细内容,更多请关注创新互联其它相关文章!
文章名称:java计算字符串中不同字符或数字个数的方法-创新互联
当前URL:http://www.jxjierui.cn/article/dcjdic.html


咨询
建站咨询
