当前位置: 代码迷 >> J2SE >> 学生党
  详细解决方案

学生党

热度:100   发布时间:2016-04-23 19:51:12.0
学生党求助
请先看代码
    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in); 
        int i=0, n=1,ab=0,bb=0,cb=0,db=0,eb=0,fb=0,gb=0,hb=0,ib=0,jb=0,kb=0,lb=0,mb=0,nb=0,ob=0,pb=0,qb=0,rb=0,sb=0,tb=0,ub=0,vb=0,wb=0,xb=0,yb=0,zb=0;
        
        System.out.println("请输入您的字符串,以整个单词或字母等组合为一个单位");
        System.out.print("请输入第一个字符串:");
        String[] str = new String[n];
        for(int a = 0; a < n; a++)
        {  
            String str1 = scan.next();  
            str[i] = str1;    
            System.out.print("是否输入完毕?请输入yes/no:");
            String str2 = scan.next(); 
            if( str2.equals( "yes") )
            {
                break ;
            }
            else
            {   
                i++; 
                n++;
                System.out.print("请输入下一个字符串:");   
            }            
        }
        
       for( i = 0; i < str.length; i++ )
        {   
            if(str[i].matches("a|A")){
                ab++;
            }
            else if(str[i].matches("b|B")){
                bb++;
            }
            else if(str[i].matches("c|C")){
                cb++;
            }
            else if(str[i].matches("d|D")){
                db++;
            }
            else if(str[i].matches("e|E")){
                eb++;
            }
            else if(str[i].matches("f|F")){
                fb++;
            }
            else if(str[i].matches("g|G")){
                gb++;
            }
            else if(str[i].matches("h|H")){
                hb++;
            }
            else if(str[i].matches("i|I")){
                ib++;
            }
            else if(str[i].matches("j|J")){
                jb++;
            }      
            else if(str[i].matches("k|K")){
                kb++;
            }
            else if(str[i].matches("l|L")){
                lb++;
            }
            else if(str[i].matches("m|M")){
                mb++;
            }
            else if(str[i].matches("n|N")){
                nb++;
            }
            else if(str[i].matches("o|O")){
                ob++;
            }
            else if(str[i].matches("p|P")){
                pb++;
            }
            else if(str[i].matches("q|Q")){
                qb++;
            }
            else if(str[i].matches("r|R")){
                rb++;
            }
            else if(str[i].matches("s|S")){
                sb++;
            }
            else if(str[i].matches("t|T")){
                tb++;
            }
            else if(str[i].matches("u|U")){
                ub++;
            }
            else if(str[i].matches("v|V")){
                vb++;
            }
            else if(str[i].matches("w|W")){
                wb++;
            }
            else if(str[i].matches("x|X")){
                xb++;
            }
            else if(str[i].matches("y|Y")){
                yb++;
            }
            else if(str[i].matches("z|Z")){
                zb++;
            }
   
        }
        System.out.println("您的字符串为:");        
        for(i = 0; i < str.length; i++)
        {
            System.out.print(str[i] +" ");
        }
        System.out.print("\n您的字符串中个数\n"+"a:"+ ab +" b:" + bb +" c:"+ cb +" d:"+ db +" e:"+ eb +" f:"+ fb +" g:"+ gb +" h:"+ hb +" i:"+ ib +" j:"+ jb +" k:"+ kb +" l:"+ lb +" m:"+ mb +" n:"+ nb +" o:"+ ob+" p:"+ pb +" q:"+ qb +" r:"+ rb +" s:"+ sb +" t:"+ tb +" u:"+ ub +" v:"+ vb +" w:"+ wb +" x:"+ xb +" y:"+ yb +" z:"+ zb); 
    }
}
怎么实现对数组内字符串内的单个字母判断个数?我的这个结果每个都是0,我要怎么改?
------解决思路----------------------
str[i]是一个以字符串为元素的数组,每输入一个字符串,就会当做一个元素存入数组内!当判断是否含有某个字母时,对照的对象是输入的字符串,而不是字母,当然结果是零!你输入一个字母当做字符串,就会有结果了!
------解决思路----------------------
import java.util.*;

