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

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


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


按照我的方式去做似乎行不通。我相信我的问题在于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);




   }  


}


慕姐4208626
浏览 83回答 4
4回答

喵喔喔

您正在覆盖变量。这应该是这样的:argspublic 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;}

胡说叔叔

如何使用 一个,并在其中有一个方法,该方法具有与类的方法相同的覆盖签名:interfaceStatinterface StatsOperation{&nbsp; &nbsp; &nbsp; &nbsp;double compute(List<Double> data);}虽然类将如下所示:Statclass 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 方法中,您可以使用 a 来保存类的方法引用(假设您使用的是 Java-8):MapStatspublic 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;}因此,根据您在数组中传递的值,我们将通过从 中获取实现来调用该操作。这采用运行时多态性来决定在运行时调用哪个方法。argsMap

青春有我

当使用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; }

慕的地10843

命令行参数是在命令行上的“java classname”之后指定的单词。因此,如果您执行以下命令:java Statdriver mean...您的方法将被赋予一个长度为一的数组,包含为元素。mainargs"mean"如果执行此命令:java Statdriver std...您的方法将被赋予一个长度为一的数组,包含为元素。mainargs"std"在所有这些情况下,长度为一。您应该只检查 。没有 ,尝试访问它将引发异常。argsargs[0]args[1]正如Andronicus所指出的,您正在尝试为 分配值,而您不应该这样做。事实上,你根本不需要改变。argsargs只需检查是否等于每个有意义的字符串值:args[0]&nbsp; if (args[0].equals("mean"))&nbsp; {&nbsp; &nbsp; &nbsp;mean = Stats.mean(data);&nbsp; &nbsp; &nbsp;System.out.println("Mean: " + mean);&nbsp; }&nbsp; else if (args[0].equals("std"))&nbsp; {&nbsp; &nbsp; &nbsp;standardDeviation = Stats.stdDev(data);&nbsp; &nbsp; &nbsp;System.out.println("StdDev: " + standardDeviation);&nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; &nbsp;mean = Stats.mean(data);&nbsp; &nbsp; &nbsp;System.out.println("Mean: " + mean);&nbsp; }它有助于考虑您实际要检查的内容。好的代码读起来像自然思维或说话:“如果第一个参数是”均值“,则计算均值,否则如果第一个参数是”std“,则计算标准差”等等。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java