本文主要是介绍java英文绕口令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
英文绕口令
peter piper picked a peck of pickled peppers
统计这段绕口令有多少个以p开头的单词
思路:先通过split分割字符串
在通过取首部比较
取num++
package Fengzhaung;public class conclude {public static int count(String sentence, char target) {String[] subsentence = sentence.split(" ");int num = 0;//开头字母的字符串数目for (int i = 0; i < subsentence.length; i++) {String a = subsentence[i].substring(0, 1);if (a.charAt(0) == target) num++;}return num;}public static void main(String[] args) {String sentence = "peter piper picked a peck of pickled peppers";System.out.println(sentence);char target = 'p';System.out.println("这段绕口令有" + count(sentence, target) + "个以" + target + "开头的英文单词");}
}
这篇关于java英文绕口令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!