java编程:从键盘输入一行字符串,统计其中‘0’到‘9’这10数字字符出现的次数。

这是Java实验编程的一道题,我知道对于学软件的来说很简单但是对于毫不相关的专业……你们懂得……求大家帮助……急……在线等,感激不尽

第1个回答  2014-11-15
public class arrayfile {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < 10; i++) {
map.put(i, 0);
}
String s = null;
while (true) {
System.out.println("请输入:");
s = sc.nextLine();
if (Pattern.matches("^[0-9]*$", s)) {
break;

}
}
char[] a =s.toCharArray();
for (char cc:a) {
Integer in = Integer.parseInt(cc+"");
map.put(in, map.get(in)+1);
System.out.println(in);
}
System.out.println(map);
}
}追问

请问可以只用循环和数组字符串章节的知识咩……太复杂看不懂……我们只学了java的前五章……会露馅的……(抱大腿)

追答

public class arrayfile {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
// Map map = new HashMap();
int [] m = new int[10];
// for (int i = 0; i < 10; i++) {
// map.put(i, 0);
// }
String s = null;
while (true) {
System.out.println("请输入:");
s = sc.nextLine();
if (Pattern.matches("^[0-9]*$", s)) {
break;
}
}
char[] a =s.toCharArray();
for (char cc:a) {
Integer in = Integer.parseInt(cc+"");
m[in]=m[in]+1;
System.out.println(in);
}
for(int i=0;i<10;i++){
System.out.println(i+"出现了"+m[i]+"次");
}
}
}

追问

刚试了一下 运行不ok哦

追答

什么啊

本回答被提问者和网友采纳
相似回答