这是肿么了?
interface Vehicle{
double getSpeed(double a,double b,double c);
}
class Car implements Vehicle{
public double getSpeed(double a,double b,double c){
return (a*b/c);
}
}
class Plane implements Vehicle{
public double getSpeed(double a,double b,double c){
return (a+b+c);
}
}
class ComputeTime{
static double getTime(Vehicle v,double len,double a,double b,double c){
return len/v.getSpeed(a, b, c);
}
}
public class Test1{
static double getTime(Vehicle v,double len,double a,double b,double c){
return len/v.getSpeed(a, b, c);
}
public static void main(String args[]){
if(args.length!=4){
System.out.println( "请先指定正确形式的参数! ");
System.exit(-1);
}
System.out.println( "交通工具: "+args[0]);
System.out.println( "参数A: "+args[1]);
System.out.println( "参数B: "+args[2]);
System.out.println( "参数C: "+args[3]);
double A=Double.parseDouble(args[1]);
double B=Double.parseDouble(args[2]);
double C=Double.parseDouble(args[3]);
double v,t;
try{
Vehicle d=(Vehicle) Class.forName(args[0]).newInstance(); v=d.getSpeed(A, B, C);
t=ComputeTime.getTime(d, 1000, A, B, C);
System.out.printf( "平均速度=%.2fkm/h\n ",v);
System.out.printf( "平均速度=%.2fkm/h\n ",t);
}catch(Exception e){
System.out.printf( "class not found ");
}
}
}
运行的结果是:请先指定正确形式的参数!
这是为什么啊?TKS
------解决方案--------------------
你直接运行肯定会输出:请先指定正确形式的参数!
因为你要向程序入口传参数之后才会执行的
你要使用Eclipse的话,右键>run as >run configurations>选择Java Application》
然后选择右边的Arguments,然后把入口参数填到program arguments里面就行了
每一个参数换一行,按照你程序写的,你的入口参数数量,要等于4才会执行的,所以你必须要有四个参数