public class test_1 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in); 
        int i=0, n=1,ab=0,bb=0,cb=0,db=0,eb=0,fb=0,gb=0,hb=0,ib=0,jb=0,kb=0,lb=0,mb=0,nb=0,ob=0,pb=0,qb=0,rb=0,sb=0,tb=0,ub=0,vb=0,wb=0,xb=0,yb=0,zb=0;
        
        System.out.println("请输入您的字符串,以整个单词或字母等组合为一个单位");
        System.out.print("请输入第一个字符串:");
        String[] str = new String[n];
        
        for(int a = 0; a < n; a++)
        {  
            String str1 = scan.next();  
            str[i] = str1;    
            System.out.print("是否输入完毕?请输入yes/no:");
            String str2 = scan.next(); 
            if( str2.equals( "yes") )
            {
                break ;
            }
            else
            {   
                i++; 
                n++;
                System.out.print("请输入下一个字符串:");   
            }            
        }
        
       for( i = 0; i < str.length; i++ )
       {
        for(int j=0;j<str[i].length();j++)
        {
        char char_=str[i].charAt(j);
        if(char_=='a'
------解决思路----------------------
char_=='A')
        {
        ab++;
        }
        if(char_=='b'
------解决思路----------------------
char_=='B')
        {
        bb++;
        }
        if(char_=='c'
------解决思路----------------------
char_=='C')
        {
        cb++;
        }
        if(char_=='d'
------解决思路----------------------
char_=='D')
        {
        db++;
        }
               if(char_=='e'
------解决思路----------------------
char_=='E')
               {
                eb++;
               }
               if(char_=='f'
------解决思路----------------------
char_=='F')
               {
                fb++;
               }
               if(char_=='g'
------解决思路----------------------
char_=='G')
               {
                gb++;
               }
               if(char_=='h'
------解决思路----------------------
char_=='H')
               {
                hb++;
               }
               if(char_=='i'
------解决思路----------------------
char_=='I')
               {
                   ib++;
               }
               if(char_=='j'
------解决思路----------------------
char_=='J')
               {
                   jb++;
               }      
               if(char_=='k'
------解决思路----------------------
char_=='K')
               {
                   kb++;
               }
               if(char_=='l'
------解决思路----------------------
char_=='L')
               {
                   lb++;
               }
               if(char_=='m'
------解决思路----------------------
char_=='M')
               {
                   mb++;
               }
               if(char_=='n'
------解决思路----------------------
char_=='N')
               {
                   nb++;
               } 
               if(char_=='o'
------解决思路----------------------
char_=='O')
               {
                   ob++;
               }
               if(char_=='p'
------解决思路----------------------
char_=='P')
               {
                   pb++;
               }
               if(char_=='q'
------解决思路----------------------
char_=='Q')
               {
                   qb++;
               }
               if(char_=='r'
------解决思路----------------------
char_=='R')
               {
                   rb++;
               }
               if(char_=='s'
------解决思路----------------------
char_=='S')
               {
                   sb++;
               }
               if(char_=='t'
------解决思路----------------------
char_=='T')
               {
                   tb++;
               }
               if(char_=='u'
------解决思路----------------------
char_=='U')
               {
                   ub++;
               }
               if(char_=='v'
------解决思路----------------------
char_=='V')
               {
                   vb++;
               }
               if(char_=='w'
------解决思路----------------------
char_=='W')
               {
                   wb++;
               }
               if(char_=='x'
------解决思路----------------------
char_=='X')
               {
                   xb++;
               }
               if(char_=='y'
------解决思路----------------------
char_=='Y')
               {
                   yb++;
               }
               if(char_=='z'
------解决思路----------------------
char_=='Z')
               {
                   zb++;
               }
        }
   
        }
        System.out.println("您的字符串为:");        
        for(i = 0; i < str.length; i++)
        {
            System.out.print(str[i] +" ");
        }
        System.out.print("\n您的字符串中个数\n"+"a:"+ ab +" b:" + bb +" c:"+ cb +" d:"+ db +" e:"+ eb +" f:"+ fb +" g:"+ gb +" h:"+ hb +" i:"+ ib +" j:"+ jb +" k:"+ kb +" l:"+ lb +" m:"+ mb +" n:"+ nb +" o:"+ ob+" p:"+ pb +" q:"+ qb +" r:"+ rb +" s:"+ sb +" t:"+ tb +" u:"+ ub +" v:"+ vb +" w:"+ wb +" x:"+ xb +" y:"+ yb +" z:"+ zb); 
    }
}

  相关解决方案