效果如图:
代码如下:
import java.util.Scanner;
public class Test_String1 {
public static void main(String[] args) {
System.out.println("请输入字符串:");
//从控制台输入字符串
Scanner sc=new Scanner(System.in);
String s=sc.next();
int upperCount=0,lowCount=0,otherCount=0;
for(int i=0;i<s.length();i++) {
char c=s.charAt(i);
if(c>='A' && c<='Z') {
upperCount++;
} else if(c>='a' && c<='z') {
lowCount++;
}else {
otherCount++;
}
}
System.out.print("大写字母有"+upperCount+"个,");
System.out.print("小写字母有"+lowCount+"个,");
System.out.print("非字母有"+otherCount+"个。");
}
}