我是java初学者,对java的理解非常薄弱。对于我已经工作的代码,它已经计算了一个句子中的单词数,一个句子的总字符数和每个单词的总字符数,我想添加另一个函数。
我想添加一段代码来计算字长的平均值,例如,如果我输入“嗨嗨猫狗”,则输出为2.4。 (因为这个句子的总字符数是12,除以单词的数量(5个单词)得到的平均值为2.4)。
下面是我正在编写的代码片段,这是我基于许多教程创建的代码,但它们都教授数字的平均值,而不是字长。我的想法是我的代码应该首先计算每个单词(word.length)的字符总和,然后除以单词之和(sentence.length)。但它似乎不起作用。 你能帮我纠正这段代码吗?
{
//prints out average word length
int length = wordcount / word.length ;
sum = sum + word.length / sentence length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);} //outputs the sum calculated above
{
下面有我的完整代码,可以帮助您更好地理解我的意思。谢谢你的帮助!
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in); //This adds a scaner/text window to the program.
while(true)
{ // have created infinite loop.
System.out.print("Enter your text or enter 'quit' to finish the program: ");
String sentence = in.nextLine();
if(sentence.equals("quit"))
{ // if enterd value is 'quit' than it comes out of loop
break;
}
else
{ //else if 'quit' wasn't typed it, the program displays whats underneath.
System.out.println("You have entered: "
+ sentence); // to print what the user has entered in the text window/scanner.
System.out.println("The total number of characters is " + sentence.length()
+ "."); // to print the total number of characters
System.out.println("This piece of text has " + countWords(sentence)
+ " words."); //this method counts the number of words in the entered sentence.
String[] words =
sentence.split(" "); // to get the individual words, splits them when the free space (" ") is found.
int maxWordLength = 0;
int wordLength = 0;
for(int i = 0; i < words.length; i++)
{
wordLength = words[i].length();
if(wordLength > maxWordLength)
{ //This piece of code is an array which counts the number of words with the same number of characters.
maxWordLength = wordLength;
}
}
int[] intArray = new int[maxWordLength + 1];
for(int i = 0; i < words.length; i++)
{
intArray[words[i].length()]++;
}
for(int i = 1; i < intArray.length; i++)
{
System.out.printf("There are " + "%d word(s) of length %d<\n>", intArray[i], i);
}
System.out.println("The numbers of characters for each word:"); //word.length method counts the number of characters for each word.
for(int i = 0; i < words.length; i++)
{
System.out.println(words[i] + " = " + words[i].length() + " characters");
}
}
}
}
{
//prints out average word length
int length = wordcount / world.length;
sum = sum + word.length / sentence
length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);
} //outputs the sum calculated above
{
in.close();
}
private static int countWords(String str)
{ //this piece of code splits the words when the space (" ") is found and prints out the length of words.
String words[] = str.split(" ");
int count = words.length;
return count;
}
}
答案 0 :(得分:0)
您只需要拨打以下内容:
public static double getAverageCharLength(String str) {
String words[] = str.split(" ");
int numWords = words.length;
int totalCharacters = 0;
for(int i = 0; i < numWords; i++)
totalCharacters = totalCharacters + words[i].length();
return totalCharacters/numWords;
}
我无法告诉你哪里出错了,因为我无法理解你的代码混乱。但这是你应该遵循的逻辑。
注意:这不能正确计算平均字长如果您的字词包含撇号等特殊字符。我不确定您是否需要在您的情况下注意这一点,但如果您这样做,请查看正则表达式以指定要忽略的字符并使用String
等contains()
方法。
另请注意,您尝试定义的以下两种方法没有方法签名:
{
//prints out average word length
int length = wordcount / world.length;
sum = sum + word.length / sentence
length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);
} //outputs the sum calculated above
{
in.close();
}
如果您不确定如何正确编写这些方法,可能会尝试浏览oracle文档中的Java语法。
答案 1 :(得分:0)
使用拆分方法。 这是一个例子:
//returns the average word length of input string s
//the method is of double type since it will likely not return an integer
double avgWordLength(String s){
String delims=",;. ";//this contains all the characters that will be used to split the string (notice there is a blank space)
//now we split the string into several substrings called "tokens"
String[] tokens = s.split(delims);
int total=0;//stores the total number of characters in words;
for(int i=0; i<tokens.length(); i++){
total += tokens[i].length(); //adds the length of the word to the total
}
double avg = total/tokens.length();
return avg;
}
你有它。
答案 2 :(得分:0)
你可以这样尝试
String input = "hello Alpha this is bravo";
String[] strArray = input.split(" ");
float totalChars = 0;
for(String s : strArray){
totalChars += s.length();
}
float words = strArray.length;
float averageWordLength = (float)(totalChars/words);
System.out.println(averageWordLength);