用java编出一个程序统计每个数字在字符串中出现的次数

题目要求用方法头publicstaticintcount(Stringstr,chara);一下是我编的程序,但是编译不了,麻烦各位帮个忙看一下应该怎么改??谢谢了!!p... 题目要求用方法头public static int count(String str,char a);一下是我编的程序,但是编译不了,麻烦各位帮个忙看一下应该怎么改??谢谢了!!
public class X85 {
public static void main(String []args) {
String SSN="343324545";
String ID="434344323";
int[]counts1=count(SSN);
int[]counts2=counts(ID);

for(int i=0;i<counts1.length;i++) {
if(counts1[i]!=0)
Syestem.out.print(int i+"appears"+counts1[i]+((counts1[i]==1)?"time\n":"times\n"));
}

for(int i=0;i<counts2.length;i++){
if(counts2[i]!=0)
Syestem.out.print(int i+"appears"+counts2[i]+((counts2[i]==1)?"time\n":"times\n"));
}

JOptionPane.showMessageDialog(null,output);
JOptionPane.showMessageDialog(null,out);
}

public static int[]count(String s) {

int[]counts1=new int[9];

for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i)))
counts1[s.charAt(i)]++;
}
return counts1;
}

public static int[]counts(String s) {

int[]counts2=new int[9];

for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i)))
counts2[s.charAt(i)]++;
}
return counts2;
}

}
展开
 我来答
卫星3D看世界
2009-08-15 · TA获得超过909个赞
知道小有建树答主
回答量:461
采纳率:66%
帮助的人:169万
展开全部
把你的代码稍稍改动了一下,仔细查看一下注释的地方,
测试结果:
SSN=343324545 's number properties as follows:
2 appears 1 time
3 appears 3 times
4 appears 3 times
5 appears 2 times

ID=434344323 's number properties as follows:
2 appears 1 time
3 appears 4 times
4 appears 4 times

public class Test {
public static void main(String[] args) {
String SSN = "343324545";
String ID = "434344323";
int[] counts1 = count(SSN);
int[] counts2 = counts(ID);
System.out.println("SSN="+ SSN + " 's number properties as follows:");
for (int i = 0; i < counts1.length; i++) {
if (counts1[i] != 0) {
System.out.println(i + " appears " + counts1[i] +
((counts1[i] == 1) ? " time" : " times"));
}
}
System.out.println("\nID="+ ID + " 's number properties as follows:");
for (int i = 0; i < counts2.length; i++) {
if (counts2[i] != 0) {
System.out.println(i + " appears " + counts2[i] +
((counts2[i] == 1) ? " time" : " times"));
}
}
}

public static int[] count(String s) {
int[] counts1 = new int[9];
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
counts1[s.charAt(i)-'0']++;//这里要减去0的ASCII吗
}
}
return counts1;
}

public static int[] counts(String s) {
int[] counts2 = new int[9];
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
counts2[s.charAt(i) - '0']++;//这里要减去0的ASCII吗
}
}
return counts2;
}
}
daodiba3
2009-08-15 · TA获得超过588个赞
知道小有建树答主
回答量:1236
采纳率:0%
帮助的人:467万
展开全部
public class TestMeticNum {
public static int count(String str,char a) {

int num = 0;
int sun = 0;
int start = 0;
int[] ii = new int[10];

while((start = str.indexOf(a, sun)) != -1) {
num++;
sun = start + 1;
}
return num;
}

public static void main(String[] args) {
String s = "154879632458";
int[] w = new int[10];

for(int i = 0; i < s.length(); i++) {
char ss = s.charAt(i);
int countSun = Integer.parseInt(String.valueOf(ss));
w[countSun] = count(s, ss);
}

for(int i = 0; i < 10; i++) {
System.out.println(i + "出现的次数为" + w[i] + "次");
}
}
}

这是我写的土方法`
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式