解题思路:
参考代码1并未在主函数中输出
参考代码2使用map集合来进行输出
注意事项:
参考代码:
参考代码1;
import java.util.Scanner;
public class Main {
public static void cs(String a){
char[] array = a.toCharArray();
int Letter = 0;
int Num = 0;
int Space = 0;
int Character = 0;
for(int i=0;i<array.length;i++){
array[i] = a.charAt(i);
if(65<=array[i]&&array[i]<=90||97<=array[i]&&array[i]<=122)
Letter++;
else if(48<=array[i]&&array[i]<=57)
Num++;
else if(array[i]==32)
Space++;
else
Character++;
}
System.out.print(Letter+" "+Num+" "+Space+" "+Character);
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
cs(a);
}
}
参考代码2;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static Map<String, Integer> cs(String a){
char[] array = a.toCharArray();
Map<String, Integer> M = new HashMap<String, Integer>();
int Letter = 0;
int Num = 0;
int Space = 0;
int Character = 0;
for(int i=0;i<array.length;i++){
array[i] = a.charAt(i);
if(65<=array[i]&&array[i]<=90||97<=array[i]&&array[i]<=122)
Letter++;
else if(48<=array[i]&&array[i]<=57)
Num++;
else if(array[i]==32)
Space++;
else
Character++;
}
M.put("Letter", Letter);
M.put("Num", Num);
M.put("Space", Space);
M.put("Character", Character);
return M;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
System.out.print(cs(a));
}
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题12.5 (C语言代码)浏览:858 |
程序员的表白 (C语言代码)浏览:1556 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:725 |
兰顿蚂蚁 (C++代码)浏览:1143 |
剪刀石头布 (C语言代码)浏览:790 |
最小公倍数 (C语言代码)浏览:1078 |
sizeof的大作用 (C语言代码)浏览:1562 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:1091 |
简单的a+b (C语言代码)浏览:491 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:471 |