编写程序ListWords.java实现从键盘输入一个英文句子,统计该句子中英文单词的个数,
将找出所有单词存放到一个数组中。例如:He said,"Ths's not a good idea."则输出
为
共有8个单词:He said ths s not a good idea
这是本人自己写的可是不对,求帮看看
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ListWords {
public static void main(String[] args) throws IOException {
readkeyboard();
}
public static void readkeyboard() throws IOException {
InputStreamReader str=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(str);
//优化代码
int score = 0;
String st=null;
String[] temp=new String[10];
while(!(st=in.readLine()).equals("quit")){
temp = st.split(" ");
score++;
}
System.out.println("你输入的单词书为"+score+";"+temp);
}
}
有这么些个思路给你,毕竟有些东西需要你自己摸索,才能更快的进步。
1:如果是从命令行,一个单词一行的话,不妨用缓冲流的readline方法读取
2:使用正则表达式进行匹配,这样更快
3:使用stringbuilder以及split方法,特殊方式特殊分割,这里也可以配合正则表达式使用哦
总之,解决问题的方法有很多,关键在于灵活的思考嘛。希望能帮到你