用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;
}
} 展开
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;
}
} 展开
2个回答
展开全部
把你的代码稍稍改动了一下,仔细查看一下注释的地方,
测试结果:
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;
}
}
测试结果:
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;
}
}
展开全部
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] + "次");
}
}
}
这是我写的土方法`
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] + "次");
}
}
}
这是我写的土方法`
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询