标签:
(1)实现代码
1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Scanner; 4 5 public class demo { 6 7 public static void main(String[] args) { 8 9 Scanner scanner = new Scanner(System.in); 10 System.out.println("请输入一段英文:"); 11 String s = scanner.nextLine(); 12 SPLIT(s); 13 } 14 15 public static void SPLIT(String s){ 16 String[] arr= s.split(" "); 17 HashMap<String, Integer> map = new HashMap<String, Integer>(); 18 for(int i=0;i<arr.length;i++){ 19 if (!map.containsKey(arr[i])) { 20 map.put(arr[i], 1); 21 } else { 22 map.put(arr[i],map.get(arr[i])+1 ); 23 } 24 } 25 26 Iterator iterator = map.keySet().iterator(); 27 while(iterator.hasNext()){ 28 String string = (String) iterator.next(); 29 System.out.println("单词:"+string+",共出现了"+map.get(string)+"次。"); 30 } 31 } 32 }
测试
1 @Test 2 public void TestSPLIT(){ 3 String s = "abc cn cn cn abc cn"; 4 demo.SPLIT(s); 5 }
覆盖率
实现代码
1 import java.util.Scanner; 2 3 4 public class demo2 { 5 6 public static void main(String[] args) { 7 8 Scanner scanner = new Scanner(System.in); 9 System.out.println("请输入一段英文:"); 10 String s = scanner.nextLine(); 11 System.out.println(reverse(s)); 12 } 13 14 private static String reverse(String s) { 15 String[] arr= s.split(" "); 16 String string=""; 17 int i = arr.length-1; 18 for( ;i>=0;i--){ 19 if(i==0){ 20 string += arr[i]; 21 break; 22 } 23 string += arr[i]+" "; 24 } 25 return string; 26 } 27 28 }
测试
1 @Test 2 public void testreverse() { 3 String s1 = "how are you"; 4 String s2 = demo2.reverse(s1); 5 Assert.assertEquals(s2, "you are how"); 6 }
覆盖率
标签:
原文地址:http://www.cnblogs.com/mjrj-ng/p/5322708.html