描述:给定一个字符串,统计其中数字,字母,空格,其他符号出现的个数
输入描述:
第一行:一串字符串。
输出描述:
数字,字母,空格,其他符号出现的次数
每次遇到这个题目,我的想法是输入一个字符串,然后遍历它,将对应字符所在类别加一,但是在今天的考试中,老师又给我们提供了另外两种方法实现,所以经过一番调试后他就来了。。。。
方法一:简单粗暴
package com.kfm.dao;
import java.util.Scanner;
public class CharTest {
public static void main(String[] args) {
//在主方法里调用一个方法来实现一个功能,可以让你的代码更加结构化,专业化
totalCharacterCount();
}
private static void totalCharacterCount() {
Scanner scanner=new Scanner(System.in);
System.out.println("请任意输入一段字符:");
String a=scanner.nextLine();
int digtal=0;//数字
int letter=0;//字母
int black=0;//空格
int other=0;//其他
//定义一个char数组,用来存储字符串中的每个字符
char[] ch=null;
//.toCharArray()方法就是将字符串分隔为一个一个的字符,然后存储到数组里
ch=a.toCharArray();
//遍历char数组
for(int i=0;i<ch.length;i++) {
//数字的取值范围是0-9
if(ch[i]>='0'&&ch[i]<='9') {
digtal++;
//字母的取值范围是a-z and A-Z
}else if((ch[i]>='a')&&(ch[i]<='z')||(ch[i]>='A')&&(ch[i]<='Z')) {
letter++;
//如果是空格可以直接判断他等不等于空格
}else if(ch[i]==' ') {
black++;
}else {
other++;
}
}
System.out.println("数字字符有:"+digtal);
System.out.println("字母字符有:"+letter);
System.out.println("空格字符有:"+black);
System.out.println("其他字符有:"+other);
}
}
因为我们在平时的学习中想要实现这个功能,用第一种方式就可以,但是如果在以后的面试或者笔试中,我们还是使用第一种方法就会显得很没有杀伤力,所以如果你想要在众多面试者中脱颖而出,那你就要不断的优化代码,尝试用多种方式去实现他,不断地探索他的最优解
方式二:使用数组实现
(思想:用数组实现就是定义两个数组,一个用来存储类别,就比如字符串,数组,空格,其他等,第二个用于存储他们相对应的值,然后遍历一个数组将另一个数组也输出)
package com.kfm.alithmetic;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MultiTable{
public static void main(String[] args) {
String input="dsajiwe-01203";
totalCharacterCount(input);
}
private static void totalCharacterCount(String input) {
//定义第一个数组
String[] sort= {"word","number","space","other"};
//定义第二个数组。他的长度和第一个数组长度保持一致
int[] count=new int[sort.length];
//遍历字符串
for (int i = 0; i < input.length(); i++) {
//检索下标为i所在的字符
char ch=input.charAt(i);
if(ch >= '0' && ch <= '9') {
count[1]++;
}else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z') ){
count[0]++;
}else if(ch==' ') {
count[2]++;
}else {
count[3]++;
}
}
//输出结果
for (int i = 0; i < count.length; i++) {
System.out.println(sort[i]+":"+count[i]);
}
}
}
方式三:使用Map接口实现
(思想:Map接口是键值对存在的,所以可以把他的类别当做key,数据类型为String,把他的统计数据当做value,数据类型为Integer,判断字符存在的类别,然后给他的value加值)
package com.kfm.alithmetic;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MultiTable{
public static void main(String[] args) {
String input="dsajiwe-01203";
Map<String,Integer> results=totalCharacterCount(input);
System.out.println(results);
}
private static Map<String, Integer> totalCharacterCount(String input) {
//初始化一个Map集合
Map<String , Integer> map = new HashMap<String, Integer>();
map.put("countNumber", 0);
map.put("countTitle", 0);
map.put("countSpace", 0);
map.put("countOption", 0);
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if(ch>='0' && ch <= '9') {
//这个地方想用其他方式实现一直有错,如果你感心趣的话可以自己试试
//得到字符所属类别,
int rs = map.get("countNumber") ;
//在Map集合中value+1
map.put("countNumber", rs+1);
}
else if((ch >= 'a'&&ch <= 'z')||(ch >= 'A'&&ch <= 'Z')) {
int rs1=map.get("countTitle");
map.put("countTitle", rs1+1);
}else if(ch == ' ') {
int rs2=map.get("countSpace");
map.put("countSpace", rs2+1);
}else {
int rs3=map.get("countOption");
map.put("countOption", rs3+1);
}
}
return map;
}
}
今天的分享就到这儿了,建议大家针对于同一个题目尽量用多种方式去实现,这样不仅可以提高编程技术,还可以将之前的理论知识得以实践,如果觉得实现起来有点难,那就多多做题,切记刚拿到题目一定要思考,就算写不出来也要尝试去做,然后再去搜方法,看看别人是怎么实现的,这样可能对你记忆更加深刻,时间久了,你就会自己实现了哦,加油!
今天是持续写作的第 6 / 200 天。可以点赞、评论、收藏啦。