我必须打印一个字符串中的字数统计N次?字符串字计数器
例如:
一二三四三两个五
输出:3
由于一个,四和五只出现一次。
我的代码显示我的输出为1
什么是错我的代码和逻辑?谢谢。
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String s=scan.nextLine(),b;
int n=scan.nextInt();
int c=0,f=0,i;
String[] a=s.split(" ");
for(i=0;i<a.length;i++)
{
b=a[i];
for(int j=i+1;j<a.length;j++)
{
if(b.equalsIgnoreCase(a[j]))
{
c++;
}
}
if(n==c)
{
f++;
}
else
{
c=0;
}
}
System.out.print(f);
}
}
我建议通过它符合一个调试器步进一行。 – notyou
你永远不会重置'i'变量。 –