繁体   English   中英

如何在 java 中编写(和修复)没有循环的代码

[英]How can I write (and fix) this code without loops in java

只是想知道如何在没有任何循环的情况下将此代码切换到不同的代码,但仍然保持它在做什么。 仅使用 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;
}

我不确定您最初的问题,但您的问题似乎可以通过将 function 替换为

return s.split(" ").length

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

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

这可以通过使用具有适当模式的String.split() function 绝对实现而无需循环。

可以有很多解决方案,但只是为了给你一个想法,这里是一个实现示例,我用“全字母字符”模式( \\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;
    }
}

这是 output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM