WordCounter的设计与实现
来源:互联网 发布:安卓街机网络对战平台 编辑:程序博客网 时间:2024/06/05 07:48
本代码段实现了统计字母,空格,数字,其他字符,以及单词数等操作。目前仅能够通过键盘输入,预留了从文件导入的接口,后续会实现。
代码写的很LOW,有些BUG到现在还没有解决。
来几张图:
附上代码:
package wordCounter;import java.util.Scanner;public class WordCounter { static int welcome() { Scanner scanner = new Scanner(System.in); System.out.println("HI!You can input a string and you can import from your file!And we will give you some message that you want konw!"); System.out.println("1、input a string \n2、implort from file"); int choice=scanner.nextInt(); return choice; } private static String getUserString(int choice) { String s = null ; if (choice==1) { //input string System.out.println("Please input your string!"); Scanner scanner=new Scanner(System.in); s = scanner.nextLine(); }else if (choice==2) { //get string from file }else { welcome(); } return s; } /** * get number\character\black\other * @param s */ private static void getMessageOne (String s) { char[] myChar = s.toCharArray(); for (int i = 0; i < myChar.length; i++) { char temp = myChar[i]; if (temp >= '0' && temp <= '9') { num[0]++; } else if (temp >= 'a' && temp <= 'z' || temp >= 'A' && temp <= 'Z') { num[1]++; } else if (temp == ' ') { num[2]++; } else { num[3]++; } } } private static void showMessage() { System.out.println("**************************"); System.out.println("Your counter at here:"); System.out.println("number:"+num[0]); System.out.println("character:"+num[1]); System.out.println("blank:"+num[2]); System.out.println("other:"+num[3]); System.out.println("words:"+num[4]); System.out.println("**************************"); System.out.println("Thank you for your using!"); } static int num[] = { 0, 0, 0, 0 ,0};// 数字、字母、空格、其他、单词 public static void main(String[] args) throws InterruptedException { String s=getUserString(welcome()); System.out.println("Now,we doing some work!Please wait!"); //System sleep! //Thread.currentThread().sleep(5000); getMessageOne(s); getMessageTwo(s); showMessage(); } private static void getMessageTwo(String s) { // TODO Auto-generated method stub System.out.println("There are the word!"); String s1[] = s.split(" ");// for(int i=0;i<s1.length;i++) { // System.out.println(s1[i]); num[4]++;//计数变量 } }}
最后附上GitHub地址:
点我点我
阅读全文
0 0
- WordCounter的设计与实现
- Storm的wordCounter计数器详解
- Storm的wordCounter计数器详解
- 状态栏的设计与实现
- 搜索引擎的设计与实现
- Spinlock 的设计与实现
- 单链表的设计与实现
- 图的设计与实现
- 组件的设计与实现
- epoll的设计与实现
- 缓存的设计与实现
- API的设计与实现
- API的设计与实现
- n的设计与实现
- HashMap的设计与实现
- cache 的设计与实现
- 哈希表的设计与实现
- 游览机器人的设计与的实现
- Ldap使用bind函数产生的问题
- String字符串处理
- Faster R-CNN WINDOWS CPU环境搭建(详细版)
- 今天遇到的一个BUG :app:compileDebugJavaWithJavac'. Could not add entry '0' to cache localClassSetAnalysis.b
- axure原型设计之tab选项卡
- WordCounter的设计与实现
- mir2ei源码韩文乱码处理
- MFC Tab control使用
- UIButton文字的显示位置
- Week Three
- 学习笔记40-哈希码(HashCode)
- MySQL 主从一致性检查
- windows下JMX连接Tomcat
- 如何使用libvlc实现最简单的播放器