A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 王震阳老师   /  2014-4-30 11:23  /  41484 人查看  /  452 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package Practice;

  2. class CharacterSorter
  3. {
  4.         public static String CharacterSort(String s)
  5.         {
  6.                 int[] count=new int[4];
  7.                 char[] chs=s.toCharArray();
  8.                 for(char ch:chs)
  9.                 {
  10.                         if(ch==' ')count[0]++;
  11.                         else if ('0'<=ch&ch<='9')
  12.                                         count[1]++;
  13.                         else if(('a'<=ch&ch<='z')||
  14.                                         ('A'<=ch&ch<='Z'))
  15.                                 count[2]++;
  16.                         else
  17.                                 count[3]++;
  18.                 }
  19.                 int sum=count[0]+count[1]+count[2]+count[3];
  20.                 return "字符总数:"+sum+"个,空格数个数:"+count[0]+"个,数字个数:"
  21.                 +count[1]+"个,字母个数:"+count[2]+"个,其他字符个数:"+count[3];
  22.         }
  23.         }
  24. public class TestCharacterSorter
  25. {
  26.         public static void main(String[] args) {
  27.                 // TODO Auto-generated method stub
  28. String str=" sd  fhg ui sd 14 6SFG5434/.x,.a";
  29. System.out.println(CharacterSorter.CharacterSort(str));
  30.         }
  31. }
  32. //打印结果
  33. //字符总数:32个,空格数个数:7个,数字个数:7个,字母个数:14个,其他字符个数:4。
复制代码

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 包名和变量名不符合命名规范。.

查看全部评分

回复 使用道具 举报
我来看看
回复 使用道具 举报
这个先看看再说!!
回复 使用道具 举报
public class number01 {         public static void main(String[] args) {                 Scanner input = new Scanner(System.in);                 System.out.println("请输入一行字符:");                 String str = input.nextLine();// 接收输入                 int abcCount = 0;// 英文字母个数                 int trimCount = 0;// 空格键个数                 int numCount = 0;// 数字个数                 int otherCount = 0;// 其他字符个数                 char[] ch = str.toCharArray();// 转化字符数组                 for (char c : ch) {                         int i = (int) c;// 阿斯克马值转换                         if (48 <= i && i <= 57) {                                 numCount++;                         } else if ((48 <= i && i <= 90) || (97 <= i && i <= 122)) {                                 abcCount++;                         } else if (i == 32) {                                 trimCount++;                         } else {                                 otherCount++;                         }                 }                 System.out.println("字母个数:" + abcCount);                 System.out.println("数字个数:" + numCount);                 System.out.println("空格个数:" + trimCount);                 System.out.println("其他字符个数:" + otherCount);         } }
回复 使用道具 举报
搞定一个,先放上来

GetCountTest.zip

897 Bytes, 阅读权限: 100, 下载次数: 1

技术分快到碗里来

评分

参与人数 1技术分 +2 收起 理由
王震阳老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
我的答案已完成请验收!谢谢!

黑马程序员练习题答案.rar

1.8 KB, 下载次数: 186

评分

参与人数 1技术分 +2 收起 理由
王震阳老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
  1. package cn.itcast.heima;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;

  5. public class AlphaSum {
  6.         public static void main(String[] args) {
  7.                 System.out.println("请输入一行字符:");
  8.                 //此处用bufferedReader接受屏幕输入
  9.                 BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
  10.                 String str = null;
  11.                 //number数组用于接受各种字符个数
  12.                 int[] number = new int[4];
  13.                 //strs数组用于和number数组配合输出
  14.                 String[] strs = {"字母个数:","空格个数:","数字个数:","其他字符个数"};
  15.                 try {
  16.                         //读入一句
  17.                         str = std.readLine();
  18.                 } catch (IOException e) {
  19.                         // TODO Auto-generated catch block
  20.                         e.printStackTrace();
  21.                 }
  22.                 //此函数计算各个字符个数
  23.                 getNum(number,str);
  24.                 for(int i = 0;i < number.length;i++){
  25.                         System.out.println(strs[i] + number[i]);
  26.                 }
  27.         }
  28.        
  29.         public static void getNum(int[] number,String str){
  30.                 byte[] bytes = str.getBytes();
  31.                 for(byte b : bytes){
  32.                         if((b >= 65 && b <= 90) || (b >= 97 && b <= 122)){
  33.                                 number[0] ++;
  34.                         }else if(b == 32){
  35.                                 number[1] ++;
  36.                         }else if(b >= 48 & b <= 57){
  37.                                 number[2]++;
  38.                         }else{
  39.                                 number[3]++;
  40.                         }
  41.                 }
  42.         }
  43.        
  44.        
  45. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
王震阳老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
查看隐藏
回复 使用道具 举报
看看题目先
回复 使用道具 举报
看看题 .. 为什么还要回复啊
回复 使用道具 举报
提交第一题

TestDemo.zip

4.55 KB, 下载次数: 177

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
我要看看这到底是什么题
回复 使用道具 举报
  1. package string;
  2. //
  3. public class StringLianxi {
  4.         public static void main (String args[]){
  5.                 String s = "ABCDEabcd123456!@#$%^&";
  6.         char [] arr = s.toCharArray();
  7.         int numUp = 0;//大写字母计数器
  8.         int numLow = 0;//小写字母计数器
  9.         int num = 0;//数字计数器
  10.         int numOther = 0;//其他字符计数器
  11.         
  12.         //第一种方式
  13.           for(int x=0;x<s.length();x++){
  14.                 if(arr[x]>=65 && arr[x]<=90)
  15.                         numUp++;
  16.                 else if(arr[x]>=97 && arr[x]<=122)
  17.                         numLow++;
  18.                 else if(arr[x]>=48 && arr[x]<=57)
  19.                         num++;
  20.                 else
  21.                         numOther++;        
  22.         }
  23.         
  24.         //第二种方式
  25.        /* for(int x=0;x<arr.length;x++){
  26.                 char chs = arr[x];
  27.                 if(chs >= 'A' && chs < 'Z')
  28.                         numUp++;
  29.                 else if(chs >='a' && chs< 'z')
  30.                         numLow++;
  31.                 else if(chs >='0' && chs<= '9')
  32.                         num++;
  33.                 else
  34.                         numOther++;         
  35.                
  36.         }*/
  37.       
  38.         System.out.println("大写"+numUp);
  39.         System.out.println("小写"+numLow);
  40.         System.out.println("数字"+num);
  41.         System.out.println("其他字符"+numOther);
  42.                
  43.         }

  44. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
王震阳老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
看看是啥!
回复 使用道具 举报
求看求给力
回复 使用道具 举报
sheng6699 发表于 2014-4-30 18:14
import java.util.*;
public class Demo {
        public static void main(String[] args) {

回复代码有样式可用,这样的代码不是很美观
回复 使用道具 举报
ily521125 发表于 2014-4-30 17:20
又一轮技术分强势来袭

你的技术分挺好高呀
回复 使用道具 举报
拿来做做
回复 使用道具 举报
本帖子需要验证码才可查看,请输入验证码:heima 即可正常访问  
回复 使用道具 举报
来领题~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马