小错能自己改 就是
LetterFrequencyDetector.java:53: 无法从静态上下文中引用非静态 方法 buildAlphabet
()
buildAlphabet( );
^
LetterFrequencyDetector.java:54: 无法从静态上下文中引用非静态 方法 removeNonLett
ers(java.lang.String)
ArrayList a=removeNonLetters(args[0]);
^
LetterFrequencyDetector.java:55: 无法从静态上下文中引用非静态 方法 calculateFreq
uencies(java.util.ArrayList)
calculateFrequencies(a);
^
LetterFrequencyDetector.java:56: 无法从静态上下文中引用非静态 方法 displayFreque
ncies()
displayFrequencies();
实在不知道怎么改了..
代码:
import java.util.*;
public class LetterFrequencyDetector{
ArrayList<Character> Alphabet = new ArrayList<Character>( );
ArrayList<Character> reAlphabet=new ArrayList<Character>( );
ArrayList<Integer> Frequency=new ArrayList<Integer>();
int[] count=new int[26];
public ArrayList buildAlphabet( ){
for(int i=97;i<=122;i++){
Alphabet.add((char)i);
}
return Alphabet;
}
public ArrayList removeNonLetters(String s){
s=s.toLowerCase();
for(int i=0;i<s.length();i++){
int c=s.charAt(i);
if(c>=97&&c<=122)
count[c-97]++;
}
for(int j=0;j<26;j++){
if(count[j]==0)
reAlphabet.remove(j);
}
return reAlphabet;
}
public ArrayList calculateFrequencies(ArrayList a){
Frequency.size=reAlphabet.size;
for(int i=0;i<26;i++){
if(count[i]!=0)
Frequency.add(count[i]);
}
return Frequency;
}
public void displayFrequencies(){
for(int i=0;i<reAlphabet.size();i++){
System.out.print(reAlphabet.get(i)+"--"+Frequency.get(i));
for(int j=0;j<Frequency.get(i);j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main(String args[]){
buildAlphabet( );
ArrayList a=removeNonLetters(args[0]);
calculateFrequencies(a);
displayFrequencies();
}
}