코드는 거의 옳은 것 같습니다. 내가 얻을 대답은 3.0이지만 그것은 2.6.I 있어야합니다. 나는/다른 stackoverflow 답변을 봤어 봤어하지만 난 수정이 필요가 없습니다. 2 시간 동안이 오류를 시도했습니다.문장 내 단어의 평균 길이를 구하십시오. Java
코드 :
public class Sentence { // words holds the array.
private static String[] words = {"hi","my","name","is","Bob"};
}
public String toString() { //Returns the array into Strings.
return Arrays.toString(words);
}
public double meanLength(String s) {
String wordysplit[] = toString().split(", ");
float numWords = wordysplit.length;
float totalChar = 0;
for (int i = 0; i < numWords; i++) {
totalChar += wordysplit[i].length();
}
return (totalChar/numWords);
}
public static void main(String[] args) // Really simplified this code to make it
cleaner for you guys
System.out.println("Mean word length:"+words.meanLength());
}
}
모든 도움을 주셔서 감사합니다. 감사. 여기
'float numWords = wordysplit.length * 1.0;','totalChar + = wordysplit [i] .length() * 1.0;'및'return ((totalChar/numWords) * 1.0;' – npinti
문장 클래스를 끝내는 중괄호가 있음을 알아 두십시오. 당신이 원하는 것 같지 않습니다. – alephtwo
@alephtwo 그래, 미안, 실수 였어. – bob9123