//【程序7】
//题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
//1.程序分析:利用while语句,条件为输入的字符不为'\n'.
//一下是我自己写的程序,有错误,请问这样写的思路对吗?可以改吗?
package test;public class test7CountNumber {
public String string;
public int i = 0;
public int blank = 0;
public int character = 0;
public int number = 0;
public test7CountNumber(String string) {
this.string = string;
}
public static void main(String[] args) {
test7CountNumber t = new test7CountNumber("asdfefg 1234");
t.unit(t.string); }
public void unit(String string) { while(string.length() != '\n') {
if((int)string.charAt(i) == 32){
blank++;
} else if ((int)string.charAt(i) >= 65 && (int)string.charAt(i) <= 122) {
character++;
} else if ((int)string.charAt(i) >= 0 && (int)string.charAt(i) <= 9) {
number++;

i++;
}
System.out.println(blank);
System.out.println(character);
System.out.println(number);
}
}

解决方案 »

  1.   

        public void unit(String string)
        {
            for (int i = 0; i < string.length(); i++)
            {
                if ((int) string.charAt(i) == 32)
                {
                    blank++;
                }
                else if ((int) string.charAt(i) >= 65
                        && (int) string.charAt(i) <= 122)
                {
                    character++;
                }
                else if ((int) string.charAt(i) >= 48
                        && (int) string.charAt(i) <= 57)
                {
                    number++;
                }        }
            System.out.println(blank);
            System.out.println(character);
            System.out.println(number);
        }
      

  2.   

    public static void addUpChar(String s)
    {
    char c;
    int sum=0;
    int letters=0,space=0,digital=0,other=0;
    for(int i=0;i<s.length();i++){
    c=s.charAt(i);
    if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
    letters++;
    else if(c==' ')
    space++;
    else if(c>='0'&&c<='9')
    digital++;
    else 
    other++;
    }
    sum=letters+space+digital+other; System.out.println("一共有"+sum+"个字符, "+"英文字母有" + letters + "个,空格有"+space + "个,数字有" + digital + ", 其他字符有" + other+"个.");
      

  3.   

    while( string.charAt(i)!= '\n') {
    if((int)string.charAt(i) == 32){
    blank++;
    } else if ((int)string.charAt(i) >= 65 && (int)string.charAt(i)  <=  122) {
    character++;
    } else if ((int)string.charAt(i) >= 0 && (int)string.charAt(i)  <= 9)  {
    number++;

    i++;
    if(i==string.length())
    break;

    }
      

  4.   

    给2楼的排个版
    还有LZ自己情况都没考虑完啊....注意是四种情况public static void addUpChar(String s) 

    char c; 
    int sum=0; 
    int letters=0,space=0,digital=0,other=0; 
    for(int i=0;i <s.length();i++){ 
    c=s.charAt(i); 
    if((c>='a'&&c <='z') ¦ ¦(c>='A'&&c <='Z')) 
    letters++; 
    else if(c==' ') 
    space++; 
    else if(c>='0'&&c <='9') 
    digital++; 
    else  
    other++; 

    sum=letters+space+digital+other; System.out.println("一共有"+sum+"个字符, "+"英文字母有" + letters + "个,空格有"+space + "个,数字有" + digital + ", 其他字符有" + other+"个."); 
      

  5.   

    重贴2L代码 去掉些错误public void unit(String s) 

    char c; 
    int sum=0; 
    int letters=0,space=0,digital=0,other=0; 
    for(int i=0;i <s.length();i++)

    c=s.charAt(i); 
    if((c>='a'&&c <='z')||(c>='A'&&c <='Z')) 
    letters++; 
    else if(c==' ') 
    space++; 
    else if(c>='0'&&c <='9') 
    digital++; 
    else  
    other++; 

    sum=letters+space+digital+other; 
    System.out.println("一共有"+sum+"个字符, "+"英文字母有" + letters + 
    "个,空格有"+space + "个,数字有" + digital + ", 其他字符有" + other+"个.");

      

  6.   

    你的代码和题意有些偏离
    题目要求:输出每行输入中字母、数字、空格的个数;你的代码没有达到要求//Scanner in = new Scanner(System.in);
    //String input = in.nextLine();
    等很多方式可以,实现读取一行字符串你结合二楼说的,应该可以实现:-)