统计字符串中每种字符类型的个数demo
/* * 统计字符串中每中字符类型的个数,思路: * 1、键盘录入一个字符串 * 2、获取到每个字符,遍历字符串 * for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); * 3、判断每个字符属于哪种类型,对应的个数++ */ package cn.string; import java.util.Scanner; public class TongJi { public static void main(String[] args) { int daxienumber = 0; int xiaoxienumber = 0; int intnumber = 0; Scanner sc = new Scanner(System.in); System.out.println("请属于一个字符串"); String s = sc.nextLine(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); System.out.println(ch); if (ch >= 'A' && ch <='Z') { daxienumber ++ ; } if (ch >= 'a' && ch <='z') { xiaoxienumber ++; } if (ch >= '0' && ch <='9') { intnumber ++; } } System.out.println("daxienumber:"+daxienumber); System.out.println("xiaoxienumber:"+xiaoxienumber); System.out.println("intnumber:"+intnumber); } }