1 public class main { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 text a = new text(); 9 a.run(); 10 text2 b = new text2(); 11 b.run(); 12 } 13 14 }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class text { /** * @param args */ public void run(){ // TODO Auto-generated method stub //String a = "Hello World My First Unit Test"; Scanner stdin = new Scanner(System.in); System.out.println("请输入:"); String a = stdin.nextLine(); String[] b = a.split(" "); Map map = new HashMap<String,Integer>(); for(int c = 0;c<b.length;c++) { if(map.containsKey(b[c])) { int d = (Integer)map.get(b[c]); map.put(b[c],d+1); } else { map.put(b[c],1); } } for(Object obj : map.keySet()){ Object value = map.get(obj ); } for(Object obj : map.keySet()) { System.out.println(obj.toString()+"出现了"+map.get(obj)+"次"); } } }
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Scanner; 4 5 6 public class text2 { 7 8 /** 9 * @param args 10 */ 11 public void run(){ 12 // TODO Auto-generated mrethod stub 13 //String a = "how are you"; 14 Scanner stdin = new Scanner(System.in); 15 System.out.println("请输入:"); 16 String a = stdin.nextLine(); 17 18 String[] b = a.split(" "); 19 StringBuffer c = new StringBuffer(); 20 for(int d=b.length-1;d>=0;d--) 21 { 22 c.append(b[d]+" "); 23 } 24 System.out.println(c); 25 26 } 27 28 }