(1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
(2)编写单元测试进行测试;
(3)用ElcEmma查看代码覆盖率,要求覆盖达到100%。
package cn.lin.test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class Test2 {
@Test
public void index() {
String strWords = "adb abc abc kk";
String[] words_Array = strWords.split(" ");
Map<String,Integer> words_Map=new HashMap<String, Integer>();
int arrLength = words_Array.length;
for(int i=0;i<arrLength;i++){
if(!words_Map.containsKey(words_Array[i])){
words_Map.put(words_Array[i], 1);
System.out.println(words_Array[i]);
System.out.println("出现");
System.out.println(words_Map.put(words_Array[i], 1));
System.out.println("次");
}else{
int currentNum = words_Map.get(words_Array[i])+1;
words_Map.remove(words_Array[i]);
words_Map.put(words_Array[i], currentNum);
System.out.println(words_Array[i]);
System.out.println("出现");
System.out.println(words_Map.put(words_Array[i], currentNum));
System.out.println("次");
}
}
}
}
1、 学习单元测试代码覆盖率工具的使用
(1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
(2)编写单元测试进行测试;
(3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
import java.util.Scanner;
public class Test {
public void reverse(String str) {
String[] wordArray = str.split(" ");
System.out.print("颠倒输出:");
for (int i = wordArray.length - 1; i >= 0; i--) {
System.out.print(wordArray[i] + " ");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一句英语句子:");
String str = input.nextLine();
Test test = new Test();
test.reverse(str);
}
}