所以我正在测试这段代码,当我读取文件时,打印时的输出停止,我得到max = freqs[i];
。
我想知道是否某个地方我无意中添加了一个限制,我得到了多少结果,否则我想不出可能出错的地方。
这是造成麻烦的一点( public int maxIndex(int[] freqs) {
int max = freqs[0];
for (int i = 1; i < freqs.length; i++) {
if (freqs[i] > max) {
max = freqs[i];
}
}
System.out.println("max number of words of certain lengths: "+max);
return freqs[max];
//return max;
}
):
import edu.duke.FileResource;
public class WordLengths {
public void countWordLengths(FileResource resource, int[] counts) {
String abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (String word : resource.words()) {
String trim = word.trim();
int wordSize = trim.length();
//System.out.println("word" + "\t" + trim);
int lastInd = trim.length()-1;
//System.out.println("lastIndexOf word" + "\t" + lastInd);
//abc.contains(trim.charAt(lastInd));
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);
//System.out.println("firstChar" + "\t" + firstChar + "\t" + "endChar" + "\t" + endChar);
int idx = abc.indexOf(firstChar);
int idx_e = abc.indexOf(endChar);
int edx_sum = idx + idx_e;
//System.out.println("indexes abc" + "\t" + edx_sum);
//int idx_s = small.indexOf(firstChar);
//int idx_s_e = small.indexOf(endChar);
//int edx_s_sum = idx_s + idx_s_e;
//System.out.println("indexes small" + "\t" + edx_s_sum);
//System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t" + idx_s + "\t" + idx_s_e);
//System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t");
if (idx == -1 && idx_e == -1) {
wordSize -= 2;
} else
if (idx == -1 || idx_e == -1) {
wordSize -= 1;
}
if(wordSize>=counts.length) {
counts[counts.length-1] += 1;
} else
//right algorithm
if( counts[wordSize] != 0) {
counts[wordSize] += 1;
} else {
counts[wordSize] = 1;
}
}
//test
/*for(int i : counts) {
System.out.println(i);
}*/
}
/**
* the method countWordLengths(FileResource resource, int[] counts) with isLetter method
*
* @param resource
* @param counts
*/
public void countWordLengthsWithIsLettermethod(FileResource resource, int[] counts) {
for (String word : resource.words()) {
String trim = word.trim();
int wordSize = trim.length();
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);
if (!Character.isLetter(firstChar) && !Character.isLetter(endChar)) {
wordSize -= 2;
} else
if (!Character.isLetter(firstChar) || !Character.isLetter(endChar)) {
wordSize -= 1;
}
if(wordSize>=counts.length) {
counts[counts.length-1] += 1;
} else
//right algorithm
if( wordSize> 0 && counts[wordSize] != 0 ) {
counts[wordSize] += 1;
} else if ( wordSize> 0) {
counts[wordSize] = 1;
}
System.out.println(counts[wordSize] + " words with length " + wordSize + ": " + word);
}
//test
/*for(int i : counts) {
System.out.println(i);
}*/
}
public int maxIndex(int[] freqs) {
int max = freqs[0];
for (int i = 1; i < freqs.length; i++) {
if (freqs[i] > max) {
max = freqs[i];
}
}
System.out.println("max number of words of certain lengths: "+max);
return freqs[max];
//return max;
}
public void testWordLengths() {
WordLengths wl = new WordLengths();
FileResource fr = new FileResource();
int counts[] = new int[100];
//wl.countWordLengths(fr, counts);
wl.countWordLengthsWithIsLettermethod(fr, counts);
wl.maxIndex(counts);
}
}
以下是包含问题区域的整个代码:
foreach (Kid item in array)
{
Console.WriteLine(item.years);
}
答案 0 :(得分:0)
如果这个词是空的(或只是填空),会怎么想?
String trim = word.trim();
int wordSize = trim.length();
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);