我在下面粘贴的代码是3种方法。第一种方法(maxLetterRecurrence)找到最多出现的字母,其次我想将发生次数最多的字母发送到第二个函数(printMax),这样我就可以打印出该函数中出现次数最多的字母。最后我想在我的main函数中调用print函数,也称为(secondaryMethodCalls)。出于某种原因,我无法理解它。我的计数器工作正常我的错误是在第二个方法(printMax)和第三个方法(secondaryMethodCalls)之间。请帮忙!对不起,如果缩进错误!
import java.util.*;
public class LetCount
{
public static final int NUMCHARS = 27;
// Method addr provided
public static int addr(char ch)
{
//int index[addr('ch')];
//return (count[addr(ch)]+1);
return (int) ch - (int) 'A' + 1;
}
public static void main(String[] args)
{
int[] count = new int[NUMCHARS];
secondaryMethodCalls(count);
} // end of main
public static void secondaryMethodCalls(int[] count)
{
Scanner keyboard = new Scanner(System.in);
String sampleInput= " ";
do{
sampleInput = keyboard.nextLine();
System.out.println(sampleInput);
letterCounter(sampleInput,count);
}while(keyboard.hasNext());
printMax(max);
print(count);
//char finalMaxLetter = maxLetterRecurrence(count);
//char finalMinLetter = minLetterRecurrence(count);
}
public static void letterCounter(String sampleInput, int[]count)
{
int length=sampleInput.length();
for(int i=0;i<=length-1;i++)
{
char ch = sampleInput.charAt(i);
if(ch >='A' && ch <= 'Z')
{
count[addr(ch)]++;
}
}
}//end of letter counter
public static void print(int[]count)
{
for(char ch = 'A'; ch <= 'Z'; ch++)
{
System.out.println("letter recurrence:"+ch+"times="+count[addr(ch)]);
}
}
public static char maxLetterRecurrence(int[] count)
{
char max = 'Z';
int testVal = count[addr('A')];
for(char ch = 'A'; ch <= 'Z'; ch++)
{
if(testVal < count[addr(ch)])
{
max=ch;
testVal = count[addr(ch)];
}
}
return max;
}//end of max letter counter
public static char minLetterRecurrence(int[] count)
{
char min = 'A';
int testVal = count[addr('A')];
for(char ch = 'A'; ch <= 'Z'; ch++)
{
if(testVal > count[addr(ch)])
{
min = ch;
testVal=count[addr(ch)];
}
}
return min;
}//end of min letter counter
public static void printMax(char max int[] count)
{
char finalMaxLetter = maxLetterReccurence(count);
System.out.println("most frequent letter:"+finalMaxLetter);
}
} // end of class
答案 0 :(得分:-1)
它没有用,或者你只是得到错误而无法编译?如果secon one,ofc存在问题 &#39;最大&#39;未在secondaryMethodCalls中定义,并且count未在printMax中定义。只要它们不是全局变量。