英文:
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 = "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++;
    
    System.out.println("Total number of words: " + wordcount);  
    System.out.println(sentence.startsWith("P"));
}}  
My question is how can i define the String sentence based on this condition:
- If more than 3 words, it will be True.
 - 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(" ");
if(tokens.length() > 3) {
  // true
} else {
  // fasle
}
答案2
得分: 0
让我们看一下实现目标更容易的步骤:
- 
首先,通过count函数计算输入字符串中的单词数。
 - 
通过发送我们的输入句子来调用该函数。
 - 
函数返回单词数,根据您的需要检查单词数的任何条件。
 
因此,您的代码将更好地像这样工作:
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;
- 
First , let's count the number of words in your input string via
count function - 
Call the function by sending our input sentence
 - 
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 = "Papa beauty lies in the eyes of beholder";
    private bool coniditon;
    int wordcount = count(sentencte);
      
    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; }
   }
}
Good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论