1. 编写一个程序,输出一个字符串中的大写英文字母个数,小写英文字母个数以及非英文字母个数.
第一种方法:
public class Test { public static void main(String[] args) { String s = "[email protected]&&&haAA"; int Lcount = 0, Ucount = 0, Ocount = 0; for(int i=0;i<s.length();i++){ if(s.charAt(i) >= ‘a‘ && s.charAt(i) <= ‘z‘){ Lcount++; } else if(s.charAt(i) >= ‘A‘ && s.charAt(i) <= ‘Z‘){ Ucount++; } else { Ocount++; } } System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount); } } //Lcount: 22,Ucount: 10,Ocount: 17
第二种方法:
public class Test { public static void main(String[] args) { String s = "[email protected]&&&haAA"; int Lcount = 0, Ucount = 0, Ocount = 0; for(int i=0;i<s.length();i++){ if(Character.isLowerCase(s.charAt(i))){ Lcount++; } else if(Character.isUpperCase(s.charAt(i))){ Ucount++; } else { Ocount++; } } System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount); } }// Lcount: 22,Ucount: 10,Ocount: 17
第三种方法:
public class Test { public static void main(String[] args) { String BL = "abcdefghijklmnopqrstuvwxyz"; String BU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String s = "[email protected]&&&haAA"; int Lcount = 0, Ucount = 0, Ocount = 0; for(int i=0;i<s.length();i++){ if(BL.indexOf(s.charAt(i)) != -1){ Lcount++; } else if(BU.indexOf(s.charAt(i)) != -1){ Ucount++; } else { Ocount++; } } System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount); } }// Lcount: 22,Ucount: 10,Ocount: 17
时间: 2024-10-24 17:46:38