java统计hello中所有26个字母出现的次数 下面我的代码哪里出现了问题?如何改进?

王新港 2016-03-21 04:53:00
package 统计英文字母;

import java.util.*;

public class 统计英文字母 {
public static void main(String[] args) {
String a="hello";
char[] c=a.toCharArray();//变成字符数组
String b="abcdefghijklmnopqrstuvwxyz";
char[] d=b.toCharArray();//变成字符数组
int e[]=new int[26];
for(int i=0;i<c.length-1;i++){
for(int j=0;j<d.length-1;i++)
if(c[i]==d[j])// 比较字符是否相等
e[j]+=e[j];// 统计每个字母出现次数
}
for(int i=0;i<c.length-1;i++){
for(int j=0;j<d.length-1;i++)
System.out.print(d[j]+" "+e[j]);
}
}
}
...全文
342 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
为什么要声明一个数组来记录次数呢 你每次记录的时候 j不一定一样 那么 你记录在不同的数组元素中 你应该声明一个整形变量 每次记录让它++不就好了?
绿旅嗒 2016-03-21
  • 打赏
  • 举报
回复
String a = "hello"; char[] c = a.toCharArray();// 变成字符数组 String b = "abcdefghijklmnopqrstuvwxyz"; char[] d = b.toCharArray();// 变成字符数组 int e[] = new int[26]; for (int i = 0; i < c.length - 1; i++) { for (int j = 0; j < d.length - 1; j++) { if (c[i] == d[j]) {// 比较字符是否相等 e[j] += 1;// 统计每个字母出现次数 } } } for (int j = 0; j < d.length - 1; j++) { System.out.println(d[j] + " " + e[j]); }
supaipai 2016-03-21
  • 打赏
  • 举报
回复
引用 2 楼 ScanQ 的回复:
[quote=引用 1 楼 supaipai 的回复:] for(int i=0;i<c.length-1;i++){ for(int j=0;j<d.length-1;i++) System.out.print(d[j]+" "+e[j]); } 第二行是j++吧
改完之后统计数字都是0[/quote] 上面那个循环第二行的i也应该是j
王新港 2016-03-21
  • 打赏
  • 举报
回复
package 统计英文字母; import java.util.*; public class 统计英文字母 { public static void main(String[] args) { String a="hello"; char[] c=a.toCharArray(); String b="abcdefghijklmnopqrstuvwxyz"; char[] d=b.toCharArray(); int e[]=new int[26]; for(int i=0;i<c.length;i++){ for(int j=0;j<d.length;j++) if(c[i]==d[j]) e[j]=e[j]+1; } for(int j=0;j<d.length;j++) System.out.println(d[j]+" "+e[j]); } } 刚才的代码length后面多了-1 以及下面多了个for 现在可以了
王新港 2016-03-21
  • 打赏
  • 举报
回复
引用 1 楼 supaipai 的回复:
for(int i=0;i<c.length-1;i++){ for(int j=0;j<d.length-1;i++) System.out.print(d[j]+" "+e[j]); } 第二行是j++吧
改完之后统计数字都是0
supaipai 2016-03-21
  • 打赏
  • 举报
回复
for(int i=0;i<c.length-1;i++){ for(int j=0;j<d.length-1;i++) System.out.print(d[j]+" "+e[j]); } 第二行是j++吧

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