*6.20(计算一个字符串中字母的个数)编写一个方法,使用下面的方法头计算字符串中的字母个数:
public static int countletters(string s)
编写一个测试程序,提示用户输入字符串,然后显示字符串中的字母个数。
*6.20(count the letters in a string) write a method that counts the number of letters in a string using the following header:
public static int countletters(string s)
write a test program that prompts the user to enter a string and displays the number of letters in the string.
下面是参考答案代码:
import java.util.scanner; public class ans6_20_page201 { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.print("enter : "); string s = input.nextline(); system.out.println("the number of letters is "+ countletters(s)); } public static int countletters(string s) { int count = 0; for (int i = 0;i < s.length();i++) { if (character.isletter(s.charat(i))) count++; } return count; } }
适用java语言程序设计与数据结构(基础篇)(原书第11版)java语言程序设计(基础篇)(原书第10/11版)
如对本文有疑问, 点击进行留言回复!!
解决idea中出现“illegal character U+200B” 问题
荐 为什么加了@Transactional注解,事务没有回滚?
Attribute ‘sklearn.linear_model._logistic.LogisticRegression.multi_class‘ must be explicitly set to
Java/Python实现 LeetCode剑指Offer 14-I.剪绳子(动态规划)
网友评论