【问题标题】:Average Word value in a sentence based on ASCII value for each letter基于每个字母的 ASCII 值的句子中的平均 Word 值
【发布时间】:2017-05-20 23:11:20
【问题描述】:

我想根据 ASCII 值计算每个单词的平均值 例如:你好,H= 72,e=101,l=108,l=108,o=111
加起来会得到 500,然后根据字母数计算平均值,即 500/5 = 100 因此,Hello 的平均值 = 100,与“世界”等方式相同。 最后计算出所有单词的平均值,加起来显示为整个句子的最终平均值 这是我创建的代码,但它在线程“main”java.lang.ArrayIndexOutOfBoundsException 中给出了一个异常

   import java.util.*;
   import java.lang.*;
   import java.io.*;
    class Word
    {
    public static void main (String[] args) 
    {

    String str="Hello World";
    int average1=0;
    int j=0;
    int[] average=new int[20];
    String[] s=str.split(" "); //Splitting into each word
    for(String s1 : s)
    {
    char[] c=s1.toCharArray();
    for(int i=0;i<str.length();i++)
    {
        average[i]=(int)c[i]; //Average ASCII based value for each word
    }
    while(average[j]!=0)
    {
    average1=average[j]/s1.length(); //Sum up average of each Word and average of who words is calculated 
    System.out.print(average1); 
    j++;
    }
    }
    }
    }

如果有人能用一个好的逻辑帮助我,那将不胜感激。

【问题讨论】:

  • "[...] 即 500/5 =100 因此平均 Hello = 500" - 我不明白您对 "average".
  • java.lang.ArrayIndexOutOfBoundsException -&gt; 你正在循环 str.length() 等于 55 而不是使用这个使用 c.length 在你的 for 循环中,你的代码也有很多问题。
  • 感谢您的线索和后半部分,我仍处于学习 Java 的新手阶段,并挑战自己解决一些新事物,但有些事情相当复杂

标签: java


【解决方案1】:

实现这一目标的一种简单方法是执行以下操作:

public class WordAverage{
   public static void main (String[] args) {

       String str="Hello World"; 
       double average=0; // you need only one double variable, why double -> because of the division later 
        // note that if you don't want the decimal you can change it to int
        for(char c : str.toCharArray()){ // cycle through every char in the String
            if(c!=' '){ // if it is not a space
               average += (int)c; // sum its value
            }
        }

       average /= str.replace(" ", "").length(); // then divide the average value by the String length after removing the spaces (if any)
        System.out.println(average);
    }
  }

测试

Hello World     -> 102.0
How Are You?    -> 96.2
Fine Thank You! -> 95.23

【讨论】:

    【解决方案2】:

    检查此代码。这比你的要简单一些。它平均每个单词,然后平均平均值。

    import java.util.*;
    import java.lang.*;
    import java.io.*;
    class Word
    {
        public static void main (String[] args)
        {
    
            String str="Hello World";
    
            String[] s = str.split(" ");
            int[] average = new int[s.length];
            for(int i = 0; i<s.length; i++) {
                    int wordAverage = 0;
                    System.out.println(s[i]);
                    for(int j=0;j<s[i].length(); j++) {
                            wordAverage += (int)s[i].charAt(j); //Average ASCII based value for each word
                    }
                    average[i] = wordAverage/s[i].length();
                    System.out.println(average[i]);
            }
            int finalAverage = 0;
            for(int i = 0; i<average.length; i++)
                    finalAverage += average[i];
            finalAverage/=average.length;
            System.out.println(finalAverage);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多