/*
 *实现从命令行参数中输入一字符串,统计'e'出先的次数
 *name: recordCharE.java
 */
 public class recordCharE
 {
 public static void main( String [] args )
 {
 char ch[] = new char[100];
 int count=0;
 int len = args.length; //问题1:这里args数组的长度始终为0                 System.out.println( args[0] );//问题2:这里本应输出程序的名字,却抛出异常  for( int i=0; i<len; i++ )
 {
 ch = args[i].toCharArray();
 for( int j=0; j < ch.length; j++ )
 {
 if( ch[j] == 'e' );
 count++;
 }
 }
 System.out.println("the count of e is:" + count );
 }
 }

解决方案 »

  1.   

    那是因为你根本就没有在java recordCharE 命令后面加参数。
      

  2.   

    而且你的程序根本不可能得到正确的结果:if( ch[j] == 'e' );
    count++;无论你的ch[j]等不等于'e',count++都会被执行。
      

  3.   

    /*
     *实现从命令行参数中输入一字符串,统计'e'出先的次数
     *name: recordCharE.java
     */
     public class recordCharE
     {
     public static void main( String [] args )
     {
     char ch[] = new char[100];
     int count=0;
     int len = args.length;                  System.out.println( args[0]);//这里的args是从参数开始计算的.不包括程序名  for( int i=0; i<len; i++ )
     {
     ch = args[i].toCharArray();
     for( int j=0; j < ch.length; j++ )
     {
     if( ch[j] =='e' )count++;
     }
     }
     System.out.println("the count of e is:" + count );
     }
     }
      

  4.   

    为什么if语句后还要加分号呢,这样一来,这个if判断都没起作用了,低级错误改后程序如下:
    /*
     *实现从命令行参数中输入一字符串,统计'e'出先的次数
     *name: recordCharE.java
     */
     public class CountCharE
     {
     public static void main( String [] args )
     {
     char ch[] = new char[100];
     int count=0;
     int len = args.length; //问题1:这里args数组的长度始终为0         try{
              System.out.println( "argslength= " + len + ":" + args[0] + args[1] );
              //问题2:这里本应输出程序的名字,却抛出异常
             }
             catch( Exception e )
             {
              System.err.println( "At least one command line parameter is needed!" );
             }
           
     for( int i=0; i<len; i++ )
     {
     ch = args[i].toCharArray();
     
     for( int j=0; j < ch.length; j++ )
     {
     if( ch[j] == 'e' )
     count++;
     }
     }
     
     System.out.println("the count of e is:" + count );
     }
     }
      

  5.   

    搂主弄明白这几个问题就明白了:
    1.args[]数组的参数不算函数名,格式可表示为 java recordCharE args[0] args[1] ....
    2.每个参数为一个字符串,各个参数以空格相隔
    3.对于本程序,如果要统计命令行参数字符串中的‘e’出现的次数,实际上是统计arg[0]这个字符串中'e'出现的次数;
    4.
    /*
     *实现从命令行参数中输入一字符串,统计'e'出先的次数
     *name: recordCharE.java
     */
     public class recordCharE
     {
     public static void main( String [] args )
     {
     int count=0;
     try
                       {
                           System.out.println(args[0]);
                        }
                        catch(Exception e)
                        {
                            System.err.println("you should enter a string as command line parameter")
                        }
                       char [] ch = args[0].toCharArray(); 
                       for( int j=0; j < ch.length; j++ )
             {
        if( ch[j] == 'e' )
           count++;
             }
    System.out.println("the count of e is:" + count );
     }
     }