class TestCountString {
public static void main(String[] args) {
String a = "zhhuu";
System.out.println("你输入的是:"+a+";长度为:"+a.length());
Makeout(a);
}
static void Makeout(String s) {
Character[] d = new Character[s.length()];
int sum = 0;
int z = 0;
int count = 0;
Character[] c = new Character[s.length()];
for(int i=0;i<s.length();i++) {
c[i] = s.charAt(i);
}
for(int k=0;k<s.length();k++) {
System.out.print(c[k]);
}
System.out.println();
for(int u=0;u<s.length();u++) {
d[u] = c[u]; //循环把c[]的值赋予d[];
for(int y=0;y<s.length();y++) {
if(d[u]==c[y]) {
z = y;
System.out.println("在第"+(z+1)+"个位置");
count++;
sum++;
}
}
System.out.println(d[u]+"有"+sum+"个");
System.out.println();
sum = 0;
/*if(count==s.length()) {
System.exit(-1);
}*/
}
}
}
public static void main(String[] args) {
String a = "zhhuu";
System.out.println("你输入的是:"+a+";长度为:"+a.length());
Makeout(a);
}
static void Makeout(String s) {
Character[] d = new Character[s.length()];
int sum = 0;
int z = 0;
int count = 0;
Character[] c = new Character[s.length()];
for(int i=0;i<s.length();i++) {
c[i] = s.charAt(i);
}
for(int k=0;k<s.length();k++) {
System.out.print(c[k]);
}
System.out.println();
for(int u=0;u<s.length();u++) {
d[u] = c[u]; //循环把c[]的值赋予d[];
for(int y=0;y<s.length();y++) {
if(d[u]==c[y]) {
z = y;
System.out.println("在第"+(z+1)+"个位置");
count++;
sum++;
}
}
System.out.println(d[u]+"有"+sum+"个");
System.out.println();
sum = 0;
/*if(count==s.length()) {
System.exit(-1);
}*/
}
}
}
解决方案 »
- 下面程序中addcomponent()函数参数数表中第四个和第五参数怎理解?
- 请问哪位有JAVA GUI程序设计那本书的代码?
- 事务问题关于 JDBC 的会的赶紧进吧
- swing中,如何批量修改同一列的若干行的数据
- 编译成功,但运行时显示"Exception in thread "main" java.lang.NoClassDefFoundError: hello/class ",这时为什么.
- 再说“==”
- [求助] JDK1.4方法读文件出现乱码,请帮忙!
- 谁可以帮我马上搞定?
- 急,在linux里怎么样表示一个路径?
- 为何Hashset、TreeSet、LinkedHashSet内部元素的排序结果不一样?
- java list鼠标事件问题
- 关于io基本概念问题,请教
还有你的Character[] d 和 int z没有必要,循环用i j k 不要用x y
由于不知道你的最终目的是啥,只对一些写法进行了修改,输出结果和你现在的逻辑(改为equals)是一样的,. static void Makeout(String s) {
if(s==null) return ;
int sum = 0;
int count = 0;
char targetChar;
char[] charSequence = s.toCharArray();//把s转为char序列
System.out.println(Arrays.toString(charSequence));//打印字符数组快捷的方式 for (int i = 0; i < charSequence.length; i++) {
targetChar = charSequence[i];
for (int j = 0; j < charSequence.length; j++) {
if (targetChar == charSequence[j]) { System.out.println("在第" + (j + 1) + "的位置");
count++;
sum++;
}
}
System.out.println(targetChar + "有" + sum + "个");
System.out.println();
sum = 0; /*if(count==s.length()) {
System.exit(-1);
}*/
}
}
应该是输出:在第一个位置
有1个z
在第2个位置
在第3个位置
有2个h 在第4个位置
在第5个位置
有2个u -----------到此结束了可是现在输出了2次h和u的位置 变成 在第一个位置
有1个z在第2个位置
在第3个位置
有2个h 在第2个位置
在第3个位置
有2个h ------------这个h输出了2次 请帮我纠正让只输出一次意思就是找到h后下面的h就不要在找了
if(s==null) return ;
int sum = 0;
int count = 0;
char targetChar;
char[] charSequence = s.toCharArray();//把s转为char序列
boolean[] hasCompare = new boolean[charSequence.length];//用来记录某个位置上的字符是否已经比较过了
System.out.println(Arrays.toString(charSequence));//打印字符数组快捷的方式 for (int i = 0; i < charSequence.length; i++) {
if(!hasCompare[i]){
targetChar = charSequence[i];
for (int j = i; j < charSequence.length; j++) {
if (targetChar == charSequence[j]) {
hasCompare[j] = true;
System.out.println("在第" + (j + 1) + "的位置");
count++;
sum++;
}
}
System.out.println(targetChar + "有" + sum + "个");
System.out.println();
sum = 0;
}
/*if(count==s.length()) {
System.exit(-1);
}*/
}
}
学习!