Java 频率分析,知道如何解决这个问题的人。我能';我不知道这封信能寄多少次
编写一个程序,可以从文件中读取文本并进行频率分析 在那些看起来 存储信息(频率)您应该使用如下数组。 -创建一个空间为127 int的数组 -一次读一个字母,将其转换为int(char->int)。价值来了 与字母的ascii码相对应。更新上的“计数器” 数组中的相应索引。 例子!你计算字母A频率的地方是 对应于“A”ie 65的ASCII码Java 频率分析,知道如何解决这个问题的人。我能';我不知道这封信能寄多少次,java,arrays,Java,Arrays,编写一个程序,可以从文件中读取文本并进行频率分析 在那些看起来 存储信息(频率)您应该使用如下数组。 -创建一个空间为127 int的数组 -一次读一个字母,将其转换为int(char->int)。价值来了 与字母的ascii码相对应。更新上的“计数器” 数组中的相应索引。 例子!你计算字母A频率的地方是 对应于“A”ie 65的ASCII码 int[]数组=新的int[127]; 扫描仪扫描=空; int c=0; 试一试{ 扫描=新扫描仪(新文件(“C:\\Users\\----\\ecli
int[]数组=新的int[127];
扫描仪扫描=空;
int c=0;
试一试{
扫描=新扫描仪(新文件(“C:\\Users\\----\\eclipse-
工作区\\algo_data\\src\\klasslista.txt”);
while(scan.hasNext()){
String str=scan.next();
char[]myChar=str.toCharArray();
对于(int j=0;j
它不计算字母数。ASCII码是您要查找的数组索引。每次遇到字母时,将该索引处的int增加一。表示该ASCII码的索引处的int是字符出现次数的计数器。数组中每个索引的默认值应为-1,因此如果然后你需要将它设置为1,否则将值增加1
int[] array = new int[127];
Scanner scan = null;
try {
scan = new Scanner(new File("C:\\Users\\-------\\eclipse-workspace\\algo_data\\src\\klasslista.txt"));
while (scan.hasNext()) {
String str = scan.next();
char[] myChar = str.toCharArray();
for (int j = 0; j < myChar.length; j++) {
int temp = array[(int) myChar[j]];
if (temp == -1) {
array[(int) myChar[j]] = 1;
} else {
array[(int) myChar[j]] = temp + 1;
}
}
}
for (int i = 0; i < 127; i++) {
char c = (char) i;
System.out.println("Count of " + c ": " + array[i]);
}
} catch (FileNotFoundException fnfe) {
}
int[]数组=新的int[127];
扫描仪扫描=空;
试一试{
scan=newscanner(新文件(“C:\\Users\\---\\eclipse workspace\\algo\u data\\src\\klasslista.txt”);
while(scan.hasNext()){
String str=scan.next();
char[]myChar=str.toCharArray();
对于(int j=0;j
不要只是将分配复制并粘贴到堆栈溢出中。我们不会为您这样做。您显然已经有了一个开始,所以请解释您尝试了什么,得到了什么结果,以及这与您期望的结果有何不同。“当我时,我得到时,当我期望得到时”你那里的代码有什么问题吗?你在坚持什么?我不明白你在问什么。要(int j=0;j int[] array = new int[127];
Scanner scan = null;
try {
scan = new Scanner(new File("C:\\Users\\-------\\eclipse-workspace\\algo_data\\src\\klasslista.txt"));
while (scan.hasNext()) {
String str = scan.next();
char[] myChar = str.toCharArray();
for (int j = 0; j < myChar.length; j++) {
int temp = array[(int) myChar[j]];
if (temp == -1) {
array[(int) myChar[j]] = 1;
} else {
array[(int) myChar[j]] = temp + 1;
}
}
}
for (int i = 0; i < 127; i++) {
char c = (char) i;
System.out.println("Count of " + c ": " + array[i]);
}
} catch (FileNotFoundException fnfe) {
}