用集合的思想解决此问题,我已经做了一个开头了,后面不知道怎么做,待续。
package cj.collection.work;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*任意输入数字,然后打印输出各数字出现的次数
* */
public class TestHomework {
public static void main(String[] args) {
//1.获取键盘录入
Scanner sc=new Scanner(System.in);
String line = sc.nextLine();
if(line.equals("over")){
System.exit(0);
}
//2.声明list集合,将数字存入集合ArrayList中;
List list=new ArrayList();
list.add(0, line);
Object n0 = list.get(0);
System.out.println(list+"<<<"+n0);
}
}
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Scanner; /** * 任意输入数字,然后打印输出各数字出现的次数 */ public class TestHomework { public static void main(String[] args) { //1.获取键盘录入 Scanner sc=new Scanner(System.in); String line = sc.nextLine(); if(line.equals("over")){ System.exit(0); } //2.声明list集合,将数字存入集合ArrayList中; List<String> list=new ArrayList(); list.add(0, line); String n0 = list.get(0); HashMap<Character, Integer> strhash = new HashMap<Character, Integer>(); int num = 0; int count = 0; for(int i=0;i<n0.length();i++){ char c = n0.charAt(i); int temp = 0; for (int j = 0; j <= n0.length() - 1; j++) { num = n0.indexOf(c, temp); if (num != -1) { count++; temp = num + 1; continue; } else { strhash.put(c, count); count = 0; break; } } } Iterator iter1 = strhash.keySet().iterator(); while (iter1.hasNext()) { Object key = iter1.next(); Object value = strhash.get(key); System.err.println(key + " 的出现次数为:" + value+" 次"); } } }
好厉害哦