/*
*实现从命令行参数中输入一字符串,统计'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 );
}
}
*实现从命令行参数中输入一字符串,统计'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 );
}
}
解决方案 »
- myeclipse的debug--F5,F6的作用和F8一样
- UDP里的DatagramPacket 对象的疑问
- 高分问菜鸟问题,jvm & jre ,到底有什么jvm不能做而jre可以做?
- An unexpected error has been detected by Java Runtime Environment:
- 关于接口打包问题
- 我做了一个简单的package,但出错,大家帮忙看一下
- XP下怎么用jdk?
- JAVA大家都用什么开发环境?
- 98下如何用JDK进行编译,谢谢
- java实现小车在公路上跑,那公路应该怎么画呢?
- >>>java用jni 调用dll的问题,怎么写dll部分<<<
- 急问jni如何传递类到VC中?
count++;无论你的ch[j]等不等于'e',count++都会被执行。
*实现从命令行参数中输入一字符串,统计'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 );
}
}
/*
*实现从命令行参数中输入一字符串,统计'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 );
}
}
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 );
}
}