解题思路:
参考代码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++代码)——(22行代码)真的只需要两个变量就够了浏览:1867 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:602 |
C二级辅导-分段函数 (C语言代码)浏览:583 |
母牛的故事 (C语言代码)浏览:1408 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1059 |
C语言训练-求函数值 (C语言代码)浏览:599 |
IP判断 (C语言代码)浏览:819 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:597 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)from DQM浏览:773 |
幸运数 (C++代码)浏览:1309 |