题目
输入: 本程序代码
输出: 程序中有多少个字母, 多少个数字, 多少个符号
样例输入:
print("I don't know!");
end
样例输出:
字母: 14
数字: 9
符号: 12
一共:35
(end表示结束, 所以不计入)
代码
public class Homework0611 {
public static void main(String[] args) {
String str = print();
int[] count = method(str);
System.out.println("\n******结果******");
System.out.println("字母: " + count[0]);
System.out.println("数字: " + count[1]);
System.out.println("符号: " + count[2]);
}
public static String print() {
Scanner sc = null;
String str = null;
String temp = null;
System.out.println("******请输入本程序代码******");
while(true){
sc = new Scanner(System.in);
temp = sc.nextLine();
str += temp;
if ("end".equals(temp)){
str = str.substring( 4, str.length() - 3 );
return str;
}
}
}
public static int[] method(String str) {
int countL = 0;
int countN = 0;
int countS = 0;
char c = 0;
for (int i = 0; i < str.length(); i++){
c = str.charAt(i);
if (48 <= c && c <= 57){
countN++;
}else if(65 <= c && c <= 90 || 97 <= c && c <= 122){
countL++;
}else{
countS++;
}
}
int[] count = {countL, countN, countS};
return count;
}
}
运行结果
******请输入本程序代码******
print("I don't know!");
end
******结果******
字母: 14
数字: 9
符号: 12