文章目录
利用Java识别并输出各部分的个数
代码如下:
String myString = null;
int stringLength = 0;
int letterNum = 0;
int decimalNum = 0;
int otherNum = 0;
char currentChar;
Scanner scan = new Scanner(System.in);
System.out.println("请输入字符串:");
myString = scan.nextLine();
stringLength = myString.length();
for(int i = 0;i < stringLength;i++){
currentChar = myString.charAt(i);
if(currentChar >= '0' && currentChar <= '9')
decimalNum ++;
else if ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z'))
letterNum ++;
else
otherNum ++;
}
System.out.println("输入的字符串:" + myString);
System.out.println("字母个数:" + letterNum);
System.out.println("数字个数:" + decimalNum);
System.out.println("其他类型字符个数:" + otherNum);
scan.close();
}
总结
例如:以上就是今天要讲的内容。