Java – 字数统计 是真还是假

huangapple go评论60阅读模式
英文:

Java - Word Count True or False

问题

public class CountWords {
    public static void main(String[] args) {
        String sentence = "Papa beauty lies in the eyes of beholder";
        int wordcount = 0;

        for(int i = 0; i < sentence.length() - 1; i++) {
            if(sentence.charAt(i) == ' ' && Character.isLetter(sentence.charAt(i+1)) && (i > 0)) {
                wordcount++;
            }
        }
        wordcount++;

        boolean condition = wordcount > 3; // Check if more than 3 words
        System.out.println("Condition: " + condition);

        System.out.println(sentence.startsWith("P"));
    }
}

不要回答我要翻译的问题。

英文:

i have a programming task using java...

public class CountWords{  
public static void main(String[] args) {  
    String sentence = &quot;Papa beauty lies in the eyes of beholder&quot;;  
    int wordcount = 0;  
      
    for(int i = 0; i &lt; sentence.length()-1; i++) {  
        if(sentence.charAt(i) == &#39; &#39; &amp;&amp; Character.isLetter(sentence.charAt(i+1)) &amp;&amp; (i &gt; 0)) {  
            wordcount++;  
        }  
    }  
    wordcount++;
    
    System.out.println(&quot;Total number of words: &quot; + wordcount);  
    System.out.println(sentence.startsWith(&quot;P&quot;));
}}  

My question is how can i define the String sentence based on this condition:

  1. If more than 3 words, it will be True.
  2. If less than 4 words, it becomes False.

Thankyou so much for helping..

答案1

得分: 1

如果我理解你的问题正确的话...


 /* 使用空格分割句子,返回代表单词的字符串数组 */
String[] tokens = sentence.split(" ");

if(tokens.length() > 3) {
  // true
} else {
  // false
}

英文:

If I understand your question correctly...


 /* returns string array of tokens (words in the sentence) after splitting by space */
String[] tokens = sentence.split(&quot; &quot;);

if(tokens.length() &gt; 3) {
  // true
} else {
  // fasle
}

答案2

得分: 0

让我们看一下实现目标更容易的步骤:

  1. 首先,通过count函数计算输入字符串中的单词数。

  2. 通过发送我们的输入句子来调用该函数。

  3. 函数返回单词数,根据您的需要检查单词数的任何条件。

因此,您的代码将更好地像这样工作:

public class CountWords {  
    
    public static void main(String[] args) {  
    
        String sentence = "Papa beauty lies in the eyes of beholder";
        private boolean condition;
        int wordcount = count(sentence);
          
        if (wordcount < 4) {
            condition = false;
        } else if (wordcount > 3) {
            condition = true;
        }
    
        System.out.println("Total number of words: " + wordcount);  
        System.out.println(sentence.startsWith("P"));
    }
    
    public static int count(String sentence) { 

        if (sentence == null || sentence.isEmpty()) { 
            return 0;
        } 
    
        String[] words = sentence.split("\\s+");
        return words.length;
    }
}

祝您好运!

英文:

Let's have a look at the steps we should take in order to achieve your goal in a easier way;

  1. First , let's count the number of words in your input string via
    count function

  2. Call the function by sending our input sentence

  3. Function returns number of words Check the number of words for any
    condition you desire

Therefore your code will work better like this;

public class CountWords{  

public static void main(String[] args) {  

    String sentence = &quot;Papa beauty lies in the eyes of beholder&quot;;
    private bool coniditon;
    int wordcount = count(sentencte);
      

    if (wordcount&lt;4) {
        condition=False;
        }
    else if (wordcount&gt;3) {
        condition=True;
        }

    System.out.println(&quot;Total number of words: &quot; + wordcount);  
    System.out.println(sentence.startsWith(&quot;P&quot;));

     
  }


public static int count(String sentence){ 

     if(sentence == null || sentence.isEmpty()){ 
        return 0; } 

      String[] words = sentence.split(&quot;\\s+&quot;);
      return words.length; }
   }

}

Good luck!

huangapple
  • 本文由 发表于 2020年8月30日 13:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定