我正在为学校做这个项目。
基本上,您输入任何短语,程序将其分解为特定字母的数量并放入一个数组和出现次数最多的字母。
前任。
I am not Santa
Santa lives in the north pole
Santa is not Canadian
a=10
b=0
c=1
等等,加上
most occuring=a
这是我的主要内容:
import java.util.Scanner;
public class LetterDriver{
public static void main(String[] args){
String[] lines = new String[3];
Scanner scan = new Scanner(System.in);
System.out.println("enter any phrases, then press enter");
int pos = 0;
String tScan = " ";
while(tScan.length() > 0){
tScan = scan.nextLine();
lines[pos] = tScan;
pos++;
}
LetterProfile.printResults(tScan);//ERROR, I feel it's a syntax issue here, but I can't figure it out.
}
}
这是我的另一堂课:
public class LetterProfile{
int cCount[] = new int [26];
public void countChars (String s){
s.toLowerCase();
char a = 'a';
for (int i =0;i < s.length();i++){
int pos = (int)s.charAt(i) -(int) a;
if ( pos >=0 && pos < 26){
cCount[pos]++;
}
}
}
public int mostOccur(){
int largest = 0;
int largestindex = 0;
for(int a = 0; a < 26; a++){
if(cCount[a] > largest){
largest = cCount[a];
}
}
return (largestindex);
}
public void printResults(){
System.out.println(this.mostOccur());
}
public void runProg(String a){
a.countChars();
System.out.println(mostOccur(a)); //ERROR
}
}
我已经为此工作了大约 5 个小时,我找不到任何问题。我认为该程序所需的所有部分已经在那里,但我只需要更好地组织它。
它给了我这些错误:
2 errors found:
File: C:\Users\Mike\Desktop\LetterDriver.java [line: 14]
Error: method printResults in class LetterProfile cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length
File: C:\Users\Mike\Desktop\LetterProfile.java [line: 30]
Error: method mostOccur in class LetterProfile cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length