簡體   English   中英

將java代碼從用戶輸入更改為命令行參數

[英]Changing java code from user input to command line argument

所以我正在開發一個將采用命令行參數的項目(例如: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");
}

}

仍然無法找到仍無法轉換為秒的錯誤。 獲得經過的時間相同。

刪除System.in並使用args[0]args[1] main()方法簽名中的args是一個命令行參數數組。

你需要一種方法來設置stopTimestartTime 我建議創建另一個帶兩個字符串的構造函數。 或者,您可以添加新的stopstart功能,以便您設置它們。

然后,就像EvervOid所提到的那樣,使用args參數中的相應位置來獲取命令行參數並將它們傳遞給新的stopstart函數。 (您不需要刪除已有的Java,因為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);
   }

如果你這樣做,那么main函數變為:

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");
    }
}
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM