【问题标题】:How can I write (and fix) this code without loops in java如何在 java 中编写(和修复)没有循环的代码
【发布时间】:2021-07-31 06:57:21
【问题描述】:

只是想知道如何在没有任何循环的情况下将此代码切换到不同的代码,但仍保持其所做的事情。仅使用 if\switch 并修复它实际上它似乎有效

  public static int countWords(String s){

    int wordCount = 0;

    boolean word = false;
    int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!Character.isLetter(s.charAt(i)) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}

【问题讨论】:

  • 没有任何循环,但仍然保持它正在做的事情。只使用 if\switch 那是 not 可能的。但你可以做return s.split("\\s+").length;
  • 试试s.split("\\PL+"),它将字符串s拆分为任何非字符。
  • “它实际上似乎有效”——然后发布 minimal reproducible example 并告诉我们它是如何无效的
  • @A.B 你能解释一下你想要实现的目标吗?例如,我使用字符串“aaddrr12345;2C”运行您的代码,结果得到 2。为什么?你想数什么?
  • @AB 例如,在字符串中我尝试了 "aaddrr12345;2C" 我在字符串的中间数了 7 个非字母,在末尾加上一个字母,所以预期的结果是 8,你为什么要 2 来代替?

标签: java loops if-statement switch-statement


【解决方案1】:

如果我理解的很好,我可以用以下两种情况来总结你的算法:

  1. 统计所有以“单词字符”开头的“非单词字符”
  2. 如果字符串以字母结尾,则增加计数器。

通过使用具有适当模式的 String.split() 函数,这绝对可以在没有循环的情况下实现。

可以有很多解决方案,但只是为了给您一个想法,这里是一个实现示例,我将字符串拆分为“全字母字符”模式 (\\p{Alpha}+)。
如果字符串以非单词(字符串数组的第一个元素)开头,则计数器递减。
此外,如果案例 n.2 得到验证,则计数器会增加。
你可以看到你的实现(方法countWords)和我的(方法countWordsNoLoop)之间的区别:

import java.util.regex.Pattern;

public class WordCounter {
    public static void main(String args[]) {
        String test1 = "aaddrr12345;2C";
        String test2 = "1111aaddrr12345;2C";
        String test3 = "aaaaa7877sssss1111aaddrr12345;2C";
        
        
        System.out.println("Counter " + test1 + " with loop: " + countWords(test1));
        System.out.println("Counter " + test1 + " without loop: " + countWordsNoLoop(test1));
        System.out.println();
        System.out.println("Counter " + test2 + " with loop: " + countWords(test2));
        System.out.println("Counter " + test2 + " without loop: " + countWordsNoLoop(test2));
        System.out.println();
        System.out.println("Counter " + test3 + " with loop: " + countWords(test3));
        System.out.println("Counter " + test3 + " without loop: " + countWordsNoLoop(test3));
    }
    
    public static int countWordsNoLoop(String s){
        String[] splitString =  s.split("\\p{Alpha}+");
        
        int wordCount = (s.startsWith(splitString[0]) ? splitString.length - 1 : splitString.length) + 
                        (Character.isLetter(s.charAt(s.length()-1)) ? 1 : 0 );

        return wordCount;
    }
    
    
    
    public static int countWords(String s){

        int wordCount = 0;
    
        boolean word = false;
        int endOfLine = s.length() - 1;
    
        for (int i = 0; i < s.length(); i++) {
            // if the char is a letter, word = true.
            if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
                word = true;
                // if char isn't a letter and there have been letters before,
                // counter goes up.
            } else if (!Character.isLetter(s.charAt(i)) && word) {
                wordCount++;
                word = false;
                // last word of String; if it doesn't end with a non letter, it
                // wouldn't count without this.
            } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
                wordCount++;
            }
        }
        return wordCount;
    }
}

这是输出:

Counter aaddrr12345;2C with loop: 2
Counter aaddrr12345;2C without loop: 2

Counter 1111aaddrr12345;2C with loop: 2
Counter 1111aaddrr12345;2C without loop: 2

Counter aaaaa7877sssss1111aaddrr12345;2C with loop: 4
Counter aaaaa7877sssss1111aaddrr12345;2C without loop: 4

【讨论】:

    【解决方案2】:

    我不确定您最初的问题,但您的问题似乎可以通过替换您的函数来解决

    return s.split(" ").length
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多