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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 337091921 中级黑马   /  2013-4-27 00:02  /  1963 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。   

评分

参与人数 1技术分 +1 收起 理由
曹睿翔 + 1 加油!多来提问,对大家的回答要仔细分析回.

查看全部评分

6 个回复

倒序浏览
  1. import javax.swing.JOptionPane;

  2. public class Count {
  3.         public static void main(String []args){
  4.                 int ch=0;
  5.                 int nu=0;
  6.                 int blank=0;
  7.                 int ot=0;
  8.                 String st = JOptionPane.showInputDialog("请输入字符串");
  9.                 for(int i=0;i<st.length();i++){
  10.                         char n=st.charAt(i);
  11.                         if(n>='0'&&n<='9')nu++;
  12.                         else if((n>='a'&n<='z')||(n>='A'&n<='Z'))ch++;
  13.                         else if(n==' ')blank++;
  14.                         else ot++;               
  15.                 }
  16.                 JOptionPane.showMessageDialog(null, "字母的个数为:"+ch+"\n数字的个数为:"+nu+"\n空格的个数为:"+blank+"\n其他符号的个数为:"+ot);
  17.         }

  18. }
复制代码

点评

给新人再加点注释吧  发表于 2013-4-27 08:44

评分

参与人数 1技术分 +1 收起 理由
曹睿翔 + 1 用到的Swing包中的静态方法是亮点.

查看全部评分

回复 使用道具 举报
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {
int digital = 0;
int character = 0;
int other = 0;
int blank = 0;
     char[] ch = null;
     Scanner sc = new Scanner(System.in);
     String s = sc.nextLine();
     ch = s.toCharArray();
     for(int i=0; i<ch.length; i++) {
      if(ch >= '0' && ch <= '9') {
       digital ++;
      } else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {
       character ++;
      } else if(ch == ' ') {
       blank ++;
      } else {
       other ++;
      }
      }
     System.out.println("数字个数: " + digital);
     System.out.println("英文字母个数: " + character);
     System.out.println("空格个数: " + blank);
     System.out.println("其他字符个数:" + other );
}
}
你看看行么呵呵 {:soso_e121:}求分啊

评分

参与人数 1技术分 +1 收起 理由
曹睿翔 + 1 当然行了,新人鼓励,加油

查看全部评分

回复 使用道具 举报
本帖最后由 xiewen 于 2013-5-1 15:51 编辑
Mycode 发表于 2013-4-27 00:10
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {

你写的很好!是不是这个编辑器有问题,那个[i]不能显示出来,所以"ch[i] 只能显示ch,希望版主反映一下这个问题
回复 使用道具 举报


如果仍有问题,请继续追问,如果问题已解决,请将分类改为已解决,谢谢
回复 使用道具 举报
import java.util.Scanner;
public class StringDemo {
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                System.out.println("请输入字符串:");
                Scanner sc=new Scanner(System.in);//从控制台输入字符串
                String str=sc.nextLine();
                System.out.println(str);
                char[] ch=str.toCharArray();//字符串转化成字符数组
                int count1=0,count2=0,count3=0;
                for (int i = 0; i < ch.length; i++) {
                        if(ch[i]>='0'&&ch[i]<='9')
                                count1++;
                        else
                                if(ch[i]>='A'&&ch[i]<='Z')
                                        count2++;
                                else
                                        if(ch[i]>='a'&&ch[i]<='z')
                                        count3++;
       
                }
                System.out.println("数字个数为:"+count1);
                System.out.println("大写字母个数为:"+count2);
                System.out.println("小写字母个数为:"+count3);
                System.out.println("其他字符个数为:"+(ch.length-count1-count2-count3));
        }

}
回复 使用道具 举报
import java.util.Scanner;

public class Test {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);//创建一个输入对象
                System.out.println("请输入一串字符");
                String s1 = sc.nextLine();//用字符串S1接收
                char[] ch1 = s1.toCharArray();//把S1转换成字符型数组
                int da = 0;//定义几个计数器
                int xiao = 0;
                int shu = 0;
                int biao = 0;
                for (int x = 0; x < ch1.length; x++) {//用for循环遍历数组中的所以元素
                        if (ch1[x] >= 'A' && ch1[x] <= 'Z') {//用if判断数组中元素是否满足条件。满足的话计数器就自增一次
                                da++;//本来是不能直接写A的,要去ASCII码表去查看。但是因为是字符型数组。所以直接把判断条件定义成字符型的了
                        } else if (ch1[x] >= 'a' && ch1[x] <= 'z') {
                                xiao++;
                        } else if (ch1[x] >= '0' && ch1[x] <= '9') {
                                shu++;
                        } else {
                                biao++;
                        }
                }//最后输出
                System.out.println("这个字符串中大写字母有" + da);
                System.out.println("这个字符串中小写字母有" + xiao);
                System.out.println("这个字符串中数字有" + shu);
                System.out.println("这个字符串中标点符号有" + biao);
        }
}
版主。虽然我代码和上面的差不多。但这真的是我写的啊 。。。。不要误会了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马