【问题标题】:Program to count the number of words in a sentence above the minimium letter requirements计算句子中超过最小字母要求的单词数的程序
【发布时间】:2012-10-08 06:37:27
【问题描述】:

这个程序会询问您一个单词和一个句子的最小长度。该程序的目的是统计一个句子中满足字母长度要求的单词数量。谁能帮我算一下,字数不会增加。

public class wordcount {

   public static void main(String[] args) {
      int length = IO.readInt();
      String sentence = IO.readString();
      int full = sentence.length();
      int wordcount = 0;

      for(int i = 0; i == length; i++){
         if(Character.isLetter(sentence.charAt(i)))
            wordcount= wordcount + 1;
      }
      System.out.print(wordcount);
   }
}

【问题讨论】:

    标签: java string io word-count


    【解决方案1】:

    在for循环中,共有三个表达式:

    • 初始化
    • 终止
    • 增量

    您的初始化和增量都很好,但是如果您想要迭代,您的终止表达式没有任何意义。正如其他人所说,将您的终止表达式更改为i < length 将纠正此问题,并允许您循环length 次数,因为i 将迭代直到它大于length,此时终止表达式将被满足,程序将继续进行。

    【讨论】:

      【解决方案2】:
      for(int i = 0; i == length; i++){
      

      您的for 循环立即退出。 i 肯定不等于length;这是0

      【讨论】:

        【解决方案3】:

        改变你的 for 循环:-

        for(int i = 0; i == length; i++)  // Code in this loop will not run even once.
        

        收件人:-

        for(int i = 0; i < length; i++)
        

        【讨论】:

          猜你喜欢
          • 2016-05-18
          • 2015-10-08
          • 2021-04-01
          • 1970-01-01
          • 2013-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-06
          相关资源
          最近更新 更多