命令行参数传递给调用方法

如何将字符串的命令行参数传递给 java main 方法,该方法将调用另一个类中的特定方法?


我需要能够传递“java Statdriver mean”将调用 mean 方法或“java Statdriver std”将调用标准偏差方法,或者不传递任何参数将导致通过终端调用 mean 方法。


按我的方式做似乎行不通。我认为我的问题在于 if、else if、else 循环。


这是我的代码:


public class StatDriver

{


   public static void main(String[] args)

   {

      Scanner input = new Scanner(System.in);


      double total;

      double mean;

      double standardDeviation;

      double average;



      total = 0;


      args[0] = "mean";

      args[1] = "std";


      ArrayList<Double> data = new ArrayList<Double>();


      while(input.hasNextDouble())

      {

         data.add(input.nextDouble());

      }


      if(args.equals(args[0]))

      {

         mean = Stats.mean(data);

         System.out.println("Mean: " + mean);

      }


      else if(args.equals(args[1]))

      {

         standardDeviation = Stats.stdDev(data);

         System.out.println("StdDev: " + standardDeviation);

      }


      else

      {

         mean = Stats.mean(data);

         System.out.println("Mean: " + mean);

      }





     // mean = Stats.mean(data);

    //standardDeviation = Stats.stdDev(data);



    //System.out.println("Mean: " + mean);

    // System.out.println(" StdDev: " + standardDeviation);




   }  


}


BIG阳
浏览 84回答 3
3回答

杨__羊羊

您正在覆盖args变量。这应该是这样的:public class StatDriver{&nbsp; &nbsp;public static void main(String[] args)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; double total;&nbsp; &nbsp; &nbsp; double mean;&nbsp; &nbsp; &nbsp; double standardDeviation;&nbsp; &nbsp; &nbsp; double average;&nbsp; &nbsp; &nbsp; total = 0;&nbsp; &nbsp; &nbsp; String[] possibleArgs = new String[2];&nbsp; &nbsp; &nbsp; possibleArgs[0] = "mean";&nbsp; &nbsp; &nbsp; possibleArgs[1] = "std";&nbsp; &nbsp; &nbsp; ArrayList<Double> data = new ArrayList<Double>();&nbsp; &nbsp; &nbsp; while(input.hasNextDouble())&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data.add(input.nextDouble());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(possibleArgs[0].equals(args[0]))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mean = Stats.mean(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Mean: " + mean);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else if(possibleArgs[1].equals(args[1]))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;standardDeviation = Stats.stdDev(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("StdDev: " + standardDeviation);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mean = Stats.mean(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Mean: " + mean);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp;&nbsp;}

智慧大石

使用 Java 反射特性时,解决方案简单易行。这是代码&nbsp; &nbsp; import java.lang.reflect.Method;&nbsp; &nbsp; import java.util.ArrayList;&nbsp; &nbsp; import java.util.Scanner;&nbsp; &nbsp; public class StatDriver {&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Double> data = new ArrayList<Double>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(input.hasNextDouble()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.add(input.nextDouble());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int length = args.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double mean = Stats.mean(data);&nbsp; // By default , mean method is called&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" Result (Mean) :"+mean);&nbsp; &nbsp; }&nbsp; &nbsp; else if(length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; String methodName = args[0];&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method method = Stats.class.getMethod(methodName, ArrayList.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object result = method.invoke(null , data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Result of the method :"+methodName+" is "+result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class Stats{&nbsp; &nbsp; &nbsp; &nbsp; public static double mean(ArrayList aList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double result = 0.0D;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Logic here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static double stdDev(ArrayList aList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double result = 0.0D;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Logic here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕的地8271018

如何在其中使用一个interface与类的方法具有相同覆盖签名的方法Stat:interface StatsOperation{&nbsp; &nbsp; &nbsp; &nbsp;double compute(List<Double> data);}而Stat类将如下:class Stats{&nbsp; &nbsp; &nbsp; &nbsp; public static double mean(List<Double> data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("mean invoked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //compute mean&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static double stdDev(List<Double> data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("stddev invoked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//compute std dev&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}现在在 main 方法中,您可以使用 aMap来保存类的方法引用Stats(假设您使用的是Java-8):public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; Map<String, StatsOperation> lookup = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; lookup.put("mean", Stats::mean); //"mean" corresponds to Stats::mean implementation&nbsp; &nbsp; &nbsp; &nbsp; lookup.put("stddev", Stats::stdDev); //"stddev" corresponds to Stats::mean implementation&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Double> data = new ArrayList<Double>();&nbsp; &nbsp; &nbsp; &nbsp; while(input.hasNextDouble())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.add(input.nextDouble());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data.size() == 5) break; //breaking after 5 elements&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; StatsOperation operation = args.length > 0 && lookup.get(args[0]) != null ? lookup.get(args[0]) : lookup.get("mean"); //by default calling mean if nothing is sent in args array&nbsp; &nbsp; &nbsp; &nbsp; double result = operation.compute(data);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Output " + result);&nbsp; &nbsp; &nbsp; &nbsp; // other business logic below&nbsp;}因此,根据您在args数组中传递的值,我们将通过从Map. 这采用运行时多态性来决定在运行时调用哪个方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java