输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
2012-08-02 00:13
567 查看
import java.util.*; /** * * @author 码农小江 * tongji.java * 2012-8-2上午12:16:32 */ public class tongji { public static void main(String[] args) { int abcCount=0;//英文字母个数 int spaceCount=0;//空格键个数 int numCount=0;//数字个数 int otherCount=0;//其他字符个数 Scanner scan=new Scanner(System.in); String str=scan.nextLine(); char[] ch = str.toCharArray(); for(int i=0;i<ch.length;i++){ if(Character.isLetter(ch[i])){ //判断是否字母 abcCount++; } else if(Character.isDigit(ch[i])){ //判断是否数字 numCount++; } else if(Character.isSpaceChar(ch[i])){ //判断是否空格键 spaceCount++; } else{ //以上都不是则认为是其他字符 otherCount++; } } System.out.println("字母个数:"+abcCount); System.out.println("数字个数:"+numCount); System.out.println("空格个数:"+spaceCount); System.out.println("其他字符个数:"+otherCount); } }
方法二:
import java.util.*; import javax.swing.JOptionPane; /** * * @author 码农小江 * tongji.java * 2012-8-2上午12:16:32 */ public class tongji { public static void main(String []args){ int letter=0; int space=0; int digit=0; int other=0; String st = JOptionPane.showInputDialog("请输入字符串"); for(int i=0;i<st.length();i++){ char n=st.charAt(i); if((n>='a'&n<='z')||(n>='A'&n<='Z')) letter++; else if(n==' ') space++; else if(n>='0'&&n<='9') digit++; else other++; } System.out.println("这一行字符串中字母数为:"+letter); System.out.println("这一行字符串中空格数为:"+space); System.out.println("这一行字符串中数字数为:"+digit); System.out.println("其它字符数为:"+other); } }
相关文章推荐
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 11_输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- #输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- java实现输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 用户输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 【程序7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 【程序17】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数