【问题标题】:Changing java code from user input to command line argument将 java 代码从用户输入更改为命令行参数
【发布时间】:2015-05-02 06:54:16
【问题描述】:

所以我正在开发一个项目,该项目将采用命令行参数(例如:11:45:12 11:48:13)并以“经过的时间(以秒为单位)为:181”的形式输出

虽然我很难将我的代码从要求用户通过按任意键“启动”计时器更改为从命令行参数获取直接时间。非常感谢任何帮助!下面是我的两个类 Clock 和 TestClock。 testClock 将使用命令行参数运行。

    import java.sql.Time;

 public class Clock {
   public Time startTime;
   public Time endTime;

   Clock() {
   } public void start(String startTime){
       startTime.startTime(convertToSeconds(startTime));


   } public void stop(String endTime){
       endTime.setTime(convertToSeconds(endTime));
   }

import java.util.Scanner;


public class TestClock {
public static void main(String args[]){
    Clock cl = new Clock();
    cl.start(args[0]);
    cl.stop(args[1]);
    System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
}

}

仍然无法找到无法转换为秒的接收错误。与 get elapsed time 相同。

【问题讨论】:

    标签: java command arguments


    【解决方案1】:

    删除System.in 并使用args[0] 和args[1]。 main() 方法签名中的 args 是一个命令行参数数组。

    【讨论】:

      【解决方案2】:

      您需要一种方法来设置stopTime 和startTime。我建议创建另一个接受两个字符串的构造函数。或者,您可以添加新的 stop 和 start 函数来设置它们。

      一旦你有了它,正如 EvervOid 所提到的,使用 args 参数中的相应位置来获取命令行参数并将它们传递给你的新 stop 和 start 函数。 (你不需要删除你已经拥有的,因为Java很聪明,可以区分两个具有相同名称和不同参数的函数。

      最终的main 函数可能类似于:

      public class TestClock {
          public static void main(String args[]){
              Clock cl = new Clock(args[0], args[1]);
              System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
          }
      }
      

      编辑:每个评论问题

      您可以有一个接受开始和结束时间的构造函数,如下所示:

         Clock(String startString, String endString) {
             startTime = new Time(convertToMilliseconds(startString));
             endTime = new Time(convertToMilliseconds(endString));
         }
      

      您需要按照我提到的另一篇文章编写转换函数。

      另一种方法是将以下内容写入函数:

         public void start(String startTime){
             startTime.setTime(convertToMilliseconds(startTime));
             System.out.println("Start time is " + startTime);
         }
      
         public void stop(String endTime){
             endTime.setTime(convertToMilliseconds(endTime));
             System.out.println("End time is " + endTime);
         }
      

      如果你这样做了,那么主函数就变成了:

      public class TestClock {
          public static void main(String args[]){
              Clock cl = new Clock();
              cl.start(args[0]);
              cl.end(args[1]);
              System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
          }
      }
      

      【讨论】:

      • 如果您需要帮助将您的字符串转换为毫秒,您可以考虑post。
      • 你是什么意思新的停止和启动功能来设置它们,那会是什么样子。 (对不起,现在脑子里放个屁。我理解摆脱 Scanner 和“按任意键开始/停止时间”
      • 现在我得到错误,找不到符号 convetToMilliSeconds(string)
      • 公共类时钟{公共时间开始时间;公共时间结束时间;时钟(){}公共无效开始(字符串startTime){startTime.startTime(convertToSeconds(startTime)); } public void stop(String endTime){ endTime.setTime(convertToSeconds(endTime)); } }
      • 这是一个你可以编写的函数的例子。您仍然需要以某种方式将字符串转换为毫秒。如何从字符串转换为时间在其他问题中很好地介绍了,例如我在第一条评论中链接到的问题。
      【解决方案3】:
      import java.sql.Time;
      public class Clock {
      
         //Create two instance of Time class with default values
         //for hour,minute and seconds
         private Time startTime=new Time(0, 0, 0);
         private Time stopTime=new Time(0, 0, 0);
      
      
      
         //set start time
         public void start(Time startTime){
             this.startTime=startTime;
         }
      
      
         //set end time
         public void stop(Time stopTime){
             this.stopTime=stopTime;
         }
      
      
      
         /**The method getElapsedTime calculates the time difference
         * between two times and returns seconds as return value.
         * */
         public int getElapsedTime(){
             int totalSeconds=0;
             int hours=stopTime.getHours()-startTime.getHours();
             if(hours>0) {          
                 int minues=stopTime.getMinutes()-startTime.getMinutes();
                 if(minues>0) {
                     minues=stopTime.getMinutes()-startTime.getMinutes();
                 }
                 else
                     minues=startTime.getMinutes()-stopTime.getHours();
      
                 int seconds=stopTime.getSeconds()-startTime.getSeconds();
                 if(minues>0) {
                     seconds=stopTime.getSeconds()-startTime.getSeconds();
                 }
                 else
                     seconds=startTime.getSeconds()-stopTime.getSeconds();
      
      
                 totalSeconds=hours*60*60+minues*60+seconds;
             }
             else {
      
                 int minutes=stopTime.getMinutes()-startTime.getMinutes();
                 if(minutes>0) {
                     minutes=stopTime.getMinutes()-startTime.getMinutes();
                 }
                 else
                     minutes=startTime.getMinutes()-stopTime.getMinutes();
      
                 int seconds=stopTime.getSeconds()-startTime.getSeconds();
                 if(seconds>0) {
                     seconds=stopTime.getSeconds()-startTime.getSeconds();
                 }
                 else
                     seconds=startTime.getSeconds()-stopTime.getSeconds();
      
      
                 totalSeconds=hours*60*60+minutes*60+seconds;
             }
             //return seconds              
             return totalSeconds;
         }//end of the getElapsedTime method
      
      
      }//end class
      

      import java.sql.Time;
      public class TestClock {
         public static void main(String[] args) {
      
      
             //Create an instance of Clock class object
             Clock clock=new Clock();      
             //Get first argument
             String startTime=args[0];
      
             //split the argument and get hour,minute and second
             String [] time=startTime.split(":");
      
             //split the argument and get hour,minute and second
             int hour=Integer.parseInt(time[0]);
             int minutes=Integer.parseInt(time[1]);
             int seconds=Integer.parseInt(time[2]);
      
      
             //call start method with an instance Time class
             clock.start(new Time(hour, minutes, seconds));
      
             //Get first argument
             String endTime=args[1];
             String [] stopTime=endTime.split(":");
      
             //split the argument and get hour,minute and second
             hour=Integer.parseInt(stopTime[0]);
             minutes=Integer.parseInt(stopTime[1]);
             seconds=Integer.parseInt(stopTime[2]);
             //call start method with an instance Time class
             clock.stop(new Time(hour, minutes, seconds));
      
             System.out.println("Elapsed time in seconds :"+clock.getElapsedTime());
      
         }//end main
      
      }//end class
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-16
        • 2015-03-26
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        相关资源
        最近更新 更多